【发布时间】:2013-06-25 11:18:04
【问题描述】:
所以我有一个正在编写的扩展程序,当用户单击 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 world 有效。 executeScript 方法没有。任何关于正在发生的事情的想法都会有所帮助。
【问题讨论】:
-
另外,在
executeScript(...)方法返回后应该触发的console.error(...)代码永远不会被调用,所以这对我没有帮助。
标签: javascript google-chrome google-chrome-extension