Jump to content

User:DaxServer/CiteWikiLinker.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DaxServer (talk | contribs) at 11:06, 24 February 2022. 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>

/**
 * –––––
 *       YOU ARE FULLY RESPONSIBLE FOR PUBLISHING EDITS USING THIS SCRIPT
 * –––––
 *
 * This script is not ready for use, unless of course, if you know what you are doing.
 * You must verify if the conversion is successful and modify if not.
 */

const regexes = [
    ['www\.deccanchronicle\.com', 'Deccan Chronicle'],
    ['www\.deccanherald\.com', 'Deccan Herald'],
    ['www\.hindustantimes\.com', 'Hindustan Times'],
    ['www\.indiatoday\.in', 'India Today'],
    ['www\.news18\.com', 'News18'],
    ['economictimes\.indiatimes\.com', 'The Economic Times'],
    ['www\.thehindu\.com', 'The Hindu'],
    ['indianexpress\.com', 'The Indian Express'],
    ['www\.newindianexpress\.com', 'The New Indian Express'],
    ['www\.thenewsminute.\com', 'The News Minute'],
    ['www\.thequint\.com', 'The Quint'],
    ['www\.siasat\.com', 'The Siasat Daily'],
    ['www\.telegraphindia\.com', 'www\.telegraphindia\.com', 'The Telegraph (India)|The Telegraph'],
    ['timesofindia\.indiatimes\.com', 'The Times of India'],
    ['www\.uiowa\.edu', 'University of Iowa'],
    ['www\.washingtonpost\.com', 'Washington Post'],
    ['movies\.yahoo\.com', 'Yahoo! Movies'],
    ['zeenews\.india\.com', 'Zee News'],
    ...(window.ds_citewikilinker_regexes || [])
];

const refPartStart = '(<ref(?: name=":?\\w+")?>{{cite (?:news|web)'
const refPartEnd = '}}<\\/ref>)'
const citePart = '[|\\w\\s=?-–-&’\'#.:;+,%\\/[\\]()]*'
const urlPart = 'url ?= ?https?:\/\/'

$.when(
    $.ready
).then(function () {
    // Activate portlet when VE source editor is enabled
    mw.hook( 've.activationComplete' ).add(function () {
        // Remove portlet when VE visual editor is enabled
        if (0 === $('.ve-ui-surface-source').length) {
            $('#ds-cite-wikilinker').remove()

            return
        }

        $.when(
            mw.loader.using( [ 'mediawiki.util' ] )
        ).then( function () {
            main()
        })
    })

    // Remove portlet when VE is deactivated
    mw.hook( 've.deactivationComplete' ).add(function () {
        $('#ds-cite-wikilinker').remove()
    })

    function main() {
        const node = mw.util.addPortletLink('p-tb', '#', 'Cite WikiLinker', 'ds-cite-wikilinker', 'Wikilink publishers in citations')

        $( node ).click(function (e) {
            let textBox = $('#wpTextbox1')
            let content = textBox.textSelection('getContents')
            let re1, re2, changes = false;

            for (const i of regexes) {
                re1 = new RegExp(`${refPartStart}${citePart}${urlPart}${i[0]}${citePart}(?:website|work) ?= ?)${i[1]}(${citePart}${refPartEnd}`, 'gim')
                re2 = new RegExp(`${refPartStart}${citePart}(?:website|work) ?= ?)${i[1]}(${citePart}${urlPart}${i[0]}${citePart}${refPartEnd}`, 'gim')

                if (re1.test(content)) {
                    changes = true
                    content = content.replaceAll(re1, `$1[[${i[2] ?? i[1]}]]$2`)
                }

                if (re2.test(content)) {
                    changes = true
                    content = content.replaceAll(re2, `$1[[${i[2] ?? i[1]}]]$2`)
                }
            }

            if (!changes) {
                mw.notify('No changes made', {
                    title: 'Cite Wikilinker',
                })

                return
            }

            textBox.textSelection('setContents', content)

            mw.notify('Citations wiki-linked', {
                title: 'Cite Wikilinker',
                type: 'success',
            })

            // Hook to add edit summary
            mw.hook( 've.saveDialog.stateChanged' ).add(prefillEditSummary)

            e.preventDefault()
        })
    }

    function prefillEditSummary() {
        if (ve.init.target.saveDialog) {
            ve.init.target.saveDialog.editSummaryInput.$input.val('Wikilink citation publisher ([[User:DaxServer/CiteWikiLinker|CiteWikiLinker.js]])')
        }

        // Remove hook upon prefilling
        mw.hook( 've.saveDialog.stateChanged' ).remove(prefillEditSummary)
    }
});

//</nowiki>