[英]Chrome extension: How to append HTML to a newly created chrome tab?
I am trying to open a new tab from the background script background.js
and have this new tab display some text I've obtained in the background script. 我正在尝试从后台脚本
background.js
打开一个新选项卡,并使该新选项卡显示一些我在后台脚本中获得的文本。 I am using chrome.tabs.create({ url: "template.html" });
我正在使用
chrome.tabs.create({ url: "template.html" });
to create the new tab using the template.html
file, which is just a blank HTML template: 使用
template.html
文件创建新标签,该文件只是一个空白的HTML模板:
<html lang="en">
<head>
<meta charset="utf-8">
<title>Testing</title>
</head>
<body>
</body>
</html>
In background.js
, I have a variable called text
which contains the text to add to the new tab page, but I'm not sure how to append it. 在
background.js
,我有一个名为text
的变量,其中包含要添加到新标签页的文本,但是我不确定如何附加它。
I thought it might work on execute a script on the new tab page to append the text, however when I try to run my script template.js
on template.html
using chrome.tabs.executeScript(tab.id, {file: 'template.js'});
我想这可能工作在新标签页上执行脚本的文字追加,但是当我尝试运行我的脚本
template.js
上template.html
使用chrome.tabs.executeScript(tab.id, {file: 'template.js'});
, I get the following error: ,出现以下错误:
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.
Since the new tab has the URL chrome-extensions://*/template.html
which is inaccessible to the extension. 由于新标签具有URL
chrome-extensions://*/template.html
,因此扩展名无法访问。
I am not sure how else to append text or HTML to the tab page. 我不确定如何在标签页上附加文本或HTML。 Any help on this appreciated.
任何帮助对此表示赞赏。 Thank you.
谢谢。
You can't use chrome.tabs.executeScript
on a chrome-extension:
page. 您无法在
chrome-extension:
页面上使用chrome.tabs.executeScript
。 The only valid schemes are http
https
file
ftp
. 唯一有效的方案是
http
https
file
ftp
。
Anyway, you don't need to. 无论如何,您不需要。 You can simply include the file you want to run in your html with a script tag.
您可以简单地在脚本中包含要在html中运行的文件。 Just add the following to template.html:
只需将以下内容添加到template.html:
<script src="template.js"></script>
Note that in extension pages such as this you have access to the full chrome.*
APIs, so for instance you can use messaging to communicate between this page and the background page. 请注意,在诸如此类的扩展页面中,您可以访问完整的
chrome.*
API,因此,例如,您可以使用消息传递在此页面和背景页面之间进行通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.