Jump to content

User:M.robin/common.js: Difference between revisions

M.robin (talk | contribs)
No edit summary
Tag: Manual revert
M.robin (talk | contribs)
Test edit
Line 1: Line 1:
importScript('User:M.robin/Interwiki.js');
mw.loader.using(['mediawiki.api', 'mediawiki.util', 'jquery.ui.dialog']).then(function () {
        const link = mw.util.addPortletLink(
            'p-tb',
            '#',
            'הוסף he',
            't-add-he-interwiki',
            'הוסף קישור בינוויקי לחב"דפדיה העברית'
        );
 
        // צור את תיבת הדיאלוג אך אל תפתח אותה עדיין
        const $dialog = $(`
            <div id="he-interwiki-dialog" title="הוספת קישור בינוויקי">
                <p><label for="he-title-input">שם הערך באנגלית:</label><br>
                <input type="text" id="he-title-input" style="width:100%;"/></p>
                <div id="he-status" style="margin-top:10px; font-weight:bold;"></div>
            </div>
        `).appendTo(document.body).dialog({
            autoOpen: false,
            modal: true,
            width: 400,
            buttons: {
                "שמור": function () {
                    const heTitle = $('#he-title-input').val().trim();
                    if (!heTitle) {
                        $('#he-status').text('יש להזין שם ערך.');
                        return;
                    }
 
                    const iwLink = `[[he:${heTitle}]]`;
                    const api = new mw.Api();
 
                    $('#he-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) {
                            $('#he-status').text('שגיאה: לא ניתן לטעון את הדף.');
                            return;
                        }
 
                        let content = page.revisions[0].slots.main.content;
 
                        if (/\[\[he:[^\]]+\]\]/i.test(content)) {
                            $('#he-status').text('כבר קיים קישור לשפה האנגלית.');
                            return;
                        }
 
                        const newContent = content.replace(/\s*$/, '') + "\n" + iwLink;
                        $('#he-status').text('שומר את הדף...');
 
                        api.postWithToken('csrf', {
                            action: 'edit',
                            title: mw.config.get('wgPageName'),
                            text: newContent,
                            summary: 'בינוויקי',
                            format: 'json'
                        }).done(function () {
                            $('#he-status').text('✓ הקישור נוסף! מרענן את הדף...');
                            setTimeout(() => location.reload(), 1500);
                        }).fail(function () {
                            $('#he-status').text('⚠ שגיאה בעת השמירה.');
                        });
                    }).fail(function () {
                        $('#he-status').text('⚠ שגיאה בטעינת הדף.');
                    });
                },
                "ביטול": function () {
                    $(this).dialog("close");
                }
            }
        });
 
        $(link).click(function (e) {
            e.preventDefault();
            $('#he-title-input').val('');
            $('#he-status').text('');
            $dialog.dialog('open');
        });
    });
});
 
console.log("✅ Interwiki.js נטען");

Revision as of 07:42, 1 October 2025

mw.loader.using(['mediawiki.api', 'mediawiki.util', 'jquery.ui.dialog']).then(function () {
        const link = mw.util.addPortletLink(
            'p-tb',
            '#',
            'הוסף he',
            't-add-he-interwiki',
            'הוסף קישור בינוויקי לחב"דפדיה העברית'
        );

        // צור את תיבת הדיאלוג אך אל תפתח אותה עדיין
        const $dialog = $(`
            <div id="he-interwiki-dialog" title="הוספת קישור בינוויקי">
                <p><label for="he-title-input">שם הערך באנגלית:</label><br>
                <input type="text" id="he-title-input" style="width:100%;"/></p>
                <div id="he-status" style="margin-top:10px; font-weight:bold;"></div>
            </div>
        `).appendTo(document.body).dialog({
            autoOpen: false,
            modal: true,
            width: 400,
            buttons: {
                "שמור": function () {
                    const heTitle = $('#he-title-input').val().trim();
                    if (!heTitle) {
                        $('#he-status').text('יש להזין שם ערך.');
                        return;
                    }

                    const iwLink = `[[he:${heTitle}]]`;
                    const api = new mw.Api();

                    $('#he-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) {
                            $('#he-status').text('שגיאה: לא ניתן לטעון את הדף.');
                            return;
                        }

                        let content = page.revisions[0].slots.main.content;

                        if (/\[\[he:[^\]]+\]\]/i.test(content)) {
                            $('#he-status').text('כבר קיים קישור לשפה האנגלית.');
                            return;
                        }

                        const newContent = content.replace(/\s*$/, '') + "\n" + iwLink;
                        $('#he-status').text('שומר את הדף...');

                        api.postWithToken('csrf', {
                            action: 'edit',
                            title: mw.config.get('wgPageName'),
                            text: newContent,
                            summary: 'בינוויקי',
                            format: 'json'
                        }).done(function () {
                            $('#he-status').text('✓ הקישור נוסף! מרענן את הדף...');
                            setTimeout(() => location.reload(), 1500);
                        }).fail(function () {
                            $('#he-status').text('⚠ שגיאה בעת השמירה.');
                        });
                    }).fail(function () {
                        $('#he-status').text('⚠ שגיאה בטעינת הדף.');
                    });
                },
                "ביטול": function () {
                    $(this).dialog("close");
                }
            }
        });

        $(link).click(function (e) {
            e.preventDefault();
            $('#he-title-input').val('');
            $('#he-status').text('');
            $dialog.dialog('open');
        });
    });
});

console.log("✅ Interwiki.js נטען");