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