User:Bosco/Unsigned helper.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
refactor top-level structure |
fix references to object removed in previous edit |
||
| Line 10: | Line 10: | ||
if (m < 10) |
if (m < 10) |
||
m = '0' + m; |
m = '0' + m; |
||
const formattedTimestamp = `${h}:${m}, ${ts.getUTCDate()} ${ |
const formattedTimestamp = `${h}:${m}, ${ts.getUTCDate()} ${months[ts.getUTCMonth()]} ${ts.getUTCFullYear()} (UTC)`; |
||
return '\x7b\x7bsubst:' + template + '|' + user + '|' + formattedTimestamp + '\x7d\x7d'; |
return '\x7b\x7bsubst:' + template + '|' + user + '|' + formattedTimestamp + '\x7d\x7d'; |
||
} |
} |
||
| Line 48: | Line 48: | ||
pos = e.value.length; |
pos = e.value.length; |
||
txt = e.value.substr(0, pos); |
txt = e.value.substr(0, pos); |
||
txt = txt.replace(new RegExp('[\\s\\S]*\\d\\d:\\d\\d, \\d+ (' + |
txt = txt.replace(new RegExp('[\\s\\S]*\\d\\d:\\d\\d, \\d+ (' + months.join('|') + ') \\d\\d\\d\\d \\(UTC\\)'), ''); |
||
txt = txt.replace(/[\s\S]*\n=+.*=+\s*\n/, ''); |
txt = txt.replace(/[\s\S]*\n=+.*=+\s*\n/, ''); |
||
} |
} |
||
| Line 96: | Line 96: | ||
$.ajax(q); |
$.ajax(q); |
||
} else { |
} else { |
||
var t = |
var t = makeUnsignedTemplate(user, ts, template); |
||
var tt = e.value.substr(0, pos).replace(/\s*$/, ' ') + t; |
var tt = e.value.substr(0, pos).replace(/\s*$/, ' ') + t; |
||
e.value = tt + e.value.substr(pos); |
e.value = tt + e.value.substr(pos); |
||
$(e).textSelection('setSelection', { |
$(e).textSelection('setSelection', { |
||
start: tt.length }); |
start: tt.length }); |
||
appendToEditSummary(`mark unsigned [[Special:Diff/${revid}]]`); |
|||
dialog.dialog('close'); |
dialog.dialog('close'); |
||
} |
} |
||
| Line 112: | Line 112: | ||
} |
} |
||
var cb = function () { |
var cb = function () { |
||
var t = |
var t = makeUnsignedTemplate(user, ts, template); |
||
var tt = e.value.substr(0, pos).replace(/\s*$/, ' ') + t; |
var tt = e.value.substr(0, pos).replace(/\s*$/, ' ') + t; |
||
e.value = tt + e.value.substr(pos); |
e.value = tt + e.value.substr(pos); |
||
$(e).textSelection('setSelection', { |
$(e).textSelection('setSelection', { |
||
start: tt.length }); |
start: tt.length }); |
||
appendToEditSummary(`unsigned [[Special:Diff/${revid}]]`); |
|||
dialog.dialog('close'); |
dialog.dialog('close'); |
||
}; |
}; |
||
Revision as of 19:28, 15 July 2024
(function () {
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
function makeUnsignedTemplate(user, timestamp, template) {
const ts = new Date(timestamp);
let h = ts.getUTCHours();
if (h < 10)
h = '0' + h;
let m = ts.getUTCMinutes();
if (m < 10)
m = '0' + m;
const formattedTimestamp = `${h}:${m}, ${ts.getUTCDate()} ${months[ts.getUTCMonth()]} ${ts.getUTCFullYear()} (UTC)`;
return '\x7b\x7bsubst:' + template + '|' + user + '|' + formattedTimestamp + '\x7d\x7d';
}
function appendToEditSummary(newSummary) {
const editSummaryField = $("#wpSummary:first");
if (editSummaryField.length == 0) {
console.warn('Cannot find edit summary text field.');
return;
}
const oldText = editSummaryField.val().trimEnd();
// get text without trailing whitespace
let newText = "";
if (oldText.match(/[*]\/$/)) {
// check if "/* section name */" is present
newText = oldText + " " + newSummary;
} else if (oldText.length != 0) {
newText = oldText + ", " + newSummary;
} else {
newText = newSummary;
}
editSummaryField.val(newText);
}
window.unsignedHelperAddUnsignedTemplate = function(evt) {
mw.loader.using(['mediawiki.util', 'jquery.ui'], function () {
var f = document.getElementById('editform');
var e = f.elements.wpTextbox1;
var pos = $(e).textSelection('getCaretPosition', { startAndEnd: true });
var txt;
if (pos[0] != pos[1]) {
txt = e.value.substring(pos[0], pos[1]);
pos = pos[1];
} else {
pos = pos[1];
if (pos <= 0)
pos = e.value.length;
txt = e.value.substr(0, pos);
txt = txt.replace(new RegExp('[\\s\\S]*\\d\\d:\\d\\d, \\d+ (' + months.join('|') + ') \\d\\d\\d\\d \\(UTC\\)'), '');
txt = txt.replace(/[\s\S]*\n=+.*=+\s*\n/, '');
}
txt = txt.replace(/^\s+|\s+$/g, '');
var rvct = 1;
var dialog = $('<div>Examining revision 1...</div>').dialog({
buttons: {
Cancel: function () {
dialog.dialog('close');
} },
modal: true,
title: 'Adding \x7b\x7bunsigned\x7d\x7d'
});
var revid, user, ts, comment, template;
var q = {
url: mw.util.wikiScript('api'),
dataType: 'json',
type: 'POST',
data: {
format: 'json',
action: 'query',
titles: mw.config.get('wgPageName'),
prop: 'revisions',
rvprop: 'ids|timestamp|user|parsedcomment|content',
rvlimit: 1,
rawcontinue: 1
},
success: function (r, sts, xhr) {
if (!dialog.dialog('isOpen'))
return;
if (!r.query || !r.query.pages) {
dialog.html('<span style="color:red"><b>Error:</b> Bad response from API</span>');
if (window.console && typeof (window.console.error) == 'function')
window.console.error("Bad response", r);
return;
}
for (var k in r.query.pages) {
var rr = r.query.pages[k].revisions[0];
var cont = function () {
if (r['query-continue'] && r['query-continue'].revisions) {
dialog.html('Evaluating revision ' + (++rvct) + '...');
q.data.rvcontinue = r['query-continue'].revisions.rvcontinue;
$.ajax(q);
} else {
var t = makeUnsignedTemplate(user, ts, template);
var tt = e.value.substr(0, pos).replace(/\s*$/, ' ') + t;
e.value = tt + e.value.substr(pos);
$(e).textSelection('setSelection', {
start: tt.length });
appendToEditSummary(`mark unsigned [[Special:Diff/${revid}]]`);
dialog.dialog('close');
}
};
if (typeof (rr['*']) != 'undefined' && rr['*'].indexOf(txt) < 0) {
if (!user) {
dialog.html('<span style="color:red"><b>Error:</b> Text was not found in the starting revision! Did you edit it?</span>');
return;
}
var cb = function () {
var t = makeUnsignedTemplate(user, ts, template);
var tt = e.value.substr(0, pos).replace(/\s*$/, ' ') + t;
e.value = tt + e.value.substr(pos);
$(e).textSelection('setSelection', {
start: tt.length });
appendToEditSummary(`unsigned [[Special:Diff/${revid}]]`);
dialog.dialog('close');
};
if (/reverted|undid/i.test(comment)) {
var dialog2 = $('<div>')
.append(
'The ',
$('<a>').prop({
href: '/w/index.php?diff=prev&oldid=' + revid, target: '_blank' }).text('found revision'),
' may be a revert: ',
comment
)
.dialog({
title: "Possible revert!",
modal: true,
buttons: {
"Use that revision": function () {
dialog2.dialog('close');
cb();
},
"Keep looking": function () {
dialog2.dialog('close');
cont();
},
"Cancel": function () {
dialog2.dialog('close');
dialog.dialog('close');
},
}
});
} else {
cb();
}
} else {
revid = rr.revid;
user = rr.user;
ts = rr.timestamp;
comment = rr.parsedcomment;
if (typeof (rr.anon) !== 'undefined') {
template = 'Unsigned IP';
} else if (typeof (rr.temp) !== 'undefined') {
// Seems unlikely "temporary" users will have a user page, so this seems the better template for them for now.
template = 'Unsigned IP';
} else {
template = 'Unsigned';
}
cont();
}
return;
}
dialog.html('<span style="color:red"><b>Error:</b> No revisions found in the page!</span>');
},
error: function (xhr, textStatus, errorThrown) {
if (!dialog.dialog('isOpen'))
return;
dialog.html('<span style="color:red"><b>Error:</b> ' + textStatus + ' ' + errorThrown + '</span>');
}
};
if (f.elements.baseRevId)
q.data.rvstartid = f.elements.baseRevId.value;
$.ajax(q);
});
evt.preventDefault();
evt.stopPropagation();
return false;
}
if (!window.charinsertCustom)
window.charinsertCustom = {};
if (!window.charinsertCustom['Insert'])
window.charinsertCustom['Insert'] = '';
window.charinsertCustom['Insert'] += ' \x7b\x7bunsigned\x7d\x7d\x10unsignedHelperAddUnsignedTemplate';
if (!window.charinsertCustom['Wiki markup'])
window.charinsertCustom['Wiki markup'] = '';
window.charinsertCustom['Wiki markup'] += ' \x7b\x7bunsigned\x7d\x7d\x10unsignedHelperAddUnsignedTemplate';
if (window.updateEditTools)
window.updateEditTools();
})();