我有这部分代码(一切都在 background.js 中),它基本上根据页面的 URL 在页面中执行 javascript。它应该适用于工具栏按钮单击和 Ctrl + Q 命令。
我将它分配给这样的按钮单击,它可以工作:
chrome.browserAction.onClicked.addListener(function(tab) {
browser.tabs.query(
{active:true},
function(tabs) {
var tab = tabs[0];
if(tab.url.indexOf("app.fillz.com/orders/edit") != -1){
chrome.tabs.executeScript({
file: "fillz.js"
});
} else if(tab.url.indexOf("amazon") != -1 && tab.url.indexOf("buy/addressselect/handlers/display.html") != -1){
chrome.tabs.executeScript({
file: "amazon.js"
});
}
}
);
});
问题是当我尝试将相同的代码分配给 onCommand 事件时,如下所示:
chrome.commands.onCommand.addListener(function(command) {
if (command == "fills2amazonCopyAndPaste") {
browser.tabs.query(
{active:true},
function(tabs) {
var tab = tabs[0];
if(tab.url.indexOf("app.fillz.com/orders/edit") != -1){
console.log("FILLZ");
chrome.tabs.executeScript({
file: "fillz.js"
});
} else if(tab.url.indexOf("amazon") != -1 && tab.url.indexOf("buy/addressselect/handlers/display.html") != -1){
console.log("AMAZON");
chrome.tabs.executeScript({
file: "amazon.js"
});
}
}
);
}
});
命令被识别,也是当前活动选项卡的 url,我用上面代码中的这两个 console.log() 命令检查了它,但是当我尝试用命令执行它时,脚本不会被执行。在浏览器控制台中,只有这个错误:
Unchecked lastError value: Error: No matching window
源自://gre/modules/ExtensionUtils.jsm
可能是什么问题,我知道我的 manifest.json 文件没问题,因为命令被识别,但它根本不会执行 javascript ...