ОБНОВЛЕНИЕ (ПОСЛЕДНИЕ) : Не думайте, что оно может стать меньше .. ДЕМО
Последнее сжатое (332):
var e=document.body.getElementsByTagName('*'),j,i,l,x,t,b;for(i=0;e[i];i++)for(j=0;b=e[i].childNodes[j];j++)if(b.nodeType==3)b.data=b.data.replace(/\w{4,}/g,function(w){if(/(^.)(\1)+$/.test(x=w.substring(1,l=w.length-1)))return w;t=w;while(t==w)t=w[0]+x.split('').sort(function(){return 0.5-Math.random()}).join('')+w[l];return t});
код:
var e = document.body.getElementsByTagName('*'),
j, i, l, x, t, b;
for (i = 0; e[i]; i++)
for (j = 0; b = e[i].childNodes[j]; j++)
if (b.nodeType == 3) b.data = b.data.replace(/\w{4,}/g, function(w) {
if (/(^.)(\1)+$/.test(x = w.substring(1, l = w.length - 1))) return w;
t = w;
while (t == w)
t = w[0] + x.split('').sort(function() {
return 0.5 - Math.random();
}).join('') + w[l];
return t;
});
ОБНОВЛЕНИЕ даже .. меньше ..
Еще меньше версия
Я не знаю, как вы используете минификатор, но этодолжен быть не менее ( РЕДАКТИРОВАТЬ 108) байтов меньше.
сжатая версия (365 байтов):
var e=document.body.getElementsByTagName('*'),a=[],c,j,i,l,x,t,b;for(i=0;c=e[i];i++)for(j=0;b=c.childNodes[j];j++)if(b.nodeType==3){b.data=b.data.replace(/\b[a-z0-9]{4,}\b/gi,function(w){if(/(^.)(\1)+$/.test(x=w.substring(1,l=w.length-1)))return w;t=w;while(t==w)t=w[0]+x.split('').sort(function(){return Math.floor(Math.random()*2)?1:-1}).join('')+w[l];return t})}
Код:
var e = document.body.getElementsByTagName('*'),
a = [],
c, j, i, l, x, t, b;
for (i = 0; c = e[i]; i++)
for (j = 0; b = c.childNodes[j]; j++)
if (b.nodeType == 3) {
b.data = b.data.replace(/\b[a-z0-9]{4,}\b/gi, function(w) {
if (/(^.)(\1)+$/.test(x = w.substring(1, l = w.length - 1))) return w;
t = w;
while (t == w)
t = w[0] + x.split('').sort(function() {
return Math.floor(Math.random() * 2) ? 1 : -1;
}).join('') + w[l];
return t;
});
}
РЕДАКТИРОВАТЬ
НОВЫЕ ПРАВИЛА ДЕМО
КОД:
var fn = function(e) {
var ret = [],c;
for (var i = 0; i < e.length; i++) {
c = e[i].childNodes;
for (var j = 0; j < c.length; j++)
if (c[j].nodeType === 3) ret.push(c[j]);
}
return ret;
};
var es = fn(document.body.getElementsByTagName('*'));
for (var i = 0; i < es.length; i++) {
var e = es[i],len,x;
e.data = e.data.replace(/\b[a-z0-9]{4,}\b/gi, function(w) {
if (/(^.)(\1)+$/.test(x = w.substring(1, len = w.length - 1))) return w;
var tmp = w;
while (tmp === w) {
tmp = w[0] + x.split('').sort(function() {
return Math.floor(Math.random() * 2) ? 1 : -1;
}).join('') + w[len];
}
return tmp;
});
}
Это должно соблюдать все правила, а также сохранять формат и пунктуацию. DEMO
//select all nodes in document and perform map on it to filter out
//non text node types, then each one of those elements is processed.
$('*').contents().map(function(i, elem) {
if (elem.nodeType !== 3) return null;
else return elem;
}).each(function(i, elem) {
//call strip funciton defined down to get an object, with a word array, and
//charecters which was stripped along with there index in the orginal string
var str1 = '',
tmp = strip(elem.data),
words = tmp.words,
sentence;
// shuffle all words
words = $.map(words, function(x, i) {
return shuffle(x);
});
//construct raw sentence (non alphanumeric charecters)
sentence = words.join('');
//reinsert spaces and punctiouation
$.each(tmp.chars, function(i, elem) {
sentence = sentence.substring(0, elem.index) + elem.char + sentence.substring(elem.index - 1 + elem.char.length);
});
//set the element text
elem.data = sentence;
});
//shuffle funciton takes a word and shuffle the charecters between the last and the firt
function shuffle(txt) {
//if the word is smaller than 4 charecters or it has repeated charecters in
//its middle (i.e. loop, 'oo' cannot be shuffled!) then return it;
if (txt.length < 4 || /(^.)(\1)+$/.test(txt.substring(1, txt.length - 1)))
return txt;
var str = txt.split(''),
ret = [],
rand, x = 0,
tmp = txt;
//while the txt hasn't changed in the first randomization cycle then repeated
while (txt === tmp) {
ret = [];
$.each(str, function(i, c) {
if (i === str.length - 1 || i === 0) {
ret[i] = c;
return;
}
while (true) {
rand = Math.floor(Math.random() * (str.length - 2) + 1);
if (!ret[rand]) {
ret[rand] = c;
break;
}
}
});
tmp = ret.join('');
}
return ret.join('');
}
function strip(txt) {
var punc = /[^A-Za-z0-9]/g,
res, nonAlphaNum = [],
arr;
//punc regex is all non-alphanumeric charecters which will act on the string
//to point out unwanted charecters and store them in an array along with
//their index
while ((res = punc.exec(txt)) != null) {
nonAlphaNum.push({
index: res.index,
char: res[0]
});
}
//split into words
arr = txt.split(/\s/);
//remove punctiuation and other unwanted chars
arr = $.map(arr, function(x, i) {
return x.replace(punc, '');
});
return {
words: arr, //words array
chars: nonAlphaNum //array of stripped charecter objects (chars, index in orginal)
};
}
кстати, хороший выбор статьи, WWiWieikikb !!