document.write()不能在Firefox的userscript中工作 [英] document.write() not working in userscript with Firefox
问题描述
我有一些使用脚本的脚本,它们使用
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)进行任何修改吗?
这是一个测试脚本
// == UserScript ==
// @name document.write()test
// @namespace stackoverflow.com
// @description测试文档.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> <李>℃下/立GT;< / UL>< /体>< / HTML>');
tab.document.close();
我不确定Greasemonkey或Firefox是否有错误这个,但是 window.open
从一个Greasemonkey脚本到一个空白页面,现在触发一个同源策略违规。
同时,页面范围,控制台范围和Firebug控制台都正常工作。
Greasemonkey作用域给出:
$ b
SecurityError:操作不安全
>
是否使用 @grant none
这加上 GM_openInTab()的一般无用性,让我怀疑它是一个Greasemonkey错误。我现在没有时间查看它,但是如果你有提交错误报告为了得到这个最新版本的Firefox(28.0)和Greasemonkey(1.15)的功能,下面是我必须做的:
- 告诉我的弹出窗口阻止程序(暂时)允许来自stackoverflow.com的弹出窗口。
- 将弹出代码注入页面范围。
- 使用显式的
about:blank
作为URL。 - 等待新窗口加载。
这是一个完整的脚本,适用于最新的FF + GM版本 p>
// == UserScript ==
// @name document.write()test
// @描述测试document.write()
// @include http://stackoverflow.com/questions/22651334/*
// == / UserScript ==
function fireNewTab (){
var newTab = window.open('about:blank','_bl ANK);
newTab.addEventListener(
load,
function(){
// ---现在按需要处理popup / tab
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);
}
I have some userscripts that use
var tab = window.open('', '_blank');
tab.document.write(myCustomHtml);
tab.document.close();
to show an output to the user (myCustomHtml being some valid HTML I defined previously in the code). It sopped working in Firefox since version 27, now I only get an empty document. No console errors whatsoever.
The newly opened document has only this content when inspected with Firefox' console
<html>
<head></head>
<body>
</body>
</html>
while the source code is empty.
The code works in Chrome.
Do I need to make any modification for newer Firefox versions (27+) and an updated Greasemonkey (1.15)? I haven't found any recent bug reported to Firefox about this issue.
Here's a test script
// ==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();
I'm not sure if Greasemonkey or Firefox has buggered this, but window.open
to a blank page, from a Greasemonkey script, now triggers a Same Origin Policy violation.
Meanwhile, Page scope, the console scope, and Firebug's console all work fine.
Greasemonkey scope gives:
SecurityError: The operation is insecure
whether @grant none
is used or not.
This, plus the general uselessness of GM_openInTab(), lead me to suspect it is a Greasemonkey bug. I don't have time to look into it right now, but file a bug report, if you wish.
To get this to work on the latest release of Firefox (28.0) and Greasemonkey (1.15), here's what I had to do:
- Tell my popup blockers to (temporarily) allow popups from stackoverflow.com.
- Inject the popup code into the page scope.
- Use an explicit
about:blank
for the URL. - Wait for the new window to load.
Here's a complete script that works on the latest 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);
}
这篇关于document.write()不能在Firefox的userscript中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!