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

我正在创建一个Chrome扩展,我用1.background.js创建了一个diretory 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”); 函数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之间切换

    这个扩展工作完美,当我加载网址与url http://onemoredemo.appspot.com/有框架创建,当我点击按钮它正在改变图标,但当我打开url stackoverflow.com然后框架创建并显示按钮,但是当我点击按钮图标仍然是相同的,我期待这个图标将被改变,而不考虑url为什么它不会改变其他网址??请说明原因


    有多个问题

  • 你期望一个带有id frame1的DOM元素存在于http://stackoverflow.com document.getElementById(“frame1”);
  • 什么是document.body.innerHTML=''+document.body.innerHTML+'';重要性document.body.innerHTML=''+document.body.innerHTML+'';
  • 链接地址: http://www.djcxy.com/p/18339.html

    上一篇: Chrome Extension Working only for one URL

    下一篇: Is the "iframe sandbox" technique safe?