简体   繁体   中英

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. Therefore I am using hidden iframes that are embedded within the popup, and am attempting to click a button within the 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.

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

$(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). Does anyone know any changes I should make to the content script to allow me to interact with the 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.

Here's some further information:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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