简体   繁体   English

Chrome扩展程序使内容脚本能够访问iframe

[英]Chrome extension enable content script to access iframe

Question is simple: Is there any way to enable a content script (through executeScript() ) to be able to access an iframe on the current webpage (say stackoverflow.com ) that has the same origin of the extension itself? 问题很简单:是否有任何方法可以使内容脚本(通过executeScript() )能够访问与扩展本身具有相同来源的当前网页上的iframe(例如stackoverflow.com )?

To put to rest: I realize you can communicate through postMessage, hashing, and so forth from the iframe script to its parent script, but my main goal is to add events onto the iframe directly from a content script rather than having to pass a message (and create a middle man). 休息一下:我知道您可以通过postMessage,哈希等从iframe脚本传递到其父脚本进行通信,但是我的主要目标是直接从内容脚本向iframe添加事件,而不必传递消息(并创建一个中间人)。

I believe injected content scripts run on the page "in their own little world", in which the page itself, nor other content scripts can access, so it leads me to believe Chrome could possibly allow these scripts access to iframes of its own origin (the extension url itself). 我相信注入的内容脚本在页面上“在他们自己的小世界中”运行,页面本身也无法访问其他内容脚本,因此,我相信Chrome可以allow这些脚本访问其自身的iframe(扩展网址本身)。

EDIT 编辑

Just to clarify, the iframe would have a url of the chrome extension itself, under the protocol chrome-extension:// . 为了澄清起见,iframe在协议chrome-extension://下将具有chrome扩展本身的网址。 The parent page of the iframe could have any url, say http://stackoverflow.com for instance. iframe的父页面可以有任何网址,例如http://stackoverflow.com So trying to access the iframe from a content script generally wont pass the same-origin-policy ...The question is if there is a way around this using Chrome's Extension library. 因此,尝试从内容脚本访问iframe通常不会通过same-origin-policy ...问题是,是否有办法使用Chrome的扩展库解决此问题。

Thanks! 谢谢!

You can certainly run a content-script on a page and interact with any embedded iframes. 您当然可以在页面上运行内容脚本,并与任何嵌入式iframe进行交互。 I was working on a project recently where I needed to do this. 我最近在一个需要执行此操作的项目上工作。

I don't have a solution in pure javascript so this would require you to include the jQuery.js library, if that works for you. 我没有纯JavaScript的解决方案,因此这需要您包括jQuery.js库(如果适用)。

In the content-script running on the parent page I did the following: 在父页面上运行的内容脚本中,我执行了以下操作:

//CAPTURE FRAME LOAD
$("#frameID").load(function () {

    //Create an object for the frame
    var firstFrame = window.parent.frames[0].document;

    //Set JQuery events to run in the context of the frame
    $("#buttonID", firstFrame).click(function(){

        //Capture click event of a button within the frame

    });


});

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

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