User:Σ/Testing facility/Archiver.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:Σ/Testing facility/Archiver. |
$(document).ready(function() {
if (mw.config.get("wgNamespaceNumber") % 2 == 0 && mw.config.get("wgNamespaceNumber") != 4)
// not a talk page and not project namespace
return;
$("head").append("<style>a.arkylink { font-weight:bold } .arkyhighlight { background-color:#D9E9FF }</style>");
var validSections = new Object();
var fromTos = new Object();
var wikiText = "";
var archiveButton = mw.util.addPortletLink("p-cactions", "#", "ØCA", "pt-oeca", "Archive all the selected threads", null, null);
$(archiveButton).click(function(e) {
var archiveTarget;
var sections, archiveThis;
var cutOffset, revisedPage;
function cut(s, start, end) {
return s.substr(0, start) + s.substring(end);
}
cutOffset = 0;
revisedPage = wikiText;
archiveTarget = prompt("Where should we move the threads to?");
sections = $("a.arkylink").map(function() {
return $(this).attr("data-section");
});
sections.each(function(i, n) {
revisedPage = cut(
revisedPage,
fromTos[n][0] - cutOffset,
fromTos[n][1] - cutOffset
);
cutOffset += fromTos[n][1] - fromTos[n][0];
});
archiveThis = sections.map(function() {
return wikiText.substring(fromTos[this][0], fromTos[this][1]);
}).toArray().join("");
console.log("archive this:" + archiveThis);
console.log("revised page:" + revisedPage);
if (1) new mw.Api().postWithToken("csrf", {action: 'edit', title: mw.config.get("wgPageName"), text: revisedPage, summary: "Remove threads from talk page"}).done(function(res1) {
alert("Successfully removed threads from talk page");
console.log(res1);
new mw.Api().postWithToken("csrf", {action: 'edit', title: archiveTarget, appendtext: archiveThis, summary: "Put threads into archive page"}).done(function(res2) {
alert("Successfully added threads to archive page");
console.log(res2);
window.location.reload();
} );
} );
});
new mw.Api().get({action: 'parse', page: mw.config.get("wgPageName")}).done(function(dataShit) {
new mw.Api().get({action: 'query', pageids: mw.config.get("wgArticleId"), prop: ['revisions'], rvprop: 'content', rawcontinue: ''}).done(function(shit) { wikiText = shit.query.pages[mw.config.get("wgArticleId")].revisions[0]["*"] });
$(dataShit.parse.sections)
.filter(function(i, s) { return s.index == parseInt(s.index) })
.each(function(i, s) { validSections[s.index] = s });
for (var i in validSections) {
i = parseInt(i);
fromTos[i] = [
validSections[i].byteoffset,
validSections.hasOwnProperty(i+1)?validSections[i+1].byteoffset:Infinity
];
}
$("#mw-content-text").find(":header").find("span.mw-headline").each(function(i, title) {
var header, editSection, sectionNumber;
header = $(this).parent();
editSection = header.find(".mw-editsection"); // 1st child
sectionNumber = header.find(".mw-editsection a:first");
if (sectionNumber[0]) {
sectionNumber = sectionNumber.attr("href").match(/§ion=(\d+)/);
if (sectionNumber)
sectionNumber = sectionNumber[1];
} else // eg <h2>not a real section</h2>
sectionNumber = undefined;
if (validSections.hasOwnProperty(sectionNumber))
editSection[0].innerHTML += " <span class=mw-editsection-bracket>[</span><a data-section=" + sectionNumber + " " + "onclick=$(this).parent().parent().toggleClass('arkyhighlight');$(this).toggleClass('arkylink');>archive</a><span class=mw-editsection-bracket>]</span>";
});
});
});