-1

我正在创建一个 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"
  }
}
  1. 作为内容脚本的 myscript.js 的代码是

    document.body.innerHTML=''+document.body.innerHTML+'' ; var iframe = document.getElementById("frame1"); 函数 addevent(){ 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 之间切换

当我将它加载到带有 url http://onemoredemo.appspot.com/的网页中时,此扩展程序完美运行,创建了框架,当我单击按钮时它正在更改图标,但是当我打开 url stackoverflow.com 时,框架被创建并显示按钮在其中,但是当我单击按钮图标保持不变时,我希望该图标将被更改而不管 url 为什么它在其他 url 中没有改变?请说明原因

4

1 回答 1

0

There are multiple issues

  • Do you expect a DOM Element with id frame1 to exist on http://stackoverflow.com document.getElementById("frame1");
  • What is significance of document.body.innerHTML=''+document.body.innerHTML+'';?
于 2013-02-12T12:26:27.787 回答