-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_links.js
51 lines (44 loc) · 1.41 KB
/
send_links.js
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Send back to the popup a sorted deduped list of valid link URLs on this page.
// The popup injects this script into all frames in the active tab.
var links = [].slice.apply(document.getElementsByTagName('a'));
links = links.map(function(element) {
// Return an anchor's href attribute, stripping any URL fragment (hash '#').
// If the html specifies a relative path, chrome converts it to an absolute
// URL.
var href = element.href;
var hashIndex = href.indexOf('#');
if (hashIndex >= 0) {
href = href.substr(0, hashIndex);
}
return {
href: href,
innerText: element.innerText
};
});
links.sort(function(a, b) {
var nameA = a.href.toUpperCase(); // ignore upper and lowercase
var nameB = b.href.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
// Remove duplicates and invalid URLs.
var kBadPrefix = 'javascript';
for (var i = 0; i < links.length;) {
if (((i > 0) && (links[i].href == links[i - 1].href)) ||
(links[i].href == '') ||
(kBadPrefix == links[i].href.toLowerCase().substr(0, kBadPrefix.length))) {
links.splice(i, 1);
} else {
++i;
}
}
chrome.extension.sendRequest(links);