User:Weeklyd3/scripts/revert.js
Appearance
< User:Weeklyd3 | scripts
(Redirected from User:Weeklyd3/revert.js)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:Weeklyd3/scripts/revert. |
(function() {
const currentPageName = (mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber")].length ? (mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber")] + ":") : "")
+ mw.config.get("wgTitle");
console.log(`Running on [[${currentPageName}]]`);
globalThis.currentPageName = currentPageName;
var apiURL;
if (location.href.includes('/w')) apiURL = '/w/api.php';
else if (location.href.includes('/index.php')) apiURL = 'api.php';
globalThis.apiURL = apiURL;
if (document.querySelector('table.diff')) {
// Probably a diff page
if (!document.querySelector('#mw-diff-ntitle1 strong a').textContent.startsWith('Latest revision')) return console.warn('Not looking at diff w/ latest version.');
const buttons = document.createElement('div');
buttons.classList.add('action-buttons');
const VANDAL = document.createElement('button');
VANDAL.textContent = 'Vandalism?';
VANDAL.addEventListener('click', function() {
roll('[[Project:Vandalism|Vandalism]]', this);
});
buttons.appendChild(VANDAL);
const TEST = document.createElement('button');
TEST.textContent = 'Test edit?';
TEST.addEventListener('click', function() {
roll('[[Project:Test edits|Test edits]]', this);
});
buttons.appendChild(TEST);
const UNSOURCED = document.createElement('button');
UNSOURCED.textContent = 'Poorly sourced edit?';
UNSOURCED.addEventListener('click', function() {
roll('Not [[Project:Verifiability|verifiable]] via [[Project:RS|reliable sources]]', this);
});
buttons.appendChild(UNSOURCED);
const COPYVIO = document.createElement('button');
COPYVIO.textContent = 'Copyright violation?';
COPYVIO.addEventListener('click', function() {
this.parentNode.appendChild(document.createTextNode('Please be sure to ask for revision deletion later.'));
roll('[[Project:Copyright violations|Copyright violation]]', this);
});
buttons.appendChild(COPYVIO);
const custom = document.createElement('button');
custom.textContent = 'Something else';
custom.addEventListener('click', function() {
const reason = prompt(`Why are you rolling back this edit?\n[An empty reason or CANCEL aborts the rollback.]`);
if (!reason) return;
roll(reason, this);
});
buttons.appendChild(custom);
const br = document.createElement('br');
buttons.appendChild(br);
document.querySelector('#mw-diff-ntitle1').after(buttons);
} else {
console.warn('Not looking at diff page.');
}
})();
function disableAllActionButtons() {
const buttons = document.querySelectorAll('.action-buttons button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].disabled = 'disabled';
}
}
function enableAllActionButtons() {
const buttons = document.querySelectorAll('.action-buttons button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].disabled = '';
}
}
function roll(reason, t, defaultSummary = true) {
t.disabled = 'disabled';
console.log(t.textContent);
t.setAttribute('data-orig', t.textContent);
t.textContent = 'Reverting (getting history of page)...';
disableAllActionButtons();
fetch(`${apiURL}?action=query&format=json&prop=revisions&titles=${encodeURIComponent(currentPageName)}&rvprop=ids|user&rvlimit=1`)
.then(function(js) {
return js.json();
})
.then(function(json) {
var page = json.query.pages[Object.keys(json.query.pages)[0]].revisions[0];
globalThis.p = page.revid;
globalThis.user = page.user;
t.textContent = `ID to revert: ${p}, getting token`;
})
.then(function() {
return fetch(`${apiURL}?action=query&meta=tokens&format=json`, {
credentials: 'include'
});
})
.then(function(res) {
return res.json();
})
.then(function(json) {
globalThis.token = json.query.tokens.csrftoken;
})
.then(function() {
var additional;
t.textContent = 'Reverting edit...';
if (defaultSummary) additional = encodeURIComponent(`Revert edits by [[User:${user}|${user}]] ([[user talk:${user}|talk]]): `);
else additional = '';
const baudy = new FormData();
baudy.set('token', globalThis.token);
return fetch(`${apiURL}?action=edit&summary=${additional}${encodeURIComponent(reason + " ([[User:weeklyd3/revert|weeklyd3's revert script]])")}&title=${currentPageName}&origin=${location.origin}&undo=${globalThis.p}&format=json`, {
"credentials": 'include',
'method': 'POST',
'body': baudy
});
})
.then(function(res) {
return res.json();
})
.then(function(js) {
console.log(js);
if (js.error) {
mw.notify(js.error.info, {title: `Aww man. Error:\n${js.error.code}`});
t.textContent = t.getAttribute('data-orig');
t.removeAttribute('data-orig');
enableAllActionButtons();
return;
}
t.textContent = 'Rollback completed, reloading page in a second...';
setTimeout(function() { location.reload(); }, 1000);
});
}