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:19, 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;
    }

    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 () {
        let result;
        
        while ((result = re.exec($('#wpTextbox1').textSelection('getContents'))) != null)
        {
            console.log(`Found: ${result[0]}`);
            console.log(`Next match starts at: ${re.lastIndex}`)
        }
    })
});
//</nowiki>