我正在尝试从后台脚本打开一个新选项卡 background.js
并让这个新选项卡显示我在后台脚本中获得的一些文本。我正在使用chrome.tabs.create({ url: "template.html" });
使用 template.html
创建新选项卡文件,它只是一个空白的 HTML 模板:
<html lang="en">
<head>
<meta charset="utf-8">
<title>Testing</title>
</head>
<body>
</body>
</html>
在 background.js
,我有一个名为 text
的变量其中包含要添加到新标签页的文本,但我不确定如何附加它。
我认为它可能适用于在新标签页上执行脚本来附加文本,但是当我尝试运行我的脚本 template.js
时上template.html
使用chrome.tabs.executeScript(tab.id, {file: 'template.js'});
,我收到以下错误:
Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-extension://*/template.html". Extension manifest must request permission to access this host.
由于新选项卡的 URL 为 chrome-extensions://*/template.html
扩展程序无法访问它。
我不知道如何将文本或 HTML 附加到选项卡页。任何对此的帮助表示赞赏。谢谢。
最佳答案
您无法在 chrome-extension:
页面上使用 chrome.tabs.executeScript
。唯一有效的方案是 http
https
file
ftp
。
无论如何,你不需要。您只需使用 script 标记将要运行的文件包含在 html 中即可。只需将以下内容添加到 template.html 中:
<script src="template.js"></script>
请注意,在此类扩展页面中,您可以访问完整的 chrome.*
API,因此您可以使用消息传递在此页面和后台页面之间进行通信。
关于javascript - Chrome 扩展程序 : How to append HTML to a newly created chrome tab?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36096648/