Chrome 扩展没有自动运行。内容脚本未执行
Chrome extension is not runing auto. Content scripts not executing
我正在试验 Google Chrome 扩展,我遇到的第一个问题是我想在加载页面时自动更改正文背景颜色(无需单击扩展图标)但是我的内容脚本没有启动:
manifest.json:
{
"name": "Test Extension",
"description": "Testing",
"version": "1.0",
"permissions": [
"tabs", "webNavigation"
],
"browser_action": {
"default_title": "Set this page's color.",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["core.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2
}
core.js:
chrome.tabs.executeScript(null,{code:'document.body.style.backgroundColor = "green";'});
无效解决方案background.js:
chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) {
chrome.tabs.executeScript(null,{file:"core.js"});
});
问题出在哪里?请帮忙。
更好的解决方案是只使用内容脚本并等待加载而不执行脚本:
manifest.json
{
"name": "Test Extension",
"description": "Testing",
"version": "1.0",
"permissions": [
"tabs", "webNavigation"
],
"browser_action": {
"default_title": "Set this page's color.",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["core.js"],
"run_at": "document_end"
}
],
"manifest_version": 2
}
core.js
window.onload=function(){
console.log("test!!");
document.body.style.backgroundColor = "green"; }
popup.html不管这里。
我只是尝试了一下,它可以在任何站点(您可以在 Whosebug 上尝试)和控制台中将背景颜色更改为绿色。
我正在试验 Google Chrome 扩展,我遇到的第一个问题是我想在加载页面时自动更改正文背景颜色(无需单击扩展图标)但是我的内容脚本没有启动:
manifest.json:
{
"name": "Test Extension",
"description": "Testing",
"version": "1.0",
"permissions": [
"tabs", "webNavigation"
],
"browser_action": {
"default_title": "Set this page's color.",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["core.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2
}
core.js:
chrome.tabs.executeScript(null,{code:'document.body.style.backgroundColor = "green";'});
无效解决方案background.js:
chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) {
chrome.tabs.executeScript(null,{file:"core.js"});
});
问题出在哪里?请帮忙。
更好的解决方案是只使用内容脚本并等待加载而不执行脚本:
manifest.json
{
"name": "Test Extension",
"description": "Testing",
"version": "1.0",
"permissions": [
"tabs", "webNavigation"
],
"browser_action": {
"default_title": "Set this page's color.",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["core.js"],
"run_at": "document_end"
}
],
"manifest_version": 2
}
core.js
window.onload=function(){
console.log("test!!");
document.body.style.backgroundColor = "green"; }
popup.html不管这里。 我只是尝试了一下,它可以在任何站点(您可以在 Whosebug 上尝试)和控制台中将背景颜色更改为绿色。