简体   繁体   English

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

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

I am writing a Chrome extension that needs to interact with urls that the user does not have open.我正在编写一个 Chrome 扩展程序,它需要与用户没有打开的 url 进行交互。 Therefore I am using hidden iframes that are embedded within the popup, and am attempting to click a button within the iframe.因此,我使用嵌入在弹出窗口中的隐藏 iframe,并尝试单击 iframe 中的按钮。 However, I am receiving a same origin policy error.但是,我收到了同源策略错误。 I know that it is possible for an extension to interact with iframes of a different domain via content scripts when the iframe is on the tab that the user has open, but I am not sure if it is possible to use content scripts to interact with iframes directly in the popup.我知道当 iframe 位于用户打开的选项卡上时,扩展程序可以通过内容脚本与不同域的 iframe 交互,但我不确定是否可以使用内容脚本与 iframe 交互直接在弹出窗口中。

Here is my code:这是我的代码:

manifest.json清单文件

"content_scripts": [


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

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

interaction.js交互.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();
      }
    })
  })
})

Error错误

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.

The error occurs in interaction.js in the third line (with the load callback).错误发生在第三行(带有加载回调)的interaction.js 中。 Does anyone know any changes I should make to the content script to allow me to interact with the iframe?有谁知道我应该对内容脚本进行任何更改以允许我与 iframe 交互? Or if there are other approaches I should take?或者如果我应该采取其他方法? Thanks!谢谢!

Your frame will also have a content script injected.您的框架还将注入一个内容脚本。

You need to communicate with that content script using Messaging and make it do what you need.您需要使用 Messaging 与该内容脚本进行通信并使其执行您需要的操作。

Here's some further information:以下是一些进一步的信息:

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

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