Jump to content

User:CFA/scripts/attributetranslation.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by CFA (talk | contribs) at 17:32, 1 June 2024 (fix). 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() {
    'use strict';

    function createInputGUI() {
        var gui = document.createElement('div');
        gui.style.position = 'fixed';
        gui.style.top = '50%';
        gui.style.left = '50%';
        gui.style.transform = 'translate(-50%, -50%)';
        gui.style.backgroundColor = '#f8f9fa';
        gui.style.border = '1px solid #a2a9b1';
        gui.style.padding = '20px';
        gui.style.zIndex = 10000;
        gui.innerHTML = `
            <label for="langCode">Language Code:</label>
            <input type="text" id="langCode" placeholder="e.g. zh, es"><br><br>
            <label for="articleName">Article Name:</label>
            <input type="text" id="articleName" placeholder="Exact name of article"><br><br>
            <button id="submitTranslation">Submit</button>
            <button id="closeGUI">Close</button>
        `;
        document.body.appendChild(gui);

        document.getElementById('submitTranslation').addEventListener('click', function() {
            var langCode = document.getElementById('langCode').value.trim();
            var articleName = document.getElementById('articleName').value.trim();
            if (langCode && articleName) {
                processTranslation(langCode, articleName);
                document.body.removeChild(gui);
            } else {
                alert('Please fill in both fields.');
            }
        });

        document.getElementById('closeGUI').addEventListener('click', function() {
            document.body.removeChild(gui);
        });
    }

    function processTranslation(langCode, articleName) {
        var pageTitle = mw.config.get('wgPageName').replace(/_/g, ' ');

        var editSummary = `Content in this edit is translated from the existing ${langCode} Wikipedia article at ${langCode}:${articleName}; see its history for attribution.`;

        $.ajax({
            url: mw.util.wikiScript('api'),
            type: 'GET',
            data: {
                format: 'json',
                action: 'query',
                prop: 'revisions',
                rvprop: 'content',
                titles: pageTitle
            },
            success: function(data) {
                var pages = data.query.pages;
                var pageContent = '';
                for (var pageId in pages) {
                    if (pages.hasOwnProperty(pageId)) {
                        pageContent = pages[pageId].revisions[0]['*'];
                    }
                }
                pageContent += ' ';

                $.ajax({
                    url: mw.util.wikiScript('api'),
                    type: 'POST',
                    data: {
                        format: 'json',
                        action: 'edit',
                        title: pageTitle,
                        text: pageContent,
                        summary: editSummary,
                        token: mw.user.tokens.get('csrfToken')
                    },
                    success: function() {
                        console.log('Dummy edit successful');
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        console.error('Error performing dummy edit:', textStatus, errorThrown);
                    }
                });
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.error('Error retrieving page content:', textStatus, errorThrown);
            }
        });

        var talkPageTitle = 'Talk:' + pageTitle;

        $.ajax({
            url: mw.util.wikiScript('api'),
            type: 'POST',
            data: {
                format: 'json',
                action: 'edit',
                title: talkPageTitle,
                section: 'new',
                sectiontitle: 'Translation attribution',
                text: `{{Translated page|${langCode}|${articleName}}}`,
                token: mw.user.tokens.get('csrfToken')
            },
            success: function() {
                console.log('Article talk page updated');
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.error('Error updating article talk page:', textStatus, errorThrown);
            }
        });

        notifyContributor(creator, langCode, articleName);
    }

    function notifyContributor(contributor, langCode, articleName) {
    var subst = "subst:";
    var notificationMessage = `{{${subst}uw-translation|1=${langCode}|2=${articleName}}}`;

        $.ajax({
            url: mw.util.wikiScript('api'),
            type: 'POST',
            data: {
                format: 'json',
                action: 'edit',
                title: 'User talk:' + contributor,
                section: 'new',
                sectiontitle: 'Notification: Translation Attribution',
                text: notificationMessage,
                token: mw.user.tokens.get('csrfToken')
            },
            success: function() {
                console.log('Notification sent to user talk page');
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.error('Error sending notification:', textStatus, errorThrown);
                if (confirm("Could not notify contributor. Retry?")) {
                    notifyContributor(contributor, langCode, articleName);
                }
            }
        });
    }

    var button = document.createElement('button');
    button.innerHTML = 'Translation Attribution';
    button.style.position = 'fixed';
    button.style.top = '10px';
    button.style.right = '10px';
    button.style.zIndex = 1000;
    button.style.backgroundColor = '#f8f9fa';
    button.style.border = '1px solid #a2a9b1';
    button.style.padding = '5px 10px';
    button.style.cursor = 'pointer';

    document.body.appendChild(button);

    button.addEventListener('click', createInputGUI);
})();