User:Danski454/w2wFinder.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:Danski454/w2wFinder. |
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 );