Chrome Extension Working only for one URL

i am creating a chrome extension i created a diretory with 1.background.js 2.icon.png 3.icon2.png 4.manifest.json 5.myscript.js

my manifest.json code is

{
  "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"
  }
}
  • code for myscript.js which is content script is

    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");
    
        }}
    
        }
    
  • and the code for background.js is

    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"});
    
      });
    

    objective of this extension is when user click on the button which is in the frame loaded from url http://onemoredemo.appspot.com/ icon should be toggled between icon.png and icon2.png

    this extension works perfectly when i load it in webpage with url http://onemoredemo.appspot.com/ there is frame created and when i click button it is changing icon but when i open url stackoverflow.com then frame is created and showing a button in it,but when i click on the button icon remains same i am expecting that icon will be changed irrespective of url why its not changing in other url??please specify the reason


    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+''; ?
  • 链接地址: http://www.djcxy.com/p/18340.html

    上一篇: 防止全球范围的污染

    下一篇: Chrome扩展程序仅适用于一个网址