Javascript 具有跨域iframe的页面的Greasemonkey脚本
我想实现JavaScript,修改从另一个域()加载的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== //
iframe
中输入字段的内容
困难:
- 不知何故,jQuery加载有延迟,因此我必须使用
来确保jQuery可用setTimeout()
- 唯一的
是从另一个域加载的,当我尝试从中使用iframe
时,导致访问属性“document”的iframe.contentWindow.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使其更容易)