-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomify.js
79 lines (75 loc) · 2.43 KB
/
domify.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
var DOM_MISTAKE_LIKELIHOOD_PERCENTAGE = 80;
var erSuffixes = ["er", "ez", "é", "ée", "és", "ées"]
var switchWords = {
"ça": "sa",
"sa": "ça",
"se": "ce",
"ce": "se",
"c'est": "ses",
"C'est": "Ses",
"ses": "c'est",
"Ses": "C'est",
"ait": "est",
"est": "ait"
}
var switchExpressions = {
"genre": "jors",
"au bout d'un moment": "ou bout d'un moment",
"Kuala Lumpur": "Guadaloumpur",
"il semblerait que": "il paraitrait comme quoi",
"il apparait que": "il paraitrait comme quoi",
"dans les écoles": "aux écoles",
"it's his fault": "his his faute"
}
function escapeRegExp(string) { return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); }
function replaceAll(string, find, replace) { return string.replace(new RegExp(escapeRegExp(find), 'g'), replace); }
function domifyText(text) {
var tokens = text.split(/\s+/);
var domifiedTokens = [];
for (t=0; t<tokens.length; t++) {
token = tokens[t];
newToken = token;
if (Math.floor(Math.random()*100) < DOM_MISTAKE_LIKELIHOOD_PERCENTAGE) {
for (j=0;j<erSuffixes.length; j++) {
regex = new RegExp("([a-zA-Z]+)" + erSuffixes[j] + "([\.,]?)$", "g");
if (regex.test(token)) {
newToken = token.replace(regex, "$1" + erSuffixes[Math.floor(Math.random()*erSuffixes.length)] + "$2");
}
}
for (sword in switchWords) {
regex = new RegExp("^" + sword + "([\.,]?)$", "g");
if (regex.test(token)) {
newToken = token.replace(regex, switchWords[sword] + "$1");
}
}
}
domifiedTokens.push(newToken);
}
output = domifiedTokens.join(" ");
for (expression in switchExpressions) {
output = replaceAll(output, expression, switchExpressions[expression]);
}
return output
}
function getTextNodesIn(node, includeWhitespaceNodes) {
var textNodes = [], nonWhitespaceMatcher = /\S/;
function getTextNodes(node) {
if (node.nodeType == 3) {
if (includeWhitespaceNodes || nonWhitespaceMatcher.test(node.nodeValue)) {
textNodes.push(node);
}
} else {
for (var i = 0, len = node.childNodes.length; i < len; ++i) {
getTextNodes(node.childNodes[i]);
}
}
}
getTextNodes(node);
return textNodes;
}
(function() {
var textElements = getTextNodesIn(document.getElementsByTagName("body")[0], false);
for (var te = 0; te < textElements.length; te++) {
textElements[te].textContent = domifyText(textElements[te].textContent);
}
})();