User:Sav/cannedsumm.js

From Test Wiki
Revision as of 14:26, 13 June 2024 by Sav (talk | contribs) (Created page with "var cannedSummaries = window.localStorage.getItem('cannedSummaries') || ''; function done(settings) { var summs = settings.split(';;'); var cbs = document.querySelector('.editCheckboxes'); var choose = document.getElementById('cannedsumm-chooseelem'); if (choose) choose.remove(); choose = document.createElement('select'); choose.id = 'cannedsumm-chooseelem'; var label = document.getElementById('wpSummaryLabel'); label.addEventListener('cl...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
var cannedSummaries = window.localStorage.getItem('cannedSummaries') || '';
function done(settings) {
    var summs = settings.split(';;');
    var cbs = document.querySelector('.editCheckboxes');
    var choose = document.getElementById('cannedsumm-chooseelem');
    if (choose) choose.remove();
    choose = document.createElement('select');
    choose.id = 'cannedsumm-chooseelem';

    var label = document.getElementById('wpSummaryLabel');
    label.addEventListener('click',function(e){askForSummary("Add an edit summary below.");});

    var choosesum = document.createElement('option');
    choosesum.id = 'cannedsumm-choose';
    choosesum.innerHTML = 'Choose a canned summary';
    choose.appendChild(choosesum);

    summs.filter(function(i){
        return i.trim() !== '';
    }).forEach(function(i){
        var summ = document.createElement('option');
        summ.title = 'Insert canned edit summary';
        summ.innerHTML = i;
        choose.appendChild(summ);
    });
    var newsum = document.createElement('option');
    newsum.id = 'cannedsumm-new';
    newsum.innerHTML = 'Add new summary...';
    choose.appendChild(newsum);
    var delsum = document.createElement('option');
    delsum.id = 'cannedsumm-del';
    delsum.innerHTML = 'Delete all your summaries';
    choose.appendChild(delsum);
    choose.addEventListener('change', function() {
        var chosen = choose.options[choose.selectedIndex];
        switch (chosen.id) {
            case 'cannedsumm-new':
                askForSummary("Add an edit summary below.");
                break;
            case 'cannedsumm-choose':
                break;
            case 'cannedsumm-del':
                cannedSummaries = '';
                window.localStorage.removeItem('cannedSummaries');
                done(cannedSummaries);
                break;
            default:
                document.getElementById('wpSummary').value = chosen.innerHTML + ') ([[User:JJBullet/Project Guides/Canned Edit Summaries|canned edit summary]]';
                document.getElementById('wpSave').click();
        }
    });
    var wrapper = document.createElement('div');
    wrapper.style.marginTop = '1em';
    wrapper.appendChild(choose);
    cbs.appendChild(wrapper);
}
function askForSummary(message) {
    var dim = document.createElement('div');
    dim.style.width = '100%'; dim.style.height = '100%';
    dim.style.position = 'fixed';
    dim.style.left = '0'; dim.style.top = '0';
    dim.style.backgroundColor = 'rgba(0,0,0,0.5)';
    dim.style.zIndex = '800';

    var box = document.createElement('div');
    box.style.borderRadius = '5px';
    box.style.backgroundColor = '#f0f0f0';
    box.style.border = '1px solid #c0c0c0';
    box.style.width = '20em';
    box.style.position = 'fixed';
    box.style.left = '45%';
    box.style.top = '45%';
    box.style.zIndex = '801';
    box.style.padding = '1em';

    var content = document.createElement('p');
    content.innerHTML = message + ' ';
    var dismiss = document.createElement('a');
    dismiss.href = '#';
    dismiss.onclick = function(){
        dim.style.display = 'none';
        box.style.display = 'none';
        done(cannedSummaries);
        return false;
    };
    dismiss.innerHTML = '(dismiss)';
    content.appendChild(dismiss);

    var input = document.createElement('input');
    input.type = 'text';
    input.style.width = '19em';
    input.style.margin = '1em';
    input.placeholder = 'Enter an edit summary to can';
    input.onkeypress = function(e){
        if (!e) e = window.event;
        if (e.key == 'Enter') {
            cannedSummaries += this.value + ';;';
            window.localStorage.setItem('cannedSummaries', cannedSummaries);
            dismiss.click();
        }
    };

    var del = document.createElement('a');
    del.href = '#';
    del.style.color = 'red';
    del.innerHTML = 'Delete all of your canned edit summaries';
    del.onclick = function(){
        cannedSummaries = '';
        window.localStorage.removeItem('cannedSummaries');
        dismiss.click();
        return false;
    };

    box.appendChild(content);
    box.appendChild(input);
    box.appendChild(del);

    document.body.appendChild(dim);
    document.body.appendChild(box);
}
window.addEventListener('load', function(){
if (['edit', 'submit'].includes(mw.config.get('wgAction')) && !(window.location.href.includes('section=new'))) {
    done(cannedSummaries);
}
});
console.log('Loaded Canned Edit Summaries');