javascript - Chrome 扩展程序 : How to append HTML to a newly created chrome tab?

标签 javascript html google-chrome google-chrome-extension content-script

我正在尝试从后台脚本打开一个新选项卡 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/

相关文章:

api - 开发谷歌浏览器插件(没有扩展!)

google-chrome - Google Chrome的默认打开位置和大小

javascript - DocumentFragments 的宽度和高度

javascript - 不允许输入中的值变为 =< 0

javascript - 测试手动引导的 Angularjs 应用程序

javascript - Chatango 嵌入代码的 W3C 验证器错误

jquery - Chrome 打印预览失败,带有 jQ​​uery 打印按钮

javascript - 来自 php 文件的 xampp 脚本不会转移到 html 文件

javascript - 在 iFrame 中重新加载同一站点的路由

javascript - 使用用户选择的 xml 文件填写表单