我有一些使用的用戶(hù)腳本
var tab = window.open('', '_blank');
tab.document.write(myCustomHtml);
tab.document.close();
向用戶(hù)顯示輸出(myCustomHtml是我之前在代碼中定義的一些有效HTML).自版本27起,它在Firefox中停止工作,現(xiàn)在我只得到一個(gè)空文檔.沒(méi)有任何控制臺(tái)錯(cuò)誤.
使用Firefox的控制臺(tái)檢查時(shí),新打開(kāi)的文檔僅具有此內(nèi)容
<html>
<head></head>
<body>
</body>
</html>
源代碼為空.
該代碼可在Chrome中運(yùn)行.
我需要對(duì)較新的Firefox版本(27)和更新的Greasemonkey(1.15)進(jìn)行任何修改嗎?我沒(méi)有發(fā)現(xiàn)任何有關(guān)此問(wèn)題的最新錯(cuò)誤報(bào)告給Firefox.
這是一個(gè)測(cè)試腳本
// ==UserScript==
// @name document.write() test
// @namespace
// @description tests document.write()
// @include https:///questions/22651334/*
// @include https:///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是否對(duì)此進(jìn)行了錯(cuò)誤診斷,但是從Greasemonkey腳本將window.open打開(kāi)到空白頁(yè)現(xiàn)在會(huì)觸發(fā)Same Origin Policy違規(guī). 同時(shí),Page范圍,控制臺(tái)范圍和Firebug的控制臺(tái)都可以正常工作.
Greasemonkey范圍提供:
SecurityError: The operation is insecure
是否使用@grant none.
加上普遍的無(wú)用GM_openInTab(),使我懷疑這是Greasemonkey的錯(cuò)誤.我現(xiàn)在沒(méi)有時(shí)間研究它,但是如果您愿意,可以查看file a bug report.
要使其在最新版本的Firefox(28.0)和Greasemonkey(1.15)上起作用,這是我必須要做的:
>告訴我的彈出窗口阻止程序(臨時(shí))允許來(lái)自的彈出窗口. >將彈出代碼插入頁(yè)面范圍. >使用明確的about:blank作為網(wǎng)址. >等待新窗口加載.
這是適用于最新FF GM版本的完整腳本:
// ==UserScript==
// @name document.write () test
// @description tests document.write ()
// @include https:///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);
}
來(lái)源:https://www./content-1-497701.html
|