Jump to content

User:ערן/WeaselWords

From Wikipedia, the free encyclopedia

/* Written by User:Ravid ziv and User:ערן

  • /

mw.hook( 've.activationComplete' ).add(function(){ importStylesheetURI('//tools.wmflabs.org/eranbot/clippy.css'); $.getScript('//tools.wmflabs.org/eranbot/clippy.min.js', function(){ var loadedAgent; var weasleWords = []; var warningsForWeasleWords = []; var WEASLE_WORD_PAGE = "User:Ravid ziv/weaselWords";

function loadAgent(callback){ if(loadedAgent) { callback(loadedAgent); } else { clippy.load('Clippy', function(agent) { loadedAgent = agent; agent._balloon.CLOSE_BALLOON_DELAY=10000; agent.show(); agent.moveTo(50,50) window.agent=agent; callback(loadedAgent); }); } }

function showWarning(warn, x, y){ loadAgent(function(agent) { agent.speak(warn); if (x && y) { agent.moveTo(x,y); } }); }

function loadWeaselWords() { var api = new mw.Api(); api.get({ action:'parse', page: WEASLE_WORD_PAGE, prop: 'wikitext' }).done(function (data) { var text = data.parse.wikitext['*'] var genrealWeasleWords = text.split('\n');

for (var i=0;i<genrealWeasleWords.length;i++) {

var splitedweasleWords = genrealWeasleWords[i].split("//"); //console.log(splitedweasleWords[0].split('*')[1]);

weasleWords.push(splitedweasleWords[0].split('*')[1]); warningsForWeasleWords.push(splitedweasleWords[1]); } searchWeasleWarnings(); }); }

function searchWeasleWarnings(){ var text = getText().join(); var warnings = []; for (var i=0; i<weasleWords.length;i++) { console.log(text.indexOf(weasleWords[i]));

if (text.indexOf(weasleWords[i])>-1) { warnings.push("weasleWord: " +weasleWords[i] + " The warning: " +warningsForWeasleWords[i]); } }

console.log(warnings); if ( warnings.length>0 ) { showWarning( warnings.join('\n') ); } } function extractText(){ var nodes = []; var model = ve.instances[0].model; function getTextNodes( obj ) { var i;

for ( i = 0; i < obj.children.length; i++ ) { if ( obj.children[i].type == 'text'){ nodes.push(obj.children[i]); }

if ( obj.children[i].children ) { getTextNodes( obj.children[i] ); } } } getTextNodes(ve.instances[0].model.documentModel.getDocumentNode()); return nodes; }


function getText (){ var textNodes = extractText(); var model = ve.instances[0].model; var firstIndex = 0; var numReplacements = 0; var text = [];

for (var nodeI = 0; nodeI < textNodes.length; nodeI++) { var node = textNodes[nodeI]; var nodeRange = node.getRange(); var nodeData = model.getFragment( nodeRange ).getData(); var nodeText = $.map(nodeData, function( e ){ if ( e instanceof Array){ return e[0]; } else { return e; } }).join(); ;

text.push(nodeText);

}

return text; }; loadAgent(function(){}) loadWeaselWords(); }); });