Nothing Special   »   [go: up one dir, main page]

User:Equazcion/OneClickArchiver.js: Difference between revisions

Content deleted Content added
No edit summary
Stop requesting inprop=edit when tokens are got from mw.user.tokens.get("csrfToken")
 
(46 intermediate revisions by 5 users not shown)
Line 1:
if ((($('#ca-addsection').length > 0) || (mw.config.get('wgPageName') == 'Wikipedia:Administrators\'_noticeboard/Edit_warring')) && (mw.config.get('wgAction') == 'view')){
$('span.mw-headline').each(function(index, value){
$('span.mw-headline').each(function(index, value){
var section = index;
if var editSectionUrl = ($(this).parent().propfind("tagName") =='.mw-editsection a:first').attr('H2href'){;
var sectionTitlesectionReg = $/&section=(this).html(*)/;
var sectionRaw = sectionReg.exec(editSectionUrl);
if ((sectionRaw != null) && (sectionRaw[1].indexOf('T') < 0)){
$(this).parent('h2').append('<a id="' + section + '" href="#archiverLink" class="archiverLink">' + 'Archive' + '</a>');
$(this).parent('h2').find('a.archiverLink').click(function(){
var request4section = {parseInt(sectionRaw[1]);
action:"query",
if ($(this).parent().prop("tagName") == 'H2'){
titles: wgPageName,
prop: "revisions|info",
$(this).parent('h2').append(' <div style="font-size:.6em;font-weight:bold;float:right"> | <a id="' + section +
intoken: "edit",
'" href="#archiverLink" class="archiverLink">' + 'Archive' + '</a></div>');
rvprop: "content",
indexpageids: 1,
dataType: "xml",
format: "xml"
};
$.get(mw.config.get("wgScriptPath")+"/api.php", request4, function(response4){
var content = $(response4).find('rev').text();
var regexObject3 = new RegExp('\\|counter ?= ?(\\d+)');
var counter = regexObject3.exec(content);
var archiveNum = counter[1];
var archiveNameReg = /\|archive ?= ?(.*)\%\(counter\)d/;
var archiveNameRegMatch = archiveNameReg.exec(content);
var archiveName = archiveNameRegMatch[1];
var regexObject2 = new RegExp( '((^|\n)={1,5} ?.*[\\s\\S]*?)\n={1,5}' , 'g');
var matches = [];
while (match = regexObject2.exec(content)) {
matches.push(match[1]);
regexObject2.lastIndex = regexObject2.lastIndex - 5;
}
if (matches != null){
var contentMatch = matches[section];
$(this).parent('h2').find('a.archiverLink').click(function(){
var nextReg = /^={2}/;
var next = section + 1;
while (nextReg.exec(matches[next]) != null){
next = next + 1;
contentMatch = contentMatch + matches[next];
//window.open("data:text/html," + encodeURIComponent(contentMatch), "_blank", "width=200,height=100");
}
}
var request1 = {
action:"edit",
title: archiveName + archiveNum,
appendtext: contentMatch,
summary: 'adding ' + sectionTitle,
token: mw.user.tokens.get("editToken")
};
 
$.post(mw.config.get("wgScriptPath")+"/api.php", request1, function(response1){
var mHeaders = '<span style="color:#454545;">Retrieving headers...</span>';
var request2 = {
var mSection = 'retrieving section content...';
action:"edit",
var mPosting = '<span style="color:#004000">Content retrieved,</span> performing edits...';
section: section + 1,
var mPosted = '<span style="color:#008000">Archive appended...</span>';
title: wgPageName,
var mCleared = '<span style="color:#008000">Section cleared...</span>';
text: "",
var mReloading = '<span style="color:#00008C">All done! </span>Reloading...';
summary: '[[User:Equazcion/OneClickArchiver|OneClickArchiver]] archived ' + sectionTitle,
token: mw.user.tokens.get("editToken")
$('body').append('<div class="overlay" style="background-color:#000;opacity:.4;position:fixed;' +
'top:0;left:0;width:100%;height:100%;z-index:500;"></div>');
$('body').prepend('<div class="arcProg" style="font-weight:bold;box-shadow: 7px 7px 5px #000;font-size:0.9em;line-height:1.5em;' +
'z-index:501;opacity:1;position:fixed;width:50%;left:25%;top:30%;background:#F7F7F7;border:#222 ridge 1px;padding:20px;"></div>');
$('.arcProg').append('<div>' + mHeaders + '</div>');
var request4 = {
action:"query",
titles: mw.config.get('wgPageName'),
rvsection: 0,
prop: "revisions|info",
rvprop: "content",
indexpageids: 1,
dataType: "xml",
format: "xml"
};
$.postget(mw.config.get("wgScriptPath")+"/api.php", request2request4, function(response1response4){
var content = $(response4).find('rev').text();
var regexObject3 = new RegExp('\\|counter ?= ?(\\d+)');
location.reload();
var counter = regexObject3.exec(content);
if ((counter == null) || (typeof counter == 'undefined')){
$('.arcProg').remove();
$('.overlay').remove();
alert("No archive counter was detected on this page, so archiving was aborted. See User:Equazcion/OneClickArchiver for details.");
} else {
var archiveNum = counter[1];
var archiveNameReg = /(\| *archive *= *.*\%\(counter\)d.*?) *(-->)?/;
var archiveNameRegMatch = archiveNameReg.exec(content);
if ((archiveNameRegMatch == null) || (typeof archiveNameRegMatch == 'undefined')){
$('.arcProg').remove();
$('.overlay').remove();
alert("No archive name was detected on this page, so archiving was aborted. See User:Equazcion/OneClickArchiver for details.");
} else {
var monthNames = ["january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december"];
var shortMonthNames = ["jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec"];
var year = new Date().getFullYear();
var month = new Date().getMonth();
var archiveName = archiveNameRegMatch[1]
.replace(/\|archive ?= ?/, '')
.replace(/\%\(year\)d/g, year)
.replace(/\%\(month\)d/g, month)
.replace(/\%\(monthname\)s/g, monthNames[month])
.replace(/\%\(monthnameshort\)s/g, shortMonthNames[month])
.replace(/\%\(counter\)d/g, archiveNum);
//https://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Albert_Einstein&rvprop=size&format=xml
$('.arcProg').append('<div>' + 'Archive name <span style="font-weight:normal;color:#003366;">' + archiveName +
'</span> <span style="darkgreen">found</span>, ' + mSection + '</div>');
var request5 = {
action:"query",
titles: mw.config.get('wgPageName'),
rvsection: section,
prop: "revisions",
rvprop: "content",
indexpageids: 1,
dataType: "xml",
format: "xml"
};
$.get(mw.config.get("wgScriptPath")+"/api.php", request5, function(response5){
$('.arcProg').append('<div>' + mPosting + '</div>');
var contentSection = '\r\r' + $(response5).find('rev').text();
var request1 = {
action:"edit",
title: archiveName,
appendtext: contentSection,
summary: '[[User:Equazcion/OneClickArchiver|OneClickArchiver]] adding 1 discussion',
token: mw.user.tokens.get("csrfToken")
};
var request2 = {
action:"edit",
section: section,
title: mw.config.get('wgPageName'),
text: "",
summary: '[[User:Equazcion/OneClickArchiver|OneClickArchiver]] archived 1 discussion to [[' + archiveName + ']]',
token: mw.user.tokens.get("csrfToken")
};
$.when(
$.post(mw.config.get("wgScriptPath")+"/api.php", request1, function(response1){
$('.arcProg').append('<div class="archiverPosted">' + mPosted + '</div>'); }),
$.post(mw.config.get("wgScriptPath")+"/api.php", request2, function(response2){
$('.arcProg').append('<div class="archiverCleared">' + mCleared + '</div>'); })
).done(function() {
$('.arcProg').append('<div>' + mReloading + '</div>');
location.reload();
});
});
}
}
});
});
});
}
});
}
});