我正在创建Chrome扩展程序 我创造了一个指挥 1.background.js 2.icon.png 3.icon2.png 4.manifest.json 5.myscript.js
我的manifest.json代码是
{
"manifest_version": 2,
"name": "One-click Kittens",
"description": "This extension demonstrates a browser action with kittens.",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"permissions": [ "storage"],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["myscript.js"]
}
],
"browser_action": {
"default_icon": "icon.png"
}
}
myscript.js的代码是内容脚本
document.body.innerHTML =''+ document.body.innerHTML +''; var iframe = document.getElementById(“frame1”); function addingevent(){ chrome.extension.sendMessage({change:“icon”},function(response){ 的console.log(response.resp); });
}
iframe.onload = function(){
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if(iframeDocument != null)
{var bb=iframeDocument.getElementById('mybutton');
if(bb != null)
{
bb.addEventListener("click",addingevent,false)
alert("Hello World");
}}
}
和background.js的代码是
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("from a content script:from the extension");
chrome.storage.local.get("icon",function(val){
if(val.icon == "icon1")
{chrome.browserAction.setIcon({"path" :"icon2.png"},function(){});
chrome.storage.local.set({"icon":"icon"},function(){});
}
else
{chrome.browserAction.setIcon({"path" :"icon.png"},function(){});
chrome.storage.local.set({"icon":"icon1"},function(){});
}
});
sendResponse({resp: "success"});
});
此扩展的目标是当用户点击从url http://onemoredemo.appspot.com/图标加载的框架中的按钮时,应在icon.png和icon2.png之间切换
当我在网页中加载网址时,这个扩展功能非常有效{urg http://onemoredemo.appspot.com/创建了框架,当我点击按钮时它正在改变图标但是当我打开url stackoverflow.com然后框架被创建并显示一个按钮它,但是当我点击按钮图标保持不变时,我预计该图标将被更改而不管url为什么它在其他网址中没有变化?请指明原因
答案 0 :(得分:0)
有多个问题
frame1
的DOM元素
的document.getElementById( “帧1”); document.body.innerHTML=''+document.body.innerHTML+'';
的重要性是什么?