Jump to content

User:Weeklyd3/scripts/revert.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Weeklyd3 (talk | contribs) at 21:26, 6 June 2022 (A script to revert edits (wrote it myself)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
(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);
    });
    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=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.user;
        t.textContent = `User to revert: ${p}, getting token`;
    })
    .then(function() {
        return fetch(`${apiURL}?action=query&meta=tokens&type=rollback&format=json`, {
            credentials: 'include'
        });
    })
    .then(function(res) {
        return res.json();
    })
    .then(function(json) {
        globalThis.token = json.query.tokens.rollbacktoken;
    })
    .then(function() {
        var additional;
        t.textContent = 'Reverting edit...';
        if (defaultSummary) additional = encodeURIComponent(`Revert edits by [[User:${p}|${p}]] ([[user talk:${p}|talk]]): `);
        else additional = '';
        const baudy = new FormData();
        baudy.set('token', globalThis.token);
    return fetch(`${apiURL}?action=rollback&summary=${additional}${encodeURIComponent(reason)}&title=${currentPageName}&origin=${location.origin}&user=${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);
    });
}