-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
186 lines (166 loc) · 4.3 KB
/
content.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//queue for paragraphs
pq = [];
changedParagraphs = [];
var punc = {"!":true, ".": true, "?": true}
var numP = 0;
var doneP = 0;
var density = 10;
var active = true;
chrome.extension.sendMessage({type: "activeQuery"},
function(response){
active = response.val;
});
// content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("received: " + request);
if(request.message === "clicked_browser_action" ) {
var firstHref = $("a[href^='http']").eq(0).attr("href");
chrome.runtime.sendMessage({"message": "open_new_tab", "url": firstHref});
}
if(request.message === "toggledOff"){
console.log("received toggledoff");
density = 0;
stripParagraphs(changedParagraphs);
active = false;
}
if(request.message === "toggledOn"){
console.log("received toggledon");
density = request["val"];
activateAll();
active = true;
}
if(request.message === "densityChange"){
density = request["val"];
console.log("active: " + active);
if(active)
activateAll();
}
}
);
chrome.extension.sendMessage({type: "densityQuery"},
function(response){
density = response.val;
});
function stripParagraphs(paragraphs){
console.log("paragraphs: " + paragraphs);
$.each(paragraphs, function(index, value){
$(this).find("a").each(function(){
console.log($(this).attr("title"));
if($(this).attr("title") != undefined){
nullify($(this));
}
});
});
}
function printParagraphs(paragraphs){
for (var i = 0; i < paragraphs.length; i++)
console.log(paragraphs[i].html());
}
function activateAll(){
$(changedParagraphs).each(function(){
modifyParagraph($(this));
});
}
function activate(aref){
var syn = $(aref).attr("inactiveTitle");
var originalWord = $(aref).text();
$(aref).removeAttr("inactiveTitle");
$(aref).attr("title", originalWord);
$(aref).text(syn);
$(aref).removeClass("inactiveMaster");
$(aref).addClass("masterTooltip");
$(aref).addClass("exthighlight");
}
function nullify(aref){
var originalWord = $(aref).attr("title");
var syn = $(aref).text();
$(aref).removeAttr("title");
$(aref).attr("inactiveTitle", syn);
$(aref).text(originalWord);
$(aref).addClass("inactiveMaster");
$(aref).removeClass("masterTooltip");
$(aref).removeClass("exthighlight");
}
function modifyParagraph(paragraph){
console.log("density: " + density);
$(paragraph).find("a").each(function(){
if(Math.random() * 10 < density ){
if($(this).attr("title") == undefined)
activate($(this));
}
else{
if($(this).attr("inactiveTitle") == undefined)
nullify($(this));
}
});
}
function analyzeQueue(){
if(pq.length == 0)
return;
paragraph = pq.shift();
console.log("paragraph: " + paragraph);
var text = paragraph.html();
var content = paragraph.text();
console.log("TEXT: " + text)
console.log("CONTENT: " + content)
if(text != content || ! punc[text[text.length - 1]]){
doneP++;
if(pq.length == 0)
return;
else{
analyzeQueue();
return;
}
}
chrome.runtime.sendMessage({type: "request", content: text},
function(response){
console.log("RESP: " + response)
if(response != null)
paragraph.html(response.content);
changedParagraphs.push(paragraph);
modifyParagraph(paragraph);
doneP++;
analyzeQueue();
});
}
$(document).ready(function(){
// console.log("ready");
$("p").each( function(){
//replace words with their synonyms
pq.push($(this));
numP++;
});
analyzeQueue();
// The node to be monitored
var target = $("body,html");
// Create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var newNodes = mutation.addedNodes; // DOM NodeList
if( newNodes !== null ) { // If there are new nodes added
var $nodes = $( newNodes ); // jQuery set
$nodes.each(function() {
var $node = $( this );
var $paragraphs = $node.find("p");
var done = (doneP == numP);
$paragraphs.each(function(){
pq.push($(this));
numP++;
});
if(done) //if previous analyze is finished
analyzeQueue();
});
}
});
});
// Configuration of the observer:
var config = {
attributes: true,
childList: true,
characterData: true,
subtree: true
};
// Pass in the target node, as well as the observer options
observer.observe(target[0], config);
});