我正在创建一个Greasemonkey脚本,并且想要打开一个新的选项卡,该选项卡不会显示URL,而是一些HTML,它们是脚本的一部分。所以基本上我想做这样的事情(这显然不起作用):
window.open('<html><head></head><body></body></html>');
or
GM_openInTab('<html><head></head><body></body></html>');
任何提示都是受欢迎的!
你可以这样做:
var newWindow = window.open();
然后呢
newWindow.document.write("ohai");
如果另一个答案给你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);
假设你有一个本地存储的.html
文件。你能做的是:
var newWindow = window.open();
newWindow.document.location.href = "/path/to/html/file";