简体   繁体   English

使用PostMessages跨域iframe访问-访问被拒绝

[英]Cross domain iframe access using postmessage - access denied

I'm using greasemonkey to try and automate filling in data. 我正在使用lublessmonkey尝试自动填充数据。 On the page where I want data pulled from I append a button to the document. 在我要从中提取数据的页面上,我向文档添加了一个按钮。 This button creates a frame containing the website that I want to push the data to. 此按钮创建一个框架,其中包含我要将数据推送到的网站。

From what I can see on SO and elsewhere, I think i'm supposed to be able to send information to an iframe by using postmessage. 从SO和其他地方的角度来看,我认为我应该能够通过使用postmessage将信息发送到iframe。 To do it i need to get the contentWindow for that frame. 为此,我需要获取该框架的contentWindow。

Now, when I create the frame, I can get access to the frame.contentWindow. 现在,当我创建框架时,可以访问frame.contentWindow。 If I attempt to get the contentWindow from within another function using document.getElementById("frameid").contentWindow I get "access denied". 如果尝试使用document.getElementById(“ frameid”)。contentWindow从另一个函数中获取contentWindow,则会出现“访问被拒绝”的情况。

I've also stored the window reference in a global variable without problem when creating the iframe, but again if I try to access that variable from another function I get the same "access denied" error. 创建iframe时,我也将窗口引用存储在全局变量中,没有问题,但是再次尝试从另一个函数访问该变量时,会出现相同的“拒绝访问”错误。

I'm very new to all this so maybe I'm just doing something stupid, but can anyone clarify if this behaviour is normal? 我对这一切都很陌生,所以也许我只是在做一些愚蠢的事情,但是没有人能澄清这种现象是否正常吗? If it is normal then how can I use postmessage to send information to that iframe if I'm not able to get the contentWindow when I need it? 如果正常,那么在需要时无法获取contentWindow的情况下,如何使用后消息向该iframe发送信息? :s :■

Any help greatly appreciated. 任何帮助,不胜感激。 Thanks :) 谢谢 :)

Last week, I had exactly same problem and I fixed it regarding related Mozilla's Developer Document. 上周,我遇到了完全相同的问题,并修复了有关Mozilla开发人员文档的问题。 https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

And here is an example: https://gist.github.com/nesimtunc/cbfee61071ab46459b5c 这是一个示例: https : //gist.github.com/nesimtunc/cbfee61071ab46459b5c

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

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