User:Danski454/w2wFinder.js
Appearance
From Wikipedia, the free encyclopedia
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 code will be executed when previewing this page.
This user script seems to have a documentation page at User:Danski454/w2wFinder.
varw2wNext; (function(mw,$){//isolate code varLIST_LOCATION="User:Danski454/w2wFinder/list"; varinsensitiveList=[]; varsensitiveList=[]; varregexList=[]; varindex=-1; varuseDiv=false; functioncreateLists(location){ $.getJSON( mw.util.wikiScript('api'), { format:'json', action:'query', prop:'revisions', rvprop:'content', rvlimit:1, titles:location } ) .done(function(data){ varpage,wikitext; //try { for(pageindata.query.pages){ wikitext=data.query.pages[page].revisions[0]['*']; createListsWikitext(wikitext); } /*} catch ( e ) { alert("Unable to load words to watch list - processing crashed"); }*/ }) .fail(function(){alert("Unable to load words to watch list - ajax failed");}); } functioncreateListsWikitext(wikitext){ varregex_regex=/^\/.+\/[gmixXsuUAJD]*$/; varlines=wikitext.split("\n"); for(vari=0;i<lines.length;i++){ if(lines[i].charAt(0)==="*"){//if line is a bullet point varline=lines[i].slice(1).trim(); if(regex_regex.test(line)){ varre,flags,end; end=line.lastIndexOf("/"); flags=line.slice(end+1); re=line.slice(1,end); if(flags.indexOf("g")===-1){ flags=flags+"g";//force g flag } try{ regexList.push(newRegExp(re,flags)); } catch(e){ console.error("Unable to create regex /"+re+"/"+flags); } }elseif(line.toLowerCase()===line){ insensitiveList.push(line); }else{ sensitiveList.push(line); } } } createUI(); } functiontestWikitext(start){ vartext,lcaseText,earliestIssue=Number.POSITIVE_INFINITY,loc,length; text=$("#wpTextbox1").val(); if(!text){ console.warn("Words to watch finder could not find editing area, you must use the 2010 editor"); return; /*text = $("div .mw-parser-output [contenteditable='true']").first().text(); useDiv = true;*/ } lcaseText=text.toLowerCase(); for(vari=0;i<insensitiveList.length;i++){ loc=lcaseText.indexOf(insensitiveList[i],start); if(loc!==-1&&loc<earliestIssue){ length=insensitiveList[i].length; earliestIssue=loc; } } for(vars=0;s<sensitiveList.length;s++){ loc=text.indexOf(sensitiveList[s],start); if(loc!==-1&&loc<earliestIssue){ length=sensitiveList[s].length; earliestIssue=loc; } } for(varr=0;r<regexList.length;r++){ while(true){ varresult=regexList[r].exec(text); if(result===null||result.index>=earliestIssue){ break; } if(result.index>=start){ length=result[0].length; earliestIssue=result.index; break; } } regexList[r].lastIndex=0; } if(earliestIssue!==Number.POSITIVE_INFINITY){ return{index:earliestIssue,length:length}; }elseif(start===0){ return{index:-1,length:0};//there are no occurences }else{ returntestWikitext(0);//we have reached the end, but the could be previous occurences } } functioncreateUI(){ $("#editform").before('Words to watch finder: <span id="w2w-status">No issues found</span> <input id="w2w-next" type="button" value="Next issue" onclick="w2wNext();" />'); if(testWikitext(0)===-1){ $("#w2w-status").html("No issues found"); }else{ $("#w2w-status").html("Possible issues found"); } } functiongotoNextIssue(){ varout=testWikitext(index+1); index=out.index; if(index===-1){ $("#w2w-status").html("No issues found"); }else{ $("#w2w-status").html("Possible issues found"); vartextarea=$("#wpTextbox1")[0]; textarea.focus(); textarea.setSelectionRange(index,index+out.length);//move cursor } } $(document).ready(function(){ if(mw.config.get("wgAction")==="edit"&&mw.config.get("wgIsProbablyEditable")&&(mw.config.get("wgNamespaceNumber")===0||mw.config.get("wgTitle")==="Sandbox")){ createLists(LIST_LOCATION); } }); w2wNext=gotoNextIssue;//allow external calls })(mediaWiki,jQuery);