[英]Why does my Chrome Extension code not open a new tab?
I'm trying to make an extension that makes a new tab open to a specific URL upon a new page being loaded. 我正在尝试进行扩展,以便在加载新页面时使新标签页打开到特定的URL。 I figured I should start by learning how to create a new tab at all. 我认为我应该首先学习如何创建新标签。 My code is below. 我的代码如下。
manifest.json 的manifest.json
{
"manifest_version": 2,
"name": "name",
"description": "desctiption",
"version": "1.0",
"permissions": [
"tabs",
"<all_urls>"
],
"browser_action": {
"default_icon": "icon.png"
},
"background" : {
"page" : "background.html",
"persistent" : false
}
}
//something.js
function createTab() {
chrome.tabs.create({url: "http://www.stackoverflow.com"});
}
<!--background.html-->
<html>
<head>
<body>
<script src="something.js"></script>
<a href="#" onclick="createTab();">Create a new tab</a>
</body>
</head>
</html>
The inline function createTab()
is never executed since JavaScript and HTML must be in separate files and inline statements are not allowed. 内联函数createTab()
永远不会执行,因为JavaScript和HTML必须位于单独的文件中,并且不允许内联语句。
Instead, bind to the event within your JS file and move your tag below the element. 而是绑定到JS文件中的事件,然后将标记移动到元素下方。
For example, HTML: 例如,HTML:
<body>
<a id="myHREF">Create a new tab</a>
<script src="popup.js"></script>
</body>
And your javascript: 和你的JavaScript:
function createTab() {
chrome.tabs.create({
url: "http://www.stackoverflow.com"
});
}
document.getElementById("myHREF").onclick = createTab;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.