-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.html
99 lines (85 loc) · 2.34 KB
/
background.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<script type="text/javascript">
var g_currentTab;
function openAllUrls(info, tab)
{
g_currentTab = tab;
chrome.tabs.sendRequest(tab.id, "openSelectedUrls", openUrls);
}
function openUrls(params)
{
urls = eliminateDuplicates(params.urls);
fromUrl = params.fromUrl;
var baseUrl = fromUrl.replace(/#.+$/, '');
var skippedAnchors = 0;
var openedUrls = 0;
var insertAtIndex = g_currentTab.index + 1;
var openerTabId = g_currentTab.id;
for (var i in urls)
{
url = urls[i]
var relativeUrl = url.replace(baseUrl, '');
if (url.match("^javascript:.*") || relativeUrl.match("^#$"))
{
// Ignore links with href of 'javascript:....' or '#'
continue;
}
if (relativeUrl.match("^#"))
{
// Don't open links which are #anchors within the current page
skippedAnchors += 1;
continue;
}
openUrl(url, openerTabId, insertAtIndex++);
openedUrls += 1;
}
if (openedUrls == 0)
{
if (skippedAnchors > 0)
{
alert('No tabs were opened because all the links selected go to other sections of this page (#anchor links).');
}
else
{
alert('No links found in selection.');
}
}
}
function openUrl(url, openerTabId, index)
{
if (openerTabId)
{
var prop = {
url: url,
index: index,
openerTabId: openerTabId,
active: false
}
}
else
{
var prop = {
url: url,
active: false
}
}
chrome.tabs.create(prop);
}
function eliminateDuplicates(arr)
{
var i, len = arr.length,
out = [],
obj = {};
for (i = 0; i < len; i++) {
obj[arr[i]] = 0;
}
for (i in obj) {
out.push(i);
}
return out;
}
chrome.contextMenus.create({
"title": "Open selected links in new tabs",
"contexts": ["selection"],
"onclick": openAllUrls
});
</script>