-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent.js
More file actions
executable file
·49 lines (42 loc) · 1.08 KB
/
content.js
File metadata and controls
executable file
·49 lines (42 loc) · 1.08 KB
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
chrome.extension.onMessage.addListener(function(message, sender, sendResponse) {
switch(message.type) {
case "click-link":
console.log('click-link');
clickLink();
break;
case "go-home":
console.log('go-home');
navHome();
break;
}
sendResponse({message: 'recieved: ' + message.type });
});
var clickLink = function() {
var link = getLinkInDomain(window.location.href);
window.location = link;
}
var getLinkInDomain = function(uri) {
domain = getHostName(uri);
var links = document.links;
linksInDomain = [];
for (i = 0; i < links.length; i++) {
if(getHostName(links[i]) == domain) {
linksInDomain.push(links[i]);
}
}
if(linksInDomain.length == 0) {
navHome();
}
var u = linksInDomain[Math.floor(Math.random()*linksInDomain.length)];
return u;
}
var navHome = function() {
chrome.runtime.sendMessage({type: 'get-starting-url'}, function(response){
window.location = response.url;
});
}
var getHostName = function(uri) {
var u = document.createElement('a');
u.href = uri;
return u.hostname;
}