【问题标题】:Chrome Extension Working only for one URLChrome 扩展仅适用于一个 URL
【发布时间】:2013-02-12 11:58:04
【问题描述】:

我正在创建一个 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"); 函数添加事件(){ chrome.extension.sendMessage({change: "icon"}, function(response) { 控制台.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 中没有改变?请说明原因

【问题讨论】:

    标签: google-chrome iframe google-chrome-extension


    【解决方案1】:

    有多个问题

    • 您是否希望 id 为 frame1 的 DOM 元素存在于 http://stackoverflow.com document.getElementById("frame1");
    • document.body.innerHTML=''+document.body.innerHTML+'';有什么意义?

    【讨论】:

    • document.body.innerHTML='
      onemoredemo.appspot.com/">< iframe>
      ' +document.body.innerHTML+'
      ' ;
    • 我正在使用javascript添加frame1然后获取它
    • 由于某些格式原因,现在省略了,请回复
    • @RiteshMehandiratta:如果代码格式化有困难,可以链接到pastebin.com,它会给我清晰的图片。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    相关资源
    最近更新 更多