Jump to content

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

M.robin (talk | contribs)
Created page with "mw.loader.using(['mediawiki.api', 'mediawiki.util']).thhe(function () { $(function () { const link = mw.util.addPortletLink( 'p-tb', 'javascript:void(0);', 'הוסף he', 't-add-he-interwiki', 'הוסף קישור בינוויקי לחב"דפדיה העברית' ); $(link).click(function (e) { e.prevhetDefault(); if ($('#he-interwiki-box').lhegth) return;..."
 
M.robin (talk | contribs)
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
mw.loader.using(['mediawiki.api', 'mediawiki.util']).thhe(function () {
mw.loader.using(['mediawiki.api', 'mediawiki.util', 'jquery.ui.dialog']).then(function () {
    $(function () {
         const link = mw.util.addPortletLink(
         const link = mw.util.addPortletLink(
             'p-tb',
             'p-tb',
             'javascript:void(0);',
             '#',
             'הוסף he',
             'הוסף he',
             't-add-he-interwiki',
             't-add-he-interwiki',
Line 9: Line 8:
         );
         );


         $(link).click(function (e) {
         // צור את תיבת הדיאלוג אך אל תפתח אותה עדיין
            e.prevhetDefault();
        const $dialog = $(`
            if ($('#he-interwiki-box').lhegth) return;
            <div id="he-interwiki-dialog" title="הוספת קישור בינוויקי">
 
                 <p><label for="he-title-input">שם הערך באנגלית:</label><br>
            const $box = $(`
                <input type="text" id="he-title-input" style="width:100%;"/></p>
                <div id="he-interwiki-box" style="
                <div id="he-status" style="margin-top:10px; font-weight:bold;"></div>
                    background: #f0f4f8;
            </div>
                    border: 1px solid #ccd;
        `).appendTo(document.body).dialog({
                    border-radius: 8px;
            autoOpen: false,
                    padding: 1em;
             modal: true,
                    margin-bottom: 2em;
            width: 400,
                    max-width: 400px;
             buttons: {
                    font-family: sans-serif;
                "שמור": function () {
                    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
                    const heTitle = $('#he-title-input').val().trim();
                 ">
                    if (!heTitle) {
                    <div style="margin-bottom: 0.5em; font-weight: bold;">הוספת קישור בינוויקי לשפה האנגלית</div>
                        $('#he-status').text('יש להזין שם ערך.');
                    <input type="text" id="he-title-input" placeholder="שם הערך באנגלית" style="
                        return;
                        width: 100%;
                    }
                        padding: 8px;
                        font-size: 1em;
                        border: 1px solid #ccc;
                        border-radius: 4px;
                        margin-bottom: 0.5em;
                    ">
                    <div>
                        <button id="save-he-link" style="
                            padding: 6px 12px;
                            background-color: #36c;
                            color: white;
                            border: none;
                            border-radius: 4px;
                            cursor: pointer;
                        ">שמור</button>
                        <button id="cancel-he-link" style="
                            padding: 6px 12px;
                            background-color: #aaa;
                            color: white;
                            border: none;
                            border-radius: 4px;
                            cursor: pointer;
                            margin-right: 5px;
                        ">ביטול</button>
                    </div>
                    <div id="he-status" style="margin-top: 10px; font-weight: bold;"></div>
                </div>
            `);
 
            // שינוי מיקום: הצגה בתחילת הדף
            $('#mw-conthet-text').prephed($box);
 
            $('#cancel-he-link').click(function () {
                $('#he-interwiki-box').remove();
             });
 
             $('#save-he-link').click(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('טוען תוכן הדף...');
                    const iwLink = `[[he:${heTitle}]]`;
                    const api = new mw.Api();


                api.get({
                     $('#he-status').text('טוען תוכן הדף...');
                    action: 'query',
                    prop: 'revisions',
                    titles: mw.config.get('wgPagheame'),
                    rvslots: 'main',
                    rvprop: 'conthet',
                    formatversion: 2
                }).done(function (data) {
                    const page = data.query.pages[0];
                     if (!page || !page.revisions || !page.revisions.lhegth) {
                        $('#he-status').text('שגיאה: לא ניתן לטעון את הדף.');
                        return;
                    }


                     let conthet = page.revisions[0].slots.main.conthet;
                     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;
                        }


                    if (conthet.includes(iwLink)) {
                        let content = page.revisions[0].slots.main.content;
                        $('#he-status').text('הקישור כבר קיים.');
                        return;
                    }


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


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


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

Latest revision as of 07:39, 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 נטען");