简体   繁体   English

从Chrome扩展程序访问iframe中的DOM元素

[英]Access DOM elements inside iframe from chrome extension

Is there a way to acces DOM elements inside an iframe without getting a cross-origin error ? 有没有办法在iframe中访问DOM元素而不会出现跨域错误?
Basically I want to be able to click a button located inside an iframe to launch a video, from my extension. 基本上,我希望能够点击扩展程序中位于iframe内的按钮来启动视频。

I really don't know how to proceed, I have tried iframe.contentDocument but I get the cross-origin error. 我真的不知道该如何进行,我尝试了iframe.contentDocument但收到了跨域错误。

background.js: background.js:

chrome.tabs.executeScript({
    "file": "script.js",
    "allFrames" : true
});

script.js : script.js

var iframe = document.getElementById("iframeId");
var button = iframe.contentDocument.getElementsById("buttonId");

The error I get : 我得到的错误:
Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "https://website.com" from accessing a cross-origin frame.

Thank you 谢谢

Brute force: inject a script in the iframe 蛮力:将脚本插入iframe

If you are having trouble accessing the contents of an iframe, the brute force method to do so is to inject a content script directly into the iframe and access the iframe from that content script. 如果您在访问iframe的内容时遇到问题,则蛮力方法是将内容脚本直接注入iframe并从该内容脚本访问iframe。

Inject into a single frame: 注入一个框架:
You can explicitly specify the frame into which your script is injected by providing the frame ID in your call to chrome.tabs.executeScript() . 您可以通过在调用chrome.tabs.executeScript()提供帧ID来显式指定将脚本注入的帧。 This could be something like: 可能是这样的:

chrome.tabs.executeScript(tabOfInterestId,{
    frameId: frameIdToInject,
    file: scriptFileWhichDoesWhatIWantInTheIframe.js
},function(results){
    //Handle any results
});

If you don't know the frame ID for the frame into which you desire to inject, you can obtain it from chrome.webNavigation.getAllFrames() like: 如果您不知道要注入的帧的帧ID,则可以从chrome.webNavigation.getAllFrames()获取它,例如:

chrome.webNavigation.getAllFrames({tabId:tabOfInterestId},function(frames){
    //Do what you want with the array of frame descriptor Objects
});

Injecting into all frames: 注入所有帧:
If you want to inject into all frames in a tab, you can do what you show in the Question that you are already doing: 如果要注入选项卡中的所有框架,则可以执行已在“问题”中显示的操作:

chrome.tabs.executeScript({
    "file": "script.js",
    "allFrames" : true
});

This will inject the script.js file into all frames within the active tab of the current window which exist at the time the tabs.executeScript() call is executed . 这会将script.js文件注入到当前窗口的活动选项卡中所有在执行tabs.executeScript()调用时存在的tabs.executeScript() A different copy of script.js will be injected in each frame within the tab, including the base frame. 将在选项卡的每个框架(包括基础框架)中注入不同的script.js副本。 This will not inject script.js in any iframes which are added to the tab after this call to tabs.executeScript() is made. 在调用tabs.executeScript()之后,这不会在添加到选项卡的任何iframe中注入script.js For iframes, the context in which the injected script exists will be valid (ie the script will exist) until the URL for the iframe changes (eg the src attribute changes), or a parent frame (eg the base frame of the tab) changes URL (ie a new page is loaded in a parent frame). 对于iframe,注入的脚本所在的上下文将一直有效(即脚本将存在),直到iframe的URL更改(例如src属性更改)或父框架(例如选项卡的基础框架)更改为止URL(即在父框架中加载了新页面)。

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

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