forked from stuartpb/s-keyboard-leopard-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leopard.user.js
66 lines (58 loc) · 1.93 KB
/
leopard.user.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
// ==UserScript==
// @name s/keyboard/leopard/g
// @version 3.0
// @description Replaces the word "keyboard" with "leopard".
// @match *://*/*
// @license MIT License
// @updateURL http://userscripts.org/scripts/source/128626.meta.js
// @downloadURL https://userscripts.org/scripts/source/128626.user.js
// ==/UserScript==
//which letters in "keyboard" get replaced with which in "leopard"
var leopard_subs = {
'k': 'l', 'K': 'L',
'y': 'o', 'Y': 'O',
'b': 'p', 'B': 'P'
};
//Transform all instances of 'keyboard' in a string into 'leopard'
function leopardize(str) {
return str.replace(/(k)(e)(y)(b)o(ard)/ig, function(match,k,e,y,b,ard){
return leopard_subs[k] + e + leopard_subs[y] + leopard_subs[b] + ard
})
}
var TEXT_NODE = Node.TEXT_NODE || 3
var replacingContent = false
function replaceTextContent(node) {
//flag that content is being replaced so the event it generates
//won't trigger another replacement
replacingContent = true
node.textContent = leopardize(node.textContent)
replacingContent = false
}
function changeTextNodes(node) {
var length, childNodes
//If this is a text node, leopardize it
if (node.nodeType == TEXT_NODE) {
replaceTextContent(node)
//If this is anything other than a text node, recurse any children
} else {
childNodes = node.childNodes
length = childNodes.length
for(var i=0; i<length; ++i){
changeTextNodes(childNodes[i])
}
}
}
function insertion_listener(event) {
//change any new text nodes in a node that is added to the body
changeTextNodes(event.target)
}
function cdm_listener(event) {
//avoid infinite loop by ignoring events triggered by replacement
if(!replacingContent){
replaceTextContent(event.target)
}
}
changeTextNodes(document.body)
document.title = leopardize(document.title)
document.body.addEventListener ("DOMNodeInserted", insertion_listener, false)
document.body.addEventListener ("DOMCharacterDataModified", cdm_listener, false)