我的脚本打开一个新窗口,然后写入它的内容。目标窗口按预期显示“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 来隐藏地址栏=false
在 window.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/