我有一些使用的用户脚本
var tab = window.open('', '_blank');
tab.document.write(myCustomHtml);
tab.document.close();
向用户显示输出(myCustomHtml 是我之前在代码中定义的一些有效的 HTML)。从版本 27 开始,它在 Firefox 中的工作就变得很糟糕,现在我只得到一个空文档。没有任何控制台错误。
使用 Firefox 控制台检查时,新打开的文档只有此内容
<html>
<head></head>
<body>
</body>
</html>
而源代码是空的。
该代码可以在 Chrome 中运行。
我需要对较新的 Firefox 版本 (27+) 和更新的 Greasemonkey (1.15) 进行任何修改吗?我还没有发现任何最近向 Firefox 报告的有关此问题的错误。
这是一个测试脚本
// ==UserScript==
// @name document.write() test
// @namespace stackoverflow.com
// @description tests document.write()
// @include https://stackoverflow.com/questions/22651334/*
// @include http://stackoverflow.com/questions/22651334/*
// @version 0.0.1
// ==/UserScript==
var tab = window.open('', '_blank');
tab.document.write('<html><head></head><body><ul><li>a</li><li>b</li><li>c</li></ul></body></html>');
tab.document.close();
我不确定 Greasemonkey 或 Firefox 是否已经犯了这个错误,但是window.open
从 Greasemonkey 脚本到空白页现在会触发同源政策违反。
同时,页面范围、控制台范围和 Firebug 的控制台都工作正常。
Greasemonkey 范围给出:
SecurityError:操作不安全
whether @grant none
使用与否。
这,加上一般无用的GM_openInTab(),让我怀疑这是一个 Greasemonkey bug。我现在没有时间研究它,但是提交错误报告, 如果你希望。
为了使其在最新版本的 Firefox (28.0) 和 Greasemonkey (1.15) 上运行,我必须执行以下操作:
- 告诉我的弹出窗口拦截器(暂时)允许来自 stackoverflow.com 的弹出窗口。
- 将弹出代码注入页面范围。
- 使用显式的
about:blank
为网址。
- 等待新窗口加载。
这是一个适用于最新 FF+GM 的完整脚本releases:
// ==UserScript==
// @name document.write () test
// @description tests document.write ()
// @include http://stackoverflow.com/questions/22651334/*
// ==/UserScript==
function fireNewTab () {
var newTab = window.open ('about:blank', '_blank');
newTab.addEventListener (
"load",
function () {
//--- Now process the popup/tab, as desired.
var destDoc = newTab.document;
destDoc.open ();
destDoc.write ('<html><head></head><body><ul><li>a</li><li>b</li><li>c</li></ul></body></html>');
destDoc.close ();
},
false
);
}
addJS_Node (null, null, fireNewTab);
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
var D = document;
var scriptNode = D.createElement ('script');
if (runOnLoad) {
scriptNode.addEventListener ("load", runOnLoad, false);
}
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)