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

问题描述 投票:23回答:3

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

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

任何提示都是受欢迎的!

javascript jquery greasemonkey
3个回答
53
投票

你可以这样做:

var newWindow = window.open();

然后呢

newWindow.document.write("ohai");


9
投票

如果另一个答案给你Error: Permission denied to access property "document",请参阅this question关于如何处理同源政策问题,或this one

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

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

-2
投票

假设你有一个本地存储的.html文件。你能做的是:

var newWindow = window.open();
newWindow.document.location.href = "/path/to/html/file";
© www.soinside.com 2019 - 2024. All rights reserved.