Jump to content

User:DaxServer-test/test.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DaxServer-test (talk | contribs) at 10:25, 21 October 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 () {
    let articleName = mw.config.get('wgPageName');
	articleName = encodeURIComponent(articleName); // fix bug involving & not getting converted to &amp;
	let pageIsSandbox = articleName.match(/sandbox$/);

    // Only on main namespace or sandbox
    // Only initiate in edit or submit mode
    if (
            ! [0, 2].includes(mw.config.get('wgNamespaceNumber'))
        ||  ! pageIsSandbox
        ||  ! ['edit', 'submit'].includes(mw.config.get('wgAction'))
    ) {
        return;
    }

    $.when(
        mw.loader.using( [ 'mediawiki.util' ] ),
        $.ready
    ).then( function () {
        let node = mw.util.addPortletLink('p-tb', '#', 'Book to Sfn', 'ds-book-to-sfn', 'Convert cite book to Sfn');
        let re = /<ref(?: name="\w+")?>({{cite book\|[|\w\s=?\-–&'#.:+\/]*}})<\/ref>/img;

        $( node ).click(function (e) {
            let content = $('#wpTextbox1').textSelection('getContents');
            let result;

            console.log(re.test(content));

            while ((result = re.exec()) != null)
            {
                console.log(`Found: ${result[0]}`);
                console.log(`Next match starts at: ${re.lastIndex}`)
            }
            
            e.preventDefault();
        });
    } );
});
//</nowiki>