【发布时间】:2026-01-04 05:40:01
【问题描述】:
我有一些使用的用户脚本
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 *.com
// @description tests document.write()
// @include https://*.com/questions/22651334/*
// @include http://*.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();
【问题讨论】:
-
document.write()在渲染过程中会产生各种不想要的效果。最好将元素直接添加到 DOM。 -
我知道这是一个讨厌的功能,我也知道风险,但它让我省去了很多行。考虑到我从一个空文档开始,使用“离线”数据和非常简单的布局,我宁愿保留它。
-
我已经在 firefox 28 中使用简单的 html 标记
"<html><head></head><body> <ul><li>a</li><li>b</li><li>c</li></ul> </body></html>"测试了您的脚本,并且对我有用,也许它与您尝试使用的 html 有关....等等呢你的greasemonkey中的配置?:我已经在一个纯html网页中测试了它。 -
抱歉,现在在greasemonkey中测试...同样的问题。
-
谢谢。我在问题中添加了一个测试脚本。
标签: javascript firefox greasemonkey userscripts