2014-03-26 92 views
1

我有一些userscripts使用document.write()的不userscript工作与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(); 
+1

'document.write()的'可以在渲染过程中有各种各样的不良影响。最好将你的元素直接添加到DOM。 – 2014-03-26 04:16:20

+0

我知道这是一个讨厌的功能,我意识到风险,但它让我省了很多线。我宁愿保留它,考虑到我从一个空白文档开始,使用“离线”数据和一个非常简单的布局。 – berbt

+0

我有一个简单的HTML标记'“

  • 一个
  • b
  • Ç
”'测试你的脚本在Firefox 28和工作对我来说,也许这是一些涉及到HTML你”重新尝试使用....等一下你的greasemonkey中的配置?:我已经在纯html网页上测试过了。 – Allende

回答

2

我不知道如果Greasemonkey的或Firefox已经鸡奸这一点,但window.open到一个空白页,从Greasemonkey的脚本,现在触发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)最新发布的工作,这是我必须做的:

  1. 告诉我的弹出窗口拦截到(临时)允许计算器弹出窗口。 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); 
} 
+0

谢谢,特别是对于功能完整的代码片段。我认为这是“我是否需要对新版Firefox进行任何修改?”的结论性答案。我会为Greasemonkey的人写一张票。 – berbt

-1

尝试使用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>'; 

(我使用的是Firefox v24.4.0与Greasemonkey的V1.15和适用于我)

我不知道这个问题的根本原因,但是当我把document.write()封入try-ca tch块我从alert()得到以下错误信息。

The operation is insecure.

可能相关的:Bug 663406 - document.write does not work in devtools scratchpad and console

+0

这个bug并不是太相关,无论如何它已经在大约3个版本之前解决了。 –

相关问题