Javascript Chrome扩展-在新打开的选项卡中不工作
我正在开发一个chrome扩展,除了在新标签中,它在所有场景中都工作得很好 i、 例如,只有在为stackoverflow.com等网站打开时,扩展才起作用。当我按ctrl+t并单击我的扩展图标时,它不起作用 我做错了什么?还是浏览器的行为 我已经添加了代码供您参考 清单Javascript Chrome扩展-在新打开的选项卡中不工作,javascript,google-chrome,google-chrome-extension,google-chrome-app,Javascript,Google Chrome,Google Chrome Extension,Google Chrome App,我正在开发一个chrome扩展,除了在新标签中,它在所有场景中都工作得很好 i、 例如,只有在为stackoverflow.com等网站打开时,扩展才起作用。当我按ctrl+t并单击我的扩展图标时,它不起作用 我做错了什么?还是浏览器的行为 我已经添加了代码供您参考 清单 { "manifest_version": 2, "background": { "scripts": ["scripts/background.js"], "persiste
{
"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"
]
}
background.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');
}
});
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();
});
}
});
问题可能在于您的清单权限
新选项卡页面不使用
http://
,而是使用chrome://newtab
,因此您可能需要将其添加到您的权限中,才能使扩展正常工作。尝试将下面的代码添加到manifest.json中
"chrome_url_overrides": {
"newtab": "blank.html"
}
blank.html:(创建自己的版本)
空白新标签页
div{
颜色:#中交;
垂直对齐:50%;
文本对齐:居中;
字体系列:无衬线;
字体大小:300%;
}
空白新标签&交易;
在“权限”内添加“chrome://*/*”(对于新选项卡),然后打开chrome并转到chrome://flags/#extensions-在chrome URL上并启用此设置。我尝试过,它会显示“权限”chrome://newtab“未知或URL模式格式不正确。”此外,我尝试了所有可能的方法,比如“*://*”、“chrome://*”、“chrome://*/”、
等等。运气不好!相同的警告权限“*”未知或URL模式格式不正确。
!这无助于通过background.js注入脚本,我认为,我们无法通过浏览器操作覆盖新的选项卡页面。非常感谢你的建议,真的很有帮助请记住,内容脚本不会注入任何chrome://
或扩展库页面
<html>
<head>
<title>Blank New Tab</title>
<style>
div {
color: #cccccc;
vertical-align: 50%;
text-align: center;
font-family: sans-serif;
font-size: 300%;
}
</style>
</head>
<body>
<div style="height:40%"></div>
<div>Blank New Tab™</div>
</body>
</html>