User:DaxServer/VisualEditorEverywhere.js

Source: Wikipedia, the free encyclopedia.
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.
//<nowiki>
mw.hook( 'wikipage.content' ).add( function () {
    // Disable on namespaces: main, user
    if ([0, 2].includes(mw.config.get('wgNamespaceNumber'))) {
        return;
    }

    let articleName = mw.config.get('wgPageName');
	articleName = encodeURIComponent(articleName); // fix bug involving & not getting converted to &amp;
	let buttonIsPresent = $('#ca-ve-edit').length;
	let pageIsUserScript = articleName.match(/(?:\.js|\.css)$/);

    if (buttonIsPresent || pageIsUserScript) {
        return;
    }

    // Insert Edit tab at top of page

    let htmlToInsert;
    let existingEdit = $('#ca-edit span').text();

    if ('Edit' === existingEdit) {
        htmlToInsert = '<li id="ca-ve-edit" class="collapsible"><a href="/w/index.php?title='+articleName+'&amp;veaction=editsource" title="Edit this page [alt-shift-v]" accesskey="v">Edit source</a></li>';
        $('#ca-edit').after(htmlToInsert);
    } else {
        htmlToInsert = '<li id="ca-ve-edit" class="collapsible"><a href="/w/index.php?title='+articleName+'&amp;veaction=edit" title="Edit this page [alt-shift-v]" accesskey="v">Edit</a></li>';
        $('#ca-edit').before(htmlToInsert);
    }

    $('#ca-ve-edit').show();

    // Insert [ edit ] by each section
    $('.mw-editsection').each(function(i) {
        if ('Edit' === existingEdit) {
            // Add "edit source"
            htmlToInsert = '<span class="mw-editsection-divider"> | </span>    <a href="/w/index.php?title='+articleName+'&amp;veaction=editsource&amp;section='+(i+1)+'" class="mw-editsection-visualeditor">edit source</a>';
            $('.mw-editsection').eq(i).children('span:last-of-type').before(htmlToInsert);
        } else {
            // Add "edit"
            htmlToInsert = '<a href="/w/index.php?title='+articleName+'&amp;veaction=edit&amp;section='+(i+1)+'" class="mw-editsection-visualeditor">edit</a>    <span class="mw-editsection-divider"> | </span>';
            $('.mw-editsection').eq(i).children('span:first-of-type').after(htmlToInsert);
        }
    });

    $('.mw-editsection-visualeditor, .mw-editsection-divider').show();
});
//</nowiki>