打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
javascript-document.write()在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        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);}
来源:https://www.icode9.com/content-1-497701.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
!!!UserScript(用户脚本)在Firefox、Google Chrome、Opera、IE6
火狐———Stylish样式列表
JavaScript动态加载script方式引用百度地图API,Uncaught ReferenceError: BMap is not defined
如何在firefox浏览器里面得到选中的文本
100个直接可以拿来用的JavaScript实用功能代码片段
JavaScript常用的代码片段 | Poetry''''s Blog
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服