summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/included.js
blob: 9c035bdbf74739d61747175cbb6af1ce136e6ae5 (plain)
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
"use strict";

var textNodeType;

function textContent(n) {
    if ( n.nodeType == textNodeType ) {
	return n.data;
    } else {
	var children = n.childNodes;
	var t = "";
	for ( var i=0 ; i<children.length ; i++ ) {
	    t = t.concat(textContent(children.item(i)));
	}
	return t;
    }
}

function despam() {
    // MSIE seems to barf...  Deactivate for now
    if ( (/MSIE *[1-6]\./).test(navigator.userAgent) )
	return;
    // Now replace as appropriate.
    var elts = document.getElementsByTagName("span");
    for ( var i=0 ; i<elts.length ; i++ ) {
	var elt = elts.item(i);
	if ( elt.className == "replace-commercial-at" ) {
	    elt.parentNode.replaceChild(document.createTextNode("@"),elt);
	    i--;  // Semi-bugware
	} else if ( elt.className == "replace-full-stop" ) {
	    elt.parentNode.replaceChild(document.createTextNode("."),elt);
	    i--;  // Semi-bugware
	}
    }
    // Merge adjacent text nodes.
    try {
	document.normalize();  // Your DOM is BROKEN!
    } catch (exn) {
	document.documentElement.normalize();
    }
    // Next, process all <a> elements having class="despammed-address".
    elts = document.getElementsByTagName("a");
    for ( var i=0 ; i<elts.length ; i++ ) {
	var elt = elts.item(i);
	if ( elt.className == "despammed-address" ) {
	    var addr = "mailto:".concat(textContent(elt));
	    elt.setAttribute("href",addr);  // (abstract)
	    elt.href = addr;  // (semantic)
	}
    }
}

function permuteWord(s) {
    if ( s.length <= 3 )
	return s;
    var tab = s.split("");
    var n = tab.length - 1;
    for ( var i=1 ; i<n ; i++ ) {
	var j = i + Math.floor(Math.random()*(n-i));
	if ( j == i )
	    continue;
	var tmp = tab[j];
	tab[j] = tab[i];
	tab[i] = tmp;
    }
    return tab.join("");
}

function permuteString(s) {
    return s.replace(/[A-Za-zÀ-ÿ]+/g, permuteWord);
}

function permuteDoc(doc) {
    var body = doc.body;
    var walker = doc.createTreeWalker(body, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, null, false);
    var node = walker.nextNode();
    while ( node ) {
	if ( node.nodeType == Node.TEXT_NODE ) {
	    var thisnode = node;
	    node = walker.nextNode();
	    var str = permuteString(thisnode.nodeValue);
	    thisnode.parentNode.replaceChild(doc.createTextNode(str), thisnode);
	} else if ( node.nodeType == Node.ELEMENT_NODE
		    && ( node.localName == "pre"
			 || node.localName == "code"
			 || node.localName == "script"
			 || node.localName == "style" ) ) {
	    do {
		node = walker.nextSibling();
		if ( node )
		    break;
		node = walker.parentNode();
		if ( ! node )
		    break;
	    } while ( true );
	} else
	    node = walker.nextNode();
    }
}

function aprilsFool() {
    var today = new Date();
    if ( today.getMonth() == 3 && today.getDate() == 1 )
	permuteDoc(window.document);
}

function onLoad() {
    // Start with some bugware...
    try {
	textNodeType = Node.TEXT_NODE;
    } catch (exn) {  // Your DOM is BROKEN!
	textNodeType = 3;
    }
    // Now despam email adresses.
    despam();
    aprilsFool();
}