משתמש:מ. רובין/ניסוי.js – הבדלי גרסאות

מ. רובין (שיחה | תרומות)
אין תקציר עריכה
מ. רובין (שיחה | תרומות)
הועבר למשתמש:מ. רובין/בינוויקי.js
תגיות: ריקון שחזור ידני
שורה 1: שורה 1:
mw.loader.using(['mediawiki.api', 'mediawiki.util', 'jquery.ui']).then(function () {
    $(function () {
        const link = mw.util.addPortletLink(
            'p-tb',
            '#',
            'הוסף en',
            't-add-en-interwiki',
            'הוסף קישור בינוויקי לחב"דפדיה האנגלית'
        );


        // צור את תיבת הדיאלוג אך אל תפתח אותה עדיין
        const $dialog = $(`
            <div id="en-interwiki-dialog" title="הוספת קישור בינוויקי">
                <p><label for="en-title-input">שם הערך באנגלית:</label><br>
                <input type="text" id="en-title-input" style="width:100%;"/></p>
                <div id="en-status" style="margin-top:10px; font-weight:bold;"></div>
            </div>
        `).appendTo(document.body).dialog({
            autoOpen: false,
            modal: true,
            width: 400,
            buttons: {
                "שמור": function () {
                    const enTitle = $('#en-title-input').val().trim();
                    if (!enTitle) {
                        $('#en-status').text('יש להזין שם ערך.');
                        return;
                    }
                    const iwLink = `[[en:${enTitle}]]`;
                    const api = new mw.Api();
                    $('#en-status').text('טוען תוכן הדף...');
                    api.get({
                        action: 'query',
                        prop: 'revisions',
                        titles: mw.config.get('wgPageName'),
                        rvslots: 'main',
                        rvprop: 'content',
                        formatversion: 2
                    }).done(function (data) {
                        const page = data.query.pages[0];
                        if (!page || !page.revisions || !page.revisions.length) {
                            $('#en-status').text('שגיאה: לא ניתן לטעון את הדף.');
                            return;
                        }
                        let content = page.revisions[0].slots.main.content;
                        if (/\[\[en:[^\]]+\]\]/i.test(content)) {
                            $('#en-status').text('כבר קיים קישור לשפה האנגלית.');
                            return;
                        }
                        const newContent = content.replace(/\s*$/, '') + "\n" + iwLink;
                        $('#en-status').text('שומר את הדף...');
                        api.postWithToken('csrf', {
                            action: 'edit',
                            title: mw.config.get('wgPageName'),
                            text: newContent,
                            summary: 'בינוויקי',
                            format: 'json'
                        }).done(function () {
                            $('#en-status').text('✓ הקישור נוסף! מרענן את הדף...');
                            setTimeout(() => location.reload(), 1500);
                        }).fail(function () {
                            $('#en-status').text('⚠ שגיאה בעת השמירה.');
                        });
                    }).fail(function () {
                        $('#en-status').text('⚠ שגיאה בטעינת הדף.');
                    });
                },
                "ביטול": function () {
                    $(this).dialog("close");
                }
            }
        });
        $(link).click(function (e) {
            e.preventDefault();
            $('#en-title-input').val('');
            $('#en-status').text('');
            $dialog.dialog('open');
        });
    });
});