-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.js
199 lines (184 loc) · 5.37 KB
/
replace.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
function tryGetReplace(word) {
if(typeof word != "string") {
return false;
}
var isCapitalized = false;
var start = word.length;
var end = 0;
for(var i = 0; i < word.length; i++) {
if(word.charCodeAt(i) >= 65 && word.charCodeAt(i) <= 90) {
isCapitalized = true;
start = i;
break;
}
else if(word.charCodeAt(i) >= 97 && word.charCodeAt(i)<= 122) {
start = i;
break;
}
}
for(var i = word.length; i >= 0; i--) {
if((word.charCodeAt(i) >= 97 && word.charCodeAt(i) <= 122) || (word.charCodeAt(i) >= 65 && word.charCodeAt(i) <= 90)) {
end = i;
break;
}
}
if(end <= start) {
return false;
}
var testedword = word.slice(start,end+1).toLowerCase();
//console.log(testedword + "-Tested Word");
var firstchars = word.slice(0,start);
var lastchars = word.slice(end+1);
/*
for(var i = 0; i < testedword.length; i++) {
if(testedword.charCodeAt(i) < 65 || testedword.charCodeAt(i) > 90){
return false;
}
}
*/
if(isSimple(testedword)) {
var rawreplacement = getSynonym(testedword);
var replacement;
if(isCapitalized) {
replacement = firstchars.concat(rawreplacement.slice(0,1).toUpperCase(),rawreplacement.slice(1),lastchars);
}
else {
replacement = firstchars.concat(rawreplacement,lastchars);
}
return replacement;
} else {
return false;
}
}
//Test whether a word is simple enough to replace but not too simple
function isSimple(word) {
/*
var rank = 0;
if(word in frequencylist) {
rank = frequencylist[word];
}
if(rank > 50 && rank < 1000) {
return true;
}
return false;
*/
if(word.length < 6) {
return true;
}
return false;
}
//Get a synonym for a word
function getSynonym(word) {
return word.concat("test");
}
//Replace approximately every nth word in an array of words with a synonym
function replaceWords(wordlist, n) {
var index = 0;
index += parseInt(Math.random()*n);
while(index < wordlist.length) {
var replacement = tryGetReplace(wordlist[index]);
//var replacement = wordlist[index].concat("ttt");
if(replacement) {
wordlist[index] = replacement;
index += parseInt(Math.random()*1.5*n);
}
else {
index += 1;
}
}
}
//Turn an array of words back to a paragraph of text
function backToText(wordlist) {
var string = wordlist[0];
for(var i = 1; i < wordlist.length; i++) {
string = string.concat(" ", wordlist[i]);
}
return string;
}
/*
var frequencylist = JSON.parse($.getJSON( "words.json"));
var list = filterParents(document.getElementByTagName("*"));
var wordlists = getWordList(list);
var textlist = {};
*/
console.log("Starting Replacement BETA");
var frequencylist;
console.log(chrome.extension.getURL('words.json'));
// $.getJSON(chrome.extension.getURL('words.json'), function(data) {
// frequencylist = data;
// console.log(typeof frequencylist);
// });
//SUCCESSFUL JSON
var freqxhr = new XMLHttpRequest(); //create a new XMLHttpRequest object to make a GET call from
freqxhr.onreadystatechange = function() { //Create a function that executes whenever the state of the XMLHttpRequest changes
if (this.readyState == 4 && this.status == 200) { //This specifies when a response is ready
frequencylist = JSON.parse(this.responseText); //responseText is the str of the response, so parse it and set frequencylist to it
}
}
freqxhr.open("GET", chrome.extension.getURL('words.json'), true); //prepare GET call
freqxhr.send(); //make GET call
console.log(typeof frequencylist);
/*
var allelements = document.getElementsByTagName("*"); //All elements on page
console.log(allelements);
var list = filterParents(allelements); //Array indexes of elements on page meeting criteria
console.log(list);
console.log(document.getElementsByTagName("*")[list[0]].innerHTML);// = "why, oh god why will this message not appear";
var wordlists = getWordList(list, allelements); //allelements index : list of words
//var textlist = {};
>>>>>>> 4e797a68a7db469abcaf444936fd0775762e76b6
for(id in wordlists) {
replaceWords(wordlists[id], 7);
allelements[id].innerHTML = backToText(wordlists[id]);
}
*/
// Inside content.js
var backgroundvars = [];
console.log("Sending Message");
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
backgroundvars[0] = response.farewell[0];
backgroundvars[1] = response.farewell[1];
});
if(backgroundvars.length < 1) {
backgroundvars[0] = 5;
backgroundvars[1] = true;
}
if(backgroundvars[1]) {
walk(document.body);
}
function walk(node)
{
// I stole this function from here:
// http://is.gd/mwZp7E
var child, next;
switch ( node.nodeType )
{
case 1: // Element
case 9: // Document
case 11: // Document fragment
child = node.firstChild;
while ( child )
{
next = child.nextSibling;
walk(child);
child = next;
}
break;
case 3: // Text node
handleText(node);
break;
}
}
function handleText(textNode)
{
var wordlist = textNode.nodeValue.split(" ");
console.log("Initial list "+wordlist);
replaceWords(wordlist, backgroundvars[0]);
console.log("Changed list " + wordlist);
var finalvalue = backToText(wordlist);
console.log("Resulting text " + finalvalue);
textNode.nodeValue = finalvalue;
var v = textNode.nodeValue;
v = v.replace(/\bAndrew\b/g, "The Greatest Among Us");
textNode.nodeValue = v;
}