MediaWiki:Gadget-autosign.js
Vai alla navigazione
Vai alla ricerca
Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.
- Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
- Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
- Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
- Opera: premi Ctrl-F5.
// pads a time to 2 digits
function padtime(time) {
return (time + '').padStart(2, '0');
}
$(function() {
'use strict';
// only run when editing
if (mw.user.options.get('usebetatoolbar') &&
/edit|submit/.test(mw.config.get('wgAction'))) {
// wait on the API library
mw.loader.using('mediawiki.api').then(function() {
// fetch the username and timestamp of the last revision
new mw.Api().get({
action: 'query',
titles: mw.config.get('wgPageName'),
prop: 'revisions',
rvprop: 'user|timestamp',
formatversion: 2
}).done(function(data) {
var page = data.query.pages[0];
// if the revision is missing, skip
if (page.missing) {
return;
}
// fetch data from the revision
var revision = page.revisions[0];
var user = revision.user;
var date = new Date(revision.timestamp);
// on the odd chance it fails to parse the date, skip date
var insert;
if (isNaN(date.getTime())) {
insert = '{{subst:'+'unsigned|'+user+'}}';
} else {
// wish there was an easier way to do this, I miss moment.js
var timestamp = padtime(date.getUTCHours())+':'+padtime(date.getUTCMinutes())+', '+date.getUTCDate()+' '+date.toLocaleDateString('en-us', {month:'long', timeZone:'UTC'})+' '+date.getUTCFullYear();
insert = '{{subst:'+'unsigned|'+user+'|'+timestamp+'}}';
}
// add the editor button
mw.loader.using('ext.wikiEditor').then(function() {
$('#wpTextbox1').wikiEditor('addToToolbar', {
section: 'advanced',
group: 'insert',
tools: {
autosign: {
label: 'Firma automaticamente commento precedente',
type: 'button',
icon: 'https://upload.wikimedia.org/wikipedia/commons/b/b3/Insert-signature.svg',
action: {type: 'replace', options: {pre: insert}}
}
}
});
});
});
});
}
});