forked from tfiers/kul-machine-learning-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlStreamHandler.user.js
158 lines (145 loc) · 4.24 KB
/
urlStreamHandler.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// ==UserScript==
// @name URL Stream Handler
// @namespace be.kuleuven.cs.dtai
// @description Send visited urls to local server and show most likely links.
// @version 1
// @grant GM_xmlhttpRequest
// @run-at document-end
// ==/UserScript==
try {
var toppage = (window.top == window.self);
if (!toppage) {
return;
}
var url = window.location.href;
var send = function(data, onload) {
data.ts = (new Date()).toISOString();
data_string = JSON.stringify(data);
GM_xmlhttpRequest({
method: "POST",
url: "http://localhost:8000",
data: data_string,
headers: {
"Content-Type": "application/json",
"Content-Length": data.length
},
onerror: function(error) {
console.log('Calling urlStreamHandler failed', error);
},
onload: onload
});
};
// Associate a click event with all links
var addClickEvent = function(element) {
if (element.dataset.dtaitracked) {
return;
}
element.dataset.dtaitracked = true;
element.addEventListener('click', function(link) {return function(event) {
try {
var href = '';
if (link.href !== undefined) {
href = link.href;
}
send({
"action": "click",
"target": href,
"url": url,
});
} catch (e) {
console.log('An error occured in a click listener', e);
}
};}(element));
};
for (var i=0; i<document.links.length; i++) {
addClickEvent(document.links[i]);
}
var observer = new MutationObserver(function(mutations) {
try {
mutations.forEach(function(mutation) {
try {
var node;
for (var i=0; i<mutation.addedNodes.length; i++) {
node = mutation.addedNodes[i];
if (node.tagName === "A" || node.tagName === "a") {
addClickEvent(node);
} else if (node.getElementsByTagName) {
var atags = node.getElementsByTagName('a');
for (var aidx=0; aidx<atags.length; aidx++) {
addClickEvent(atags[aidx]);
}
}
}
} catch (e) {
console.log('An error occured processing added nodes', e);
}
});
} catch (e) {
console.log('An error occured after a mutation change', e);
}
});
observer.observe(document.body, {childList:true, subtree:true});
// Window events
// Possible events include hashchange, pageshow, popstate, beforeunload
window.addEventListener('beforeunload', function(event) {
send({
"action": 'beforeunload',
"url": url
});
});
// Catch any other changes by polling the location
setInterval(function() {
try {
if(location.href !== url) {
send({
"action": 'polling',
"url": location.href,
});
url = location.href;
}
} catch (e) {
console.log('An error occured while processing polled change', e);
}
}, 500);
// Send current page load and react to result
var html = '';
if (document.body) {
html = document.body.innerHTML;
}
send({
"action": "load",
"url": url,
"top": toppage,
"html": html
}, function(response) {
try {
data = JSON.parse(response.response);
console.log("create div");
var div = document.createElement("div");
div.style.fontFamily = "sans"
div.style.width = "300px";
div.style.height = "100px";
div.style.background = "#DCDCDC";
div.style.color = "black";
div.style.border = "1px solid black";
div.style.position = "fixed";
div.style.right = "10px";
div.style.top = "50px";
div.style.zIndex = "10000";
var best_guesses = data.guesses;
for (var i=0; i<best_guesses.length; i++) {
var aTag = document.createElement("a");
var br = document.createElement("br");
aTag.setAttribute("href",best_guesses[i]);
aTag.innerHTML = " " + (i+1).toString() + " " + best_guesses[i];
div.appendChild(aTag);
div.appendChild(br);
}
document.body.appendChild(div);
} catch (e) {
console.log('An error occured while processing the guesses', e);
}
});
} catch (e) {
console.log('An error occured', e);
}