这个问题可能很愚蠢,我刚开始学习chrome API。但是在寻找答案时我真的很疲惫。在这里,我们有:将chrome API操作封装成函数
当我使用这样的结构,一切都OK:
chrome.tabs.executeScript(tabID, { file: "jquery.js" }, function() {
chrome.tabs.executeScript(tabID, { file: "script.js" }, function(res) {
alert(res); // I can use this result inside this block
});
});
的问题是,我想封装这个代码到这样的功能:
function aFunction(tabID)
{
chrome.tabs.executeScript(tabID, { file: "jquery.js" }, function() {
chrome.tabs.executeScript(tabID, { file: "script.js" }, function(res) {
return res;
});
});
}
然后我使用该函数是这样的:
alert(aFunction(tabID));
通过此警报指令所提供的信息是“理解过程罚款”。我尝试了很多方法来编写这个函数,但是我经常把“undefined”作为返回结果。
希望有一种方法可以植入我想要的东西。
在此先感谢。
这是一个异步调用!它不会像那样工作。原始函数调用在executeScript行运行之前返回。由于函数中没有返回值,因此您会得到未定义的值。 – epascarello
是的,我明白这一点),但我认为把回归指令放到一个我们真正得到回应的块中就能解决问题。 –
@ITcrusader它不起作用,因为'aFunction'必须在执行'executeScript'回调之前声明它的返回值并终止*。因此,'executeScript'不可能影响'aFunction'的返回值。 – apsillers