【问题标题】:Chrome extension content scripts and iframeChrome 扩展内容脚本和 iframe
【发布时间】:2011-09-28 01:27:39
【问题描述】:

我正在尝试为 google chrome 做一个扩展来做这些事情:

加载 www.example.com 时运行 example.com 有一个指向另一个站点的 iframe,我需要从这个 iframe 访问一个链接(这是 rapidshare,我需要获取下载链接)

到目前为止还不错,但是..

然后我需要扩展来通知 example.com 的链接 url 以进行进一步处理。

有什么想法或方向吗??

我已经阅读了http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication,但无法让它发挥作用...

【问题讨论】:

标签: javascript google-chrome-extension


【解决方案1】:

v3:serg 的回答,什么对我有用

"manifest_version": 3,
"content_scripts": [
    {
      "matches": ["http://www.rapidshare.com/*"],
      "all_frames": true,
      "js": ["insideIframe-content.js"]
    }
]

background.js:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  console.log(url)
})

insideIframe-content.js:

chrome.runtime.sendMessage(url)

【讨论】:

    【解决方案2】:

    iframe 是浏览器的单独文档,这就是为什么您可以使用 chrome 扩展并将其直接集成到 iframe 中的原因。您只需在 chrome 扩展清单文件中提及 iframe 的 URL。我们将 chrome 扩展应用程序的一些菜单和工具栏集成到 iframe 中。它有效。

    有一篇不错的文章作为 Chrome 扩展程序和 iFrames 的附加信息http://www.natewillard.com/blog/chrome/javascript/iframes/2015/10/20/chrome-communication/

    【讨论】:

      【解决方案3】:

      您需要注入 2 个内容脚本:

      "content_scripts": [
          {
            "matches": ["http://www.example.com/*"],
            "js": ["example.js"]
          },{
            "matches": ["http://www.rapidshare.com/*"],
            "all_frames": true,
            "js": ["rapidshare.js"]
          }
      ]
      

      为了将链接从一个脚本传输到另一个脚本,您需要通过后台页面进行通信:

      rapidshare.js:

      chrome.extension.sendRequest({url: "link"});
      

      background.js:

      chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
          chrome.tabs.sendRequest(sender.tab.id, request);
          sendResponse({});
      });
      

      example.js:

      chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
          console.log("received url:", request.url);
          sendResponse({});
      });
      

      【讨论】:

      • 谢谢 Serg,我现在没有时间测试它,但我会告诉你它是怎么回事!再次感谢
      • 非常感谢您指出可以通过matches属性将不同的脚本加载到不同的页面中!
      • all_frames 是做什么的?
      • all_frames 表示 - 匹配适用于 iFrame 以及顶部帧
      猜你喜欢
      • 2011-11-02
      • 2011-04-25
      • 2012-07-04
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多