Jump to content

User:Sohom Datta/mas.js

From Meta, a Wikimedia project coordination wiki

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// This is a script to test
// matchandsplit.toolforge.org.
// This is alpha software and should be
// treated as such.
// Please check the output of the
// script+bot carefully and do not think
// anything for granted. Ping Sohom/Sodium
// if anything seems off.

function showMatchAndSplit() {
	if ( mw.config.get('wgWikiFamily') !== 'wikisource' ) {
		return;
	}

    const url = new URL(window.location.href);

    let lang = url.hostname.split('.')[0];

    if ( url.hostname.startsWith( 'wikisource' ) ) {
		lang = 'old';
	}

    if ( mw.config.get('wgNamespaceNumber') !== 0 ) {
        return;
    }

    const matchPortlet = mw.util.addPortletLink(
		'p-cactions',
        '#',
		'Run match on page [EXPERIMENTAL] ',
		'ca-match',
	);

    matchPortlet.addEventListener('click', async function() {
        await fetch('https://tools.wmflabs.org/matchandsplit/match', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                lang: lang,
                title: mw.config.get('wgPageName'),
                username: mw.config.get( 'wgUserName' ),
            }),
        });
    });

    const splitPortlet = mw.util.addPortletLink(
		'p-cactions',
        '#',
		'Run split on page [EXPERIMENTAL]',
		'ca-split',
	);

    splitPortlet.addEventListener('click', async function() {
        await fetch('https://tools.wmflabs.org/matchandsplit/split', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                lang: lang,
                title: mw.config.get('wgPageName'),
                username: mw.config.get( 'wgUserName' ),
            }),
        });
    });
}

window.addEventListener( 'load', function() {
    mw.loader.using( 'mediawiki.util', showMatchAndSplit );
} );