使用自定义HTML而不是URL打开新选项卡

时间:2022-09-22 18:14:01

I'm making a Greasemonkey script and would like to open a new tab which will not display a URL but some HTML that is part of the script. So basically I want to do something like this (which is obviously not working):

我正在创建一个Greasemonkey脚本,并且想要打开一个新的选项卡,该选项卡不会显示URL,而是一些HTML,它们是脚本的一部分。所以基本上我想做这样的事情(这显然不起作用):

window.open('<html><head></head><body></body></html>');
or
GM_openInTab('<html><head></head><body></body></html>');

Any hints are welcome!

任何提示都是受欢迎的!

2 个解决方案

#1


40  

You can do this:

你可以这样做:

var newWindow = window.open();

var newWindow = window.open();

and then do

然后呢

newWindow.document.write("ohai");

newWindow.document.write( “ohai”);

#2


6  

If the other answer gives you Error: Permission denied to access property "document", see this question about how to handle same-origin policy problems, or this one.

如果另一个答案为您提供错误:拒绝访问属性“文档”的权限,请参阅此问题,了解如何处理同源策略问题或此问题。

Or, quick and dirty, use a data URI:

或者,快速和脏,使用数据URI:

var html = '<html><head></head><body>ohai</body></html>';
var uri = "data:text/html," + encodeURIComponent(html);
var newWindow = window.open(uri);

#1


40  

You can do this:

你可以这样做:

var newWindow = window.open();

var newWindow = window.open();

and then do

然后呢

newWindow.document.write("ohai");

newWindow.document.write( “ohai”);

#2


6  

If the other answer gives you Error: Permission denied to access property "document", see this question about how to handle same-origin policy problems, or this one.

如果另一个答案为您提供错误:拒绝访问属性“文档”的权限,请参阅此问题,了解如何处理同源策略问题或此问题。

Or, quick and dirty, use a data URI:

或者,快速和脏,使用数据URI:

var html = '<html><head></head><body>ohai</body></html>';
var uri = "data:text/html," + encodeURIComponent(html);
var newWindow = window.open(uri);