-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopen-selected-links.user.js
55 lines (46 loc) · 1.71 KB
/
open-selected-links.user.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
52
53
54
55
// ==UserScript==
// @name open-selected-links
// @namespace http://www.strongasanox.co.uk/greasemonkey
// @description Opens all the selected links in new tabs (one tab per link)
// @include *
// ==/UserScript==
(function() {
document.addEventListener('keydown', function(e) {
var ctrlKeyCode = 17;
var cmdKeyCode = 224;
if (e.keyCode == ctrlKeyCode || e.keyCode == cmdKeyCode) {
document.addEventListener('mouseup', openSelectedLinks, true);
}
}, true);
document.addEventListener('keyup', function(e) {
// Remove the mousedown handler so it doesn't open links just
// by selecting text *without* using ctrl / cmd
document.removeEventListener('mouseup', openSelectedLinks, true);
}, true);
function openSelectedLinks(e) {
var selection = window.getSelection();
if (selection) {
var range = selection.getRangeAt(0);
var selectionDom = range.cloneContents();
var selectedLinks = selectionDom.querySelectorAll('a');
var link;
for (var i = selectedLinks.length - 1; i >= 0; i--){
link = selectedLinks[i];
if (isGoogleResultsPage() &&
(isGoogleCachedLink(link) || isGoogleSimilarLink(link))) {
continue;
}
GM_openInTab(selectedLinks[i].href);
}
}
}
function isGoogleResultsPage() {
return document.location.host.indexOf('.google.') !== -1;
}
function isGoogleCachedLink(link) {
return link && link.textContent === 'Cached';
}
function isGoogleSimilarLink(link) {
return link && link.textContent === 'Similar';
}
})();