User:Anomie/censorship.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Anomie/censorship and an accompanying .css page at User:Anomie/censorship.css. |
$( function(){
if ( !document.createNodeIterator || !window.NodeFilter || !NodeFilter.SHOW_TEXT ) {
alert( 'The script "User:Anomie/censorship" is not supported on this browser. Offensive words in the page will not be hidden.' );
$( '#content' ).addClass( 'isCensored' );
return;
}
var iter, node, nextNode, fragment, span;
var re1 = /^\s*$/;
var re2 = /^\S+$/;
var re3 = /(\s*)(\S+|$)/g;
iter = document.createNodeIterator( document.getElementById( 'content' ), NodeFilter.SHOW_TEXT, function ( node ) {
var nn = node.parentNode.nodeName.toLowerCase();
if ( nn === 'textarea' ) {
return NodeFilter.FILTER_REJECT;
}
return NodeFilter.FILTER_ACCEPT;
}, true );
node = iter.nextNode();
while ( node ) {
nextNode = iter.nextNode();
if ( re1.test( node.nodeValue ) ) {
/* Do nothing */
} else if ( re2.test( node.nodeValue ) ) {
span = document.createElement( 'span' );
span.className = 'censored';
node.parentNode.insertBefore( span, node );
span.appendChild( node );
} else {
fragment = document.createDocumentFragment();
node.nodeValue.replace( re3, function ( m, space, text ) {
if ( space.length ) {
fragment.appendChild( document.createTextNode( space ) );
}
if ( text.length ) {
span = document.createElement( 'span' );
span.className = 'censored';
span.appendChild( document.createTextNode( text ) );
fragment.appendChild( span );
}
} );
node.parentNode.replaceChild( fragment, node );
}
node = nextNode;
}
$( '#content' ).addClass( 'isCensored' );
} );