Javascript 内容脚本未执行
我已经创建了chrome扩展,当我点击扩展时,它应该会改变页面的主体颜色,但我希望它在页面加载时完成,当警报显示控件正在传递代码时,它也不会调用函数。请帮忙解决这个问题 ----------manifest.json----------- ------------------background.js--------------------Javascript 内容脚本未执行,javascript,google-chrome,javascript-events,google-chrome-extension,Javascript,Google Chrome,Javascript Events,Google Chrome Extension,我已经创建了chrome扩展,当我点击扩展时,它应该会改变页面的主体颜色,但我希望它在页面加载时完成,当警报显示控件正在传递代码时,它也不会调用函数。请帮忙解决这个问题 ----------manifest.json----------- ------------------background.js-------------------- 您的doWhatYouWant函数位于后台页面中,因此您无法从内容脚本调用它 如果您要运行的代码长度超过两行,请考虑将其放入扩展中的文件中,并在调用代码>
您的
doWhatYouWant
函数位于后台页面中,因此您无法从内容脚本调用它
如果您要运行的代码长度超过两行,请考虑将其放入扩展中的文件中,并在调用代码> Chrop.Tabs,ExcuteScript < /COD> < /P>时,在CuxEdvices参数中使用<代码>文件:字段。 此外,如果希望它在每个页面上运行,可以在清单中将其声明为内容脚本,而不是更新
chrome.tabs.onListener.google内容脚本教程中有一个“prince”示例:
我在学习“谷歌扩展”(?)时尝试过,效果很好。
祝你好运
艾德-呃,忘了,文件是
{
"name": "My Page changer",
"description": "Make the current page red",
"version": "2.0",
"permissions": [
"activeTab"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_title": "Make this page blue"
},
"manifest_version": 2
}
debugger;
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
debugger;
alert('I am here too ');
/*chrome.tabs.executeScript({
code: 'document.body.style.backgroundColor="blue"'
//code : 'doWhatYouWant()'
});*/
// Execute some script when the page is fully (DOM) ready
chrome.tabs.executeScript(null, {code:"doWhatYouWant();"});
}
});
function doWhatYouWant(){
alert('I am inside doWhatYouWant');
document.body.style.backgroundColor="blue";
}
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
// No tabs or host permissions needed!
alert('i am here');
console.log('Turning ' + tab.url + ' red!');
chrome.tabs.executeScript({
code: 'document.body.style.backgroundColor="red"'
});
});