ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-09 21:38:44  阅读:418  来源: 互联网

标签:userscripts javascript firefox greasemonkey


我有一些使用的用户脚本

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        https://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 Origin Policy违规.
同时,Page范围,控制台范围和Firebug的控制台都可以正常工作.

Greasemonkey范围提供:

SecurityError: The operation is insecure

是否使用@grant none.

加上普遍的无用GM_openInTab(),使我怀疑这是Greasemonkey的错误.我现在没有时间研究它,但是如果您愿意,可以查看file a bug report.

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

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

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

// ==UserScript==
// @name        document.write () test
// @description tests document.write ()
// @include     https://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);
}

标签:userscripts,javascript,firefox,greasemonkey
来源: https://codeday.me/bug/20191009/1882196.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有