Jump to content

User:MusikAnimal/dablinkwarn.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MusikAnimal (talk | contribs) at 02:36, 11 August 2021 (MusikAnimal moved page User:MusikAnimal/dablink.js to User:MusikAnimal/dablinkwarn.js without leaving a redirect). 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.
( function ( mw, $ ) {
    var $wpTextbox1 = $( '#wpTextbox1' ),
        api = new mw.Api();

    function addListeners() {
        $wpTextbox1.on('keyup', function (e) {
            if (e.key === ']') {
                var context = $wpTextbox1.val().substring(0, e.selectionStart),
                    matches = context.match(/\[\[(.*?)]]/);

                if (matches && matches.length) {
                    var pageTitle = matches[1];
                    api.get({
                        action: 'query',
                        titles: matches[1],
                        prop: 'pageprops',
                        ppprop: 'disambiguation',
                        list: 'prefixsearch',
                        pssearch: pageTitle,
                        pslimit: 5,
                        formatversion: 2,
                    }).then(function (resp) {
                        if (resp.query.pages[0].pageprops.disambiguation === '') {
                            var alternatives = resp.query.prefixsearch.map(function (elem) {
                                    return elem.title;
                                }).filter(function (title) {
                                    return title !== pageTitle;
                                }),
                                $container = $('<div>'),
                                $helpLink = $('<a>').prop('href', new mw.Title('Disambiguation', 4).getUrl())
                                    .text('disambiguation page'),
                                $pageLink = $('<a>').prop('href', new mw.Title(pageTitle).getUrl())
                                    .text(pageTitle),
                                $message = $('<p>').append('You linked to the page "')
                                    .append($pageLink)
                                    .append('" which is a ')
                                    .append($helpLink)
                                    .append('. ');
                            $container.append($message);

                            if (alternatives.length) {
                                $message.append('Did you mean one of the following?');
                                var $links = alternatives.map(function (title) {
                                        return $('<a>').prop('href', new mw.Title(title).getUrl())
                                            .text(title);
                                    }),
                                    $altList = $('<ul>');
                                $links.forEach($link => {
                                    $altList.append(
                                        $('<li>').append($link)
                                    );
                                });
                                $container.append($altList);
                            }

                            mw.notify($container);
                        }
                    });
                }
            }
        });
    }

    // TODO: only do this for inexperienced editors
    if (mw.config.get('wgNamespaceNumber') === 0) {
        addListeners();
    }
} )( mediaWiki, jQuery );