繁体   English   中英

document.write()在Firefox的用户脚本中不起作用

[英]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)进行任何修改吗? 我没有发现任何有关此问题的最新错误报告给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是否对此进行了错误诊断,但是从Greasemonkey脚本将window.open打开到空白页现在会触发Same同源策略冲突。
同时,Page范围,控制台范围和Firebug的控制台都可以正常工作。

Greasemonkey范围提供:

SecurityError:操作不安全

是否使用@grant none

这加上GM_openInTab()的普遍无用,使我怀疑这是一个Greasemonkey错误。 我现在没有时间研究它,但可以根据需要提交错误报告

要使其在最新版本的Firefox(28.0)和Greasemonkey(1.15)上起作用,这是我必须要做的:

  1. 告诉我的弹出窗口阻止程序(临时)允许来自stackoverflow.com的弹出窗口。
  2. 将弹出代码插入页面范围。
  3. 使用明确的about:blank作为URL。
  4. 等待新窗口加载。

这是适用于最新FF + GM 版本的完整脚本:

// ==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);
}

尝试使用tab.document.body.innerHTML而不是tab.document.write()tab.document.close() ,即,

var tab = window.open('', '_blank');
tab.document.body.innerHTML = '<ul><li>a</li><li>b</li><li>c</li></ul>';

(我使用的是带有Greasemonkey v1.15的Firefox v24.4.0,这对我有用。)

我不知道此问题的根本原因,但是当我在try-catch块中包含document.write()时,我从alert()得到了以下错误消息。

The operation is insecure.

可能相关: 错误663406-document.write在devtools暂存器和控制台中不起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM