I make chrome extension that collects links and opening them in new tab when clicking on them. I made the first part that is collecting all the current URLs and this is the 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? or there is better idea to open links i get in new tab??
This is 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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.