Cross Origin Chrome Extension

前端 未结 2 1037
悲哀的现实
悲哀的现实 2021-02-16 00:22

I have been reading and playing around with Chrome Extensions for the last week or so but I\'m having trouble trying to achieve what I want. What I am trying to create is an Ext

相关标签:
2条回答
  • 2021-02-16 00:29

    There seems to be no way to circumvent the Same-origin policy for extension pages. See https://bugs.chromium.org/p/chromium/issues/detail?id=344341.

    You can achieve your objective by injecting a content script into the iframe on your background page and accessing and manipulating the iframe DOM using the content script.

    A trivial example:

    Add the following to your manifest.json:

    "content_scripts": [
    {
      "matches": ["http://www.myserver.com/iframe/CO-TEST-FRAME.html"],
      "js": ["contentScript.js"],
            "all_frames": true
    }
    


    contentScript.js:

    console.log("Content script injected.");
    var test = document.getElementById("desired").innerHTML;
    console.log("Text from " + document.URL + ": " + test);
    

    Note that there is no window.onload in the content script. Content scripts are injected after the DOM has loaded be default, so the window.onload event would not trigger.

    In case some communcation between the background page and the content script is needed, you will need to employ message passing. There are many questions related to that on SO.

    0 讨论(0)
  • 2021-02-16 00:36

    You need to explicitly list all the URLs under permissions in manifest.js Trying to use anything which is not listed would cause a Cross origin error.

    you can list the url under permissions section and that should work.

    "permissions": [
            "webRequest",
            "storage",
            "https://mail.google.com/*",
            "http://myserver.com/*",
            "https://blah.blah.com/*"
        ],
    

    Hope that helps

    0 讨论(0)
提交回复
热议问题