User:MZMcBride/wordclick.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. |
![]() | Documentation for this user script can be added at User:MZMcBride/wordclick. |
/*
Proof of anti-concept. This sucks, don't use it.
Bite me, MZM.
*/
if(wgAction == 'view' && wgNamespaceNumber == 0) addOnloadHook(ondblclickBuild)
function ondblclickBuild() {
var content = document.getElementById('bodyContent') || document.getElementById('content') || document.body
findTextNodes(content);
appendCSS('#definediv {position:fixed;top:0;right:0;width:50%;height:50%;border:2px solid black;z-index:10;background-color:#ffffff;padding:1em;margin:2em;display:block;} .ddivclose {float:right;border:1px solid black;}');
}
function findTextNodes(obj) {
var childs = 0;
switch (obj.nodeType) {
case(1):
//tag
break;
case(3):
//txt
var txt = obj.nodeValue;
var txts = txt.split(' ');
obj.nodeValue = '';
for(var i=0;i<txts.length;i++) {
if(txts[i].replace(/[\n\r\t\s\[\]]*/ig,'') == '') continue
var span = document.createElement('span');
span.className = 'dclick';
span.appendChild(document.createTextNode(' ' + txts[i] + ' '));
span.setAttribute('ondblclick','defineMe("' + txts[i] + '")');
obj.parentNode.insertBefore(span,obj);
}
break;
}
while(obj.childNodes[childs]) {
if(obj.className != 'dclick' && obj.tagName != 'A') {
findTextNodes(obj.childNodes[childs]);
}
childs++;
}
}
function defineMe(word) {
var content = document.getElementById('content') || document.body
var div = document.createElement('div');
div.id = 'definediv';
var close = document.createElement('a');
close.setAttribute('href','javascript:killDefdiv()');
close.className = 'ddivclose';
close.appendChild(document.createTextNode('close'));
div.appendChild(close)
div.appendChild(document.createTextNode('The word "' + word + '" sucks. Don\'t click it.'));
content.appendChild(div);
}
function killDefdiv() {
var div = document.getElementById('definediv');
while(div.firstChild) div.removeChild(div.firstChild);
div.parentNode.removeChild(div);
}