繁体   English   中英

Chrome 扩展:与嵌入在弹出窗口中的 iframe 交互

[英]Chrome Extension: Interacting with iframe embedded within popup

我正在编写一个 Chrome 扩展程序,它需要与用户没有打开的 url 进行交互。 因此,我使用嵌入在弹出窗口中的隐藏 iframe,并尝试单击 iframe 中的按钮。 但是,我收到了同源策略错误。 我知道当 iframe 位于用户打开的选项卡上时,扩展程序可以通过内容脚本与不同域的 iframe 交互,但我不确定是否可以使用内容脚本与 iframe 交互直接在弹出窗口中。

这是我的代码:

清单文件

"content_scripts": [


{
    "js": [ "bin/jquery.min.js", "interaction.js" ],
    "all_frames": true,
    "run_at": "document_start",
    "matches": [ "http://*/*",
                 "https://*/*" ]
  }],

  "permissions": [
    "activeTab",
    "tabs",
    "http://*/",
    "https://*/"
],

交互.js

$(document).ready(function() {
  $('div#iframes').append("<iframe id='shop' src='https://www.google.com/'></iframe>")
  $('iframe').bind("load", function() {
    $('iframe').contents().find("html").ready(function() {
      loadedStores += 1;
      if (loadedStores == carts.totalStores) {
        $('div#cost').append(carts.grandTotal)
        showMain();
      }
    })
  })
})

错误

Uncaught SecurityError: Blocked a frame with origin "chrome-extension://mapgjiofchdchalgcifmdolgcekfaadp" from accessing a frame with    origin "https://www.google.com/".  The frame requesting access has a protocol of "chrome-extension", the frame being accessed has a protocol of "http". Protocols must match.

错误发生在第三行(带有加载回调)的interaction.js 中。 有谁知道我应该对内容脚本进行任何更改以允许我与 iframe 交互? 或者如果我应该采取其他方法? 谢谢!

您的框架还将注入一个内容脚本。

您需要使用 Messaging 与该内容脚本进行通信并使其执行您需要的操作。

以下是一些进一步的信息:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM