因此,我有一个我正在编写的扩展,当用户单击pageAction图标时,我试图执行一个脚本。单击图标时,该方法将调用chrome.tabs.executeScript(.)。问题是chrome.tabs.executeScript函数没有执行,我也不知道为什么。我知道我正在进入它调用executeScript的代码,因为我在那里出现了一个警报。以下是我的一些代码:
manifest.json
{
"manifest_version": 2,
"name": "name here",
"description": "description here",
"version": "0.1",
"permissions": [
"<all_urls>",
"tabs"
],
"icons": {
"16" : "images/icon16.png",
"48" : "images/icon48.png",
"128": "images/icon128.png"
},
"background": {
"scripts": ["js/check.js"]
},
"page_action": {
"default_icon": {
"19": "images/icon19.png",
"38": "images/icon38.png"
},
"default_title": "default title here"
}
}
js/check.js
chrome.tabs.onUpdated.addListener(checkForValidUrl);
function checkForValidUrl(tabId, changeInfo, tab) {
if (tab.url.indexOf('g') > -1) {
chrome.pageAction.show(tabId);
}
};
chrome.pageAction.onClicked.addListener(function(tab) {
alert("hello world"); //this code is executed...
//...this code is not
chrome.tabs.executeScript(tab.id, {file: "save.js"}, function() {
if(chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
}
});
});
js/save.js
alert("hello world");
正如我在代码中所说的,我的pageAction onClick函数中的hello可以工作。executeScript方法没有。任何关于正在发生的事情的想法都是有帮助的。
发布于 2013-06-25 03:07:15
在我的代码中处理了很多不同的东西之后,我找到了解决问题的方法。错误似乎出现在{file: "save.js"}
的一行中。当它寻找save.js
时,它显然是在最上面的目录中,我的manifest.json
文件所在的位置,而不是我的代码所在的目录中。为了找到我的{file: "js/save.js"}
文件,我不得不将代码更改为save.js
。
https://stackoverflow.com/questions/17288204
复制相似问题