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:00, 1 June 2024 (testing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.`;
        var editContent = document.getElementById('wpTextbox1').value;
        editContent += ' '; // Add a space to the bottom to make a dummy edit

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

        var creationLog = mw.config.get('wgPageCreationLog');
        var creator = creationLog[0].user;
        var date = new Date();
        var monthYear = date.toLocaleString('default', { month: 'long', year: 'numeric' });
        var userTalkPage = 'User talk:' + creator;

        $.ajax({
            url: mw.util.wikiScript('api'),
            type: 'POST',
            data: {
                format: 'json',
                action: 'edit',
                title: userTalkPage,
                section: 'new',
                sectiontitle: monthYear,
                text: '{{uw-translation}}',
                token: mw.user.tokens.get('csrfToken')
            },
            success: function() {
                console.log('User talk page updated');
            },
            error: function() {
                console.error('Error updating user talk page');
            }
        });

        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() {
                console.error('Error updating article talk page');
            }
        });
    }

    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);
})();