User:Guywan/Scripts/BulletSort.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:Guywan/Scripts/BulletSort. |
// <nowiki>
$(function(api)
{
const config =
{
debug : false
};
if(mw.config.get("wgAction") !== "edit")
{
return;
}
document.onkeyup = function(e)
{
if(e.ctrlKey && e.altKey && e.which == 83)
{
mw.notify("Sorting...", "info");
// Purely to allow 'Sorting...' to display before we start sorting.
// That's a 100 miliseconds you'll never get back.
setTimeout(function()
{
console.time("_");
try
{
run();
mw.notify("Done.", "info");
}
catch(e)
{
console.log(e);
mw.notify("Failed. See your console for more info.", "info");
}
console.timeEnd("_");
}, 100);
}
};
function run()
{
api = new mw.Api();
const txtarea = document.getElementById("wpTextbox1");
const start = txtarea.selectionStart;
const end = txtarea.selectionEnd;
// Strip ref tags.
var lines = txtarea.value.substring(start, end).replace(/<ref>.*?<\/ref>/g, "").split("\n");
if(config.debug) console.log(lines);
var level = "*";
// (1) Create the tree.
var t = tree(level, lines)[0];
if(config.debug) console.log(t);
// (2) Sort the tree.
var parsedLines = parse(txtarea.value.substring(start, end))
.replace(/(<\/?[^>]*>)|(<!--.*?-->)/g, "")
.split("\n");
if(config.debug) console.log(parsedLines);
map = {};
for(var i = 0; i < lines.length; i++)
{
map[lines[i]] = parsedLines[i].trim();
}
sort(t.sort(compare));
// (3) Convert the tree back into a string.
var sorted = join(t);
txtarea.value = txtarea.value.substring(0, start) + sorted + txtarea.value.substr(end + 1);
}
function tree(level, list)
{
var branch = [];
for(var i = 0; i < list.length; i++)
{
var line = list[i];
if(line.match(/^\*+/)[0] == level)
{
branch.push({"text": line, "children": null});
}
else if(line.match(/^\*+/)[0] == level + "*")
{
var result = tree(level + "*", list.slice(i));
branch[branch.length - 1].children = result[0];
i += result[1] - 1;
}
else
{
return [branch, i];
}
}
return [branch, i];
}
function sort(list)
{
for(var i = 0; i < list.length; i++)
{
if(list[i].children !== null && list[i].children !== undefined)
{
list[i].children.sort(compare);
sort(list[i].children);
}
}
}
function join(list)
{
joined = "";
for(var i = 0; i < list.length; i++)
{
joined += list[i].text + "\n";
if(list[i].children !== null)
{
joined += join(list[i].children);
}
}
return joined;
}
function compare(a, b)
{
return map[a.text].localeCompare(map[b.text]);
}
function parse(text)
{
var response = $.ajax(
{
url : `https://en.wikipedia.org/w/api.php?action=parse&text=${encodeURI(text).replace(/&/g, "%26")}&contentmodel=wikitext&format=json`,
async : false
});
return JSON.parse(response.responseText).parse.text["*"];
}
});
// </nowiki>