Cross Origin Chrome Extension

前端 未结 2 1034
悲哀的现实
悲哀的现实 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.

提交回复
热议问题