Javascript 具有跨域iframe的页面的Greasemonkey脚本

Javascript 具有跨域iframe的页面的Greasemonkey脚本,javascript,cross-domain,greasemonkey,Javascript,Cross Domain,Greasemonkey,我想实现JavaScript,修改从另一个域()加载的iframe中输入字段的内容 困难: 不知何故,jQuery加载有延迟,因此我必须使用setTimeout()来确保jQuery可用 唯一的iframe是从另一个域加载的,当我尝试从中使用iframe.contentWindow.document时,导致访问属性“document”的权限被拒绝 我尝试应用中提供的跨域技术,但不起作用 元素的ID包含分号,所以应该使用advice from 脚本: // ==UserScript== //

我想实现JavaScript,修改从另一个域()加载的
iframe
中输入字段的内容

困难:

  • 不知何故,jQuery加载有延迟,因此我必须使用
    setTimeout()
    来确保jQuery可用
  • 唯一的
    iframe
    是从另一个域加载的,当我尝试从中使用
    iframe.contentWindow.document
    时,导致访问属性“document”的
    权限被拒绝
  • 我尝试应用中提供的跨域技术,但不起作用
  • 元素的ID包含分号,所以应该使用advice from
脚本:

// ==UserScript==
// @name    ah.nl
// @include http://www.ah.nl/over-ah/services/mobiel/*
// @include https://ezpay.liquix.eu/WebReloadSolution/*
// @grant   none
// @namespace   http://www.example.com/gmscripts/
// ==/UserScript==

document.domain= 'ah.nl';
if (self === top) {  
    setTimeout(function() {
            try {
                $('iframe').load(function() {
                    console.log("Start " + this.src);
                    var inputs = $('input', this.contentDocument);
                    console.log("Got " + inputs.length);
                    // Below does not work:
                    //inputs.find('#orderForm\\:msisdnConfirm, #orderForm\\:msisdn').val('1234567890');
                });
            } catch(e) {
                console.log(e);
            }
    }, 1000);
}
产生:

Start https://ezpay.liquix.eu/WebReloadSolution/index.jsp?merchant=ahnl&language=nl&mode=reload&productCode=1
Got 1
之所以有一个元素,是因为在顶层文档中找到了
元素

有谁能提供一个有效的解决方案吗


FF 43.0.1,Greasemonkey v3.8。

由于页面和iframe位于完全不同的域上,因此这种方法不起作用。使用
postMessage
在脚本的两个实例之间进行通信。有关示例,请参见。也许对你的案子来说是个更好的选择。您还可以使用
GM_setValue()
在实例之间进行通信,但在FF+GM中这有点混乱。(Chrome+TM使其更容易)