【发布时间】:2014-03-04 13:42:15
【问题描述】:
我正在开发一个 chrome 扩展,它在所有场景中都运行良好,除了在新标签页中。
即,扩展程序仅在打开网站时才有效,例如。 stackoverflow.com。当我按 ctrl+t 并单击我的扩展程序图标时,它不起作用。
我做错了什么?还是浏览器行为?
我已经添加了我的代码供你参考。
清单
{
"manifest_version": 2,
"background": {
"scripts": ["scripts/background.js"],
"persistent": false
},
"content_scripts":[{
"matches" : ["<all_urls>"],
"js": ["scripts/jquery-2.1.0-min.js", "scripts/init.js"],
"run_at": "document_end"
}],
"permissions": [
"storage", "activeTab", "http://*/*", "https://*/*"
],
"browser_action": {
"default_icon": "images/plugin-icon-24.png"
},
"web_accessible_resources": [
"*.html",
"images/*.gif",
"images/*.png"
]
}
init.js
chrome.storage.sync.get('logged_in', function(status){
if(status.logged_in){
chrome.runtime.sendMessage('LOGGED_IN');
} else {
chrome.runtime.sendMessage('NOT_LOGGED_IN');
}
});
background.js
var add_resource = function(){
chrome.tabs.executeScript({
file: 'scripts/plugin.js'
});
chrome.tabs.insertCSS({
file: 'styles/plugin.css'
});
};
chrome.runtime.onMessage.addListener(function(message){
alert(message);
/*This alerts comes even in the newly opened tab.
But the script is not getting executed.*/
if(message == 'LOGGED_IN'){
add_resource();
} else {
chrome.browserAction.onClicked.addListener(function(tab){
add_resource();
});
}
});
【问题讨论】:
标签: javascript google-chrome google-chrome-extension google-chrome-app