-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
95 lines (88 loc) · 2.82 KB
/
content.js
File metadata and controls
95 lines (88 loc) · 2.82 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
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
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
// get selection
if (message.method == "get_selected_text"){
console.log("get selected message recieved")
var selText = window.getSelection().toString()
sendResponse({data: selText})
// console logger
} else if (message.method == "console_log"){
console.log(message.data)
// text highlight
} else if (message.method == "highlight_text"){
colourText(message.text)
}
})
function colourText(text){
text = escapeRegExp(text)
// match half words -- some<b>word</b>
// very slow tech
text = text.replace(/([^\s\\])/g, '$1(<[^\>]*?>)*')
// console.log(text)
// TODO: match escaped html in code
var hay = text.replace(/\s/g, '(<[^\>]*?>|\\s)*')
var reg = new RegExp("(" + hay + ")") // g option matches more than one, without g only one
// console.log(reg)
// match and break tags (as side nesting tags is invalid)
var bodyHtml = $(document.body).html()
var match = bodyHtml.match(reg)
var repl = match[0].replace(
/(<.*?>)/g,
"</span>" + "$1" + "<span class='anotode-highlighted-text'>"
)
// console.log(repl)
$(document.body).html(
bodyHtml.replace(
match[0]
,"<span class='anotode-highlighted-text'>" + repl + "</span>"
)
)
}
// http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
/*
* FETCH DATA FROM SERVER AND HIGHLIGHT
*/
getKey("token", function(token){
if (token == null)
return;
var currentUrl = window.location.href
$.getJSON(
"https://anotode.herokuapp.com/api/highlights?token=" + token + "&url_eq=" + encodeURIComponent(currentUrl),
// ^^ http falls victim to cross origin restriction policy
// http://stackoverflow.com/questions/26285539/
// bg.js has no such restriction as it has the url in permissions
// http://stackoverflow.com/questions/36348559/chrome
function(data, textStatus, jqXHR){
console.log(data)
for (i=0; i<data.length; i++){
if (data[i].url == currentUrl){
try {
colourText(data[i].text)
} catch (er) {
console.log('Failed')
}
}
}
}
)
/*
* HIGHLIGHT Google Searches
*/
var googleRegex = new RegExp("google[^/]+/search.q=") // ? doesnt match in url. no idea why
var matches = currentUrl.match(googleRegex)
if (matches) {
$.getJSON(
"https://anotode.herokuapp.com/api/highlights/urls?token=" + token,
function (data, textStatus, jqXHR) {
$("h3.r").each(function () {
var link = $(this).find("a").attr("href")
if (data.indexOf(link) != -1) {
$(this).find("a").addClass("anotode-search-highlight")
}
})
}
)
}
})