简体   繁体   English

新标签功能

[英]New Tab function

I make chrome extension that collects links and opening them in new tab when clicking on them. 我制作了chrome扩展程序,用于收集链接并在单击它们时在新标签页中打开它们。 I made the first part that is collecting all the current URLs and this is the popup.js: 我制作了收集所有当前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(??)); 

I try to use this OpenInNewTab() function but i cant send links from GetUrls() to it because these are local variables..any help in this point? 我尝试使用此OpenInNewTab()函数,但是由于这些是局部变量,因此无法从GetUrls()向其发送链接。 or there is better idea to open links i get in new tab?? 还是有更好的主意来打开我在新标签页中获得的链接?

This is popup.html: 这是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 file: 清单文件:

{
  "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'"
}

You should add target attribute to <a> as follows 您应该将目标属性添加到<a> ,如下所示

document.write("<b>" + fourmTabs[i].title + "</b>" + "<br/><a href='" + fourmTabs[i].url + "' target='_blank'>" + fourmTabs[i].url + "</a><br/><br/>");

For more information check this http://www.w3schools.com/tags/att_a_target.asp out. 有关更多信息,请访问http://www.w3schools.com/tags/att_a_target.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM