-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-results-to-domain.js
60 lines (52 loc) · 1.82 KB
/
google-results-to-domain.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
56
57
58
59
60
// ==UserScript==
// @name Copy Google Results Domain
// @include https://www.google.tld/*
// @run-at document-start
// @grant GM_setClipboard
// ==/UserScript==
//Majority of this has been taken from https://stackoverflow.com/questions/8498592/extract-hostname-name-from-string
function extractHostname(url) {
var hostname;
//find & remove protocol (http, ftp, etc.) and get hostname
if (url.indexOf("//") > -1) {
hostname = url.split('/')[2];
}
else {
hostname = url.split('/')[0];
}
hostname = hostname.split(' ')[0];
//find & remove port number
hostname = hostname.split(':')[0];
//find & remove "?"
hostname = hostname.split('?')[0];
if (hostname.includes("www.") == true) {
hostname = hostname.split('www.')[1]
}
return hostname;
}
window.onload = function(){
if (location.href.match(/[#&?]q=/)) {
// get all results in an array
var links = document.querySelectorAll('.r cite');
//var index = 1
links.forEach(function(entry, index) {
console.log(index + 1, extractHostname(entry.innerHTML));
index += 1
});
window.addEventListener('keydown', function(e) {
var digit = e.keyCode - 48;
//I'm aware this next section is messy and unefficient. Especially the linkIndex +=100 haha. But, lunchtime project...
var linkIndex = 1
links.forEach(function(entry){
if (linkIndex == digit){
var domain = extractHostname(entry.innerHTML);
GM_setClipboard (domain);
console.log("Copied entry ", digit, " - " , domain)
linkIndex +=100
}else {
linkIndex += 1
}
});
});
};
};