javascript - 新打开的窗口显示我的动态内容,但位置错误

标签 javascript url greasemonkey window.open

我的脚本打开一个新窗口,然后写入它的内容。目标窗口按预期显示“Hello World”,但 URL 与运行脚本的窗口相同,我不明白为什么。

有没有办法在不选择旧 URL 的情况下构建新窗口?

function doTest() {
    var impl = document.implementation;
    var tempDoc = impl.createHTMLDocument("Hello");
    var tempBody = tempDoc.getElementsByTagName("body")[0];
    var tempDiv = tempDoc.createElement('div');
    var tempMsg = tempDoc.createTextNode('Hello World.');
    tempDiv.appendChild(tempMsg);
    tempBody.appendChild(tempDiv);

    var destWindow=window.open("about:blank", "title", null, false);
    var destDoc=destWindow.document;
    destDoc.open();
    destDoc.write("<html>"+tempDoc.documentElement.innerHTML+"</html>");    
    destDoc.close();
}


var btnTest = document.createElement( 'input' );
btnTest.setAttribute( 'value', 'Test' );
btnTest.setAttribute( 'type', 'button' );
btnTest.addEventListener('click',doTest,true);
document.body.appendChild( btnTest );


我使用的是 Firefox 20 和 Greasemonkey 1.8。

最佳答案

我不认为你能做到这一点。这可能是一项安全功能;见Bug 337344, "Disable location bar hiding by default, to make chrome spoofing harder"和相关的错误。

我认为一旦父窗口 javascript 尝试更改内容,反欺骗保护就会将位置强制为开启者的 URL。即使您使用 about:blank 以外的 URL 打开窗口也是如此。

您可以通过在 about:config 中将设置 dom.disable_window_open_feature.location 设置为 true 来隐藏地址栏,然后通过传递 location 来隐藏地址栏=falsewindow.open 中。
但如果您这样做,那么打开器的 URL 将被添加到标题栏之前。

您可能不得不忍受这一点,并庆幸网络钓鱼变得更加困难。 ;-) 或者,如果您可以提出令人信服的理由来说明为什么您应该能够更改或留空该位置:(1) 将其添加到您的问题中,以及 (2) 使用 Mozilla 提出错误或功能请求。


解决方法:
您可以将脚本设置为也在 about:blank 上触发,然后让 GM 从空白实例创建页面,而不是打开实例。参见 this answer对于几乎相同的场景/解决方案。

一定要用明确的about:blank打开窗口,像这样:

var destWindow = window.open("about:blank", "title", null);

关于javascript - 新打开的窗口显示我的动态内容,但位置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380534/

相关文章:

javascript - i 没有在 while 循环中定义

javascript - 将参数传递给 'document.getElementById'

javascript - Greasemonkey 1.0 中的 jQuery 与使用 jQuery 的网站发生冲突

javascript - 如何强制网站使用最新版本的 CKEditor.js 和用户脚本?

javascript - Greasemonkey 脚本在使用 AJAX 提交时将文本附加到表单?

Javascript:如何提取样式中动态变化的数字的值?

javascript - 在 javascript/jquery 中单击按钮时如何选择按钮 ID

java - 如何从 xml 文件中获取字符串形式的 URL?

url - 这个 steam 桌面快捷方式 URL steam ://mean 是什么意思

python - 用 Flask 重写 URL