-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentScript.js
35 lines (35 loc) · 1.12 KB
/
contentScript.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
chrome.extension.onRequest.addListener(function(command, sender, sendResponse) {
if (command == "openSelectedUrls") {
var urls = new Array();
var selection = window.getSelection();
var doc = window.document;
if (selection.type != 'Range') {
// Look for selection within iframes on the page instead
var fs = document.getElementsByTagName('iframe');
var f;
for (var i = 0; f = fs[i]; i++) {
try {
doc = f.contentDocument;
selection = doc.getSelection();
if (selection.type == 'Range') {
break;
}
}
catch(e)
{
continue;
}
}
}
var hrefs = doc.links;
for (i = 0; i < hrefs.length; i++) {
if (selection.containsNode(hrefs[i], true)) {
urls.push(hrefs[i].href);
}
}
sendResponse({
"urls": urls,
"fromUrl": document.location.href
});
}
});