var w2wNext;
( function( mw, $ ) {//isolate code
var LIST_LOCATION = "User:Danski454/w2wFinder/list";
var insensitiveList = [];
var sensitiveList = [];
var regexList = [];
var index = -1;
var useDiv = false;
function createLists(location) {
$.getJSON(
mw.util.wikiScript('api'),
{
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'content',
rvlimit: 1,
titles: location
}
)
.done(function ( data ) {
var page, wikitext;
//try {
for ( page in data.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"); } );
}
function createListsWikitext(wikitext) {
var regex_regex = /^\/. \/[gmixXsuUAJD]*$/;
var lines = wikitext.split("\n");
for (var i = 0; i < lines.length; i ) {
if (lines[i].charAt(0) === "*") {//if line is a bullet point
var line = lines[i].slice(1).trim();
if (regex_regex.test(line)) {
var re, 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(new RegExp(re, flags));
}
catch (e){
console.error("Unable to create regex /" re "/" flags);
}
} else if (line.toLowerCase() === line) {
insensitiveList.push(line);
} else {
sensitiveList.push(line);
}
}
}
createUI();
}
function testWikitext(start) {
var text, 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 (var i = 0; i < insensitiveList.length; i ) {
loc = lcaseText.indexOf(insensitiveList[i], start);
if (loc !== -1 && loc < earliestIssue) {
length = insensitiveList[i].length;
earliestIssue = loc;
}
}
for (var s = 0; s < sensitiveList.length; s ) {
loc = text.indexOf(sensitiveList[s], start);
if (loc !== -1 && loc < earliestIssue) {
length = sensitiveList[s].length;
earliestIssue = loc;
}
}
for (var r = 0; r < regexList.length; r ) {
while (true) {
var result = 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};
} else if (start === 0) {
return {index:-1, length:0};//there are no occurences
} else {
return testWikitext(0);//we have reached the end, but the could be previous occurences
}
}
function createUI(){
$("#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");
}
}
function gotoNextIssue() {
var out = testWikitext(index 1);
index = out.index;
if (index === -1){
$("#w2w-status").html("No issues found");
} else {
$("#w2w-status").html("Possible issues found");
var textarea = $("#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 );