Skip to content

Commit

Permalink
Version 2.1 - simplified locking
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartpb committed Mar 19, 2012
1 parent 1d733cc commit dc31034
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions leopard.user.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// ==UserScript==
// @name s/keyboard/leopard/g
// @version 2.0
// @version 2.1
// @description Replaces the word "keyboard" with "leopard".
// @match *://*/*
// @updateURL http://userscripts.org/scripts/source/128626.meta.js
// @downloadURL https://userscripts.org/scripts/source/128626.user.js
// ==/UserScript==


function leopardize(str) {
return str.replace(/keyboard/g,"leopard")
.replace(/Keyboard/g,"Leopard")
Expand All @@ -16,8 +15,14 @@ function leopardize(str) {
.replace(/K[Ee][Yy][Bb][Oo][Aa][Rr][Dd]/g,"Leopard")
}

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) {
Expand All @@ -35,25 +40,19 @@ function changeTextNodes(node) {
}
}

var replacement_running

function lock_safe(f,param) {
if(!replacement_running){
replacement_running = true
f(param)
replacement_running = false
}
}

function insertion_listener(event) {
lock_safe(changeTextNodes,event.target)
//change any new text nodes in a node that is added to the body
changeTextNodes(event.target)
}

function cdm_listener(event) {
lock_safe(replaceTextContent,event.target)
//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)
document.title = leopardize(document.title)

0 comments on commit dc31034

Please # to comment.