我制作了Chrome扩展程序,用于收集链接并在点击它们时在新标签页中打开它们。 我做了第一部分收集所有当前的URL,这是popup.js:
function GetUrls()
{
var fourmTabs = new Array();
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
fourmTabs[i] = tabs[i];
}
for (var i = 0; i < fourmTabs.length; i++) {
if (fourmTabs[i] != null)
{
document.write("<b>" + fourmTabs[i].title + "</b>" + "<br/><a href='" + fourmTabs[i].url + "'>" + fourmTabs[i].url + "</a><br/><br/>");
}
else {
document.write("??" + i);
}
}
});
}
window.addEventListener("DOMContentLoaded", GetUrls());
function OpenInNewTab(url )
{
var win=window.open(url, '_blank');
win.focus();
}
//document.addEventListener('??', OpenInNewTab(??));
我尝试使用这个OpenInNewTab()函数,但我不能从GetUrls()发送链接到它,因为这些是局部变量。在这一点上有什么帮助? 或者有更好的想法打开新标签中的链接??
这是popup.html:
<!DOCTYPE html>
<html>
<head>
<title>Links Collector</title>
<style>
body {
min-width:500px;
overflow-x:hidden;
}
</style>
<script src="popup.js"></script>
</head>
<body>
<div style="height: 150px; clear: both;"></div>
</body>
</html>
清单文件:
{
"manifest_version": 2,
"name": "Links collector",
"description": "This extension get urls you open and can save them.",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs"
],
"content_security_policy": "script-src 'self'; object-src 'self'"
}
答案 0 :(得分:0)
您应该将目标属性添加到<a>
,如下所示
document.write("<b>" + fourmTabs[i].title + "</b>" + "<br/><a href='" + fourmTabs[i].url + "' target='_blank'>" + fourmTabs[i].url + "</a><br/><br/>");
有关详细信息,请查看此http://www.w3schools.com/tags/att_a_target.asp。