Inject javascript in an iframe using chrome extension

后端 未结 1 1334
-上瘾入骨i
-上瘾入骨i 2020-12-04 22:22

There are lots of questions/answers on injecting javascript into the document page. What I really want to do is inject javascript in an iframe within the document.

T

相关标签:
1条回答
  • 2020-12-04 22:36

    Yes, you can use content_scripts property in manifest.json like this:

    "content_scripts": [{
        "matches": ["http://*/*", "https://*/*"],
        "js": ["content_frame.js"],
        "all_frames": true
    }],
    

    By setting all_frames to true you will inject/include content_frame.js javascript file into top document and all iframes. In case you just need javascript injected in iframe but not in top document you can check inside your content_frame.js file like this:

    if (parent === top) {
        // here you can put your code that will run only inside iframe
    }
    
    0 讨论(0)
提交回复
热议问题