调用chrome.tabs.query后,结果不可用 [英] After calling chrome.tabs.query, the results are not available
问题描述
我正在为 Google Chrome 创建(学习)一个扩展程序.
I'm creating (learning) an extension for Google Chrome.
为了调试一些代码,我插入了console.log()
,如下:
To debug some code, I inserted console.log()
, as follows:
var fourmTabs = new Array();
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
fourmTabs[i] = tabs[i];
}
});
for (var i = 0; i < fourmTabs.length; i++) {
if (fourmTabs[i] != null)
window.console.log(fourmTabs[i].url);
else {
window.console.log("??" + i);
}
}
代码非常简单:将所有标签信息放入我自己的数组中,然后打印一些内容.
It's very simple code: get all tabs info into an array of my own, and print some things.
为了检查代码是否正常工作,我运行了代码.问题来了:
To check whether the code works as it should, I run the code. Here comes the problem:
- 当我使用断点(通过开发者工具)时,代码运行良好.
- 如果没有断点,则不会打印任何内容.
知道为什么吗?
推荐答案
您的问题可以简化为:
/*1.*/ var fourmTabs = [];
/*2.*/ chrome.tabs.query({}, function(tabs) {
/*3.*/ fourmTabs[0] = tabs[0];
/*4.*/ });
/*5.*/ console.log(fourmTabs[0]);
您希望在到达第 5 行时更新 fourmTabs
数组(按第 3 行).
这是错误的,因为chrome.tabs.query
方法是异步.
You expect that the fourmTabs
array is updated (by line 3) when line 5 is reached.
That is wrong, because the chrome.tabs.query
method is asynchronous.
为了让您了解异步方面的重要性,我展示了一个与您的代码具有相同结构的代码片段和一个故事.
In an attempt to make you understand the significance of the asynchronous aspect, I show a code snippet with the same structure as your code and a story.
/*1.*/ var rope = null;
/*2.*/ requestRope(function(receivedRope) {
/*3.*/ rope = receivedRope;
/*4.*/ });
/*5.*/ grab(rope);
- 在第 1 行,宣布存在绳索.
- 在第 2-4 行,创建了一个 回调函数,它应该被
requestRope
函数调用. - 在第 5 行,您将通过
grab
函数抓住绳子. - At line 1, the presence of a rope is announced.
- At lines 2-4, a callback function is created, which ought to be called by the
requestRope
function. - At line 5, you're going to grab the rope via the
grab
function.
当requestRope
同步实现时,没有问题:
你:我想要一根绳子.当你有绳子时,请扔绳子调用回调函数"."
她:当然."扔绳子
你:跳跃并抓住绳子 - 你设法到达另一边,活着.
When requestRope
is implemented synchronously, there's no problem:
You: "Hi, I want a rope. Please throw the rope"call the callback function" when you've got one."
She: "Sure." throws rope
You: Jumps and grabs rope - You manage to get at the other side, alive.
当requestRope
是异步实现的,如果你把它当成同步的,你可能会遇到问题:
你:请向我扔绳子."
她:当然.让我们看看……"
你:跳跃并试图抓住绳子因为没有绳子,你会摔倒而死.
她:扔绳子 当然太晚了.
When requestRope
is implemented asynchronously, you may have a problem if you treat it as synchronous:
You: "Please throw a rope at me."
She: "Sure. Let's have a look..."
You: Jumps and attempts to grab rope Because there's no rope, you fall and die.
She: Throws rope Too late, of course.
现在您已经了解了异步和同步实现的函数之间的区别,让我们解决您最初的问题:
Now you've seen the difference between an asynchronously and synchronously implemented function, let's solve your original question:
var fourmTabs = new Array();
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
fourmTabs[i] = tabs[i];
}
// Moved code inside the callback handler
for (var i = 0; i < fourmTabs.length; i++) {
if (fourmTabs[i] != null)
window.console.log(fourmTabs[i].url);
else {
window.console.log("??" + i);
}
}
});
// <moved code inside callback function of chrome.tabs.query>
使用断点,您的代码可以工作,因为在到达代码的第二部分时,回调已经被调用.
With breakpoints, your code works, because by the time that the second part of the code is reached, the callback has already been called.
这篇关于调用chrome.tabs.query后,结果不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!