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
Line 1: Line 1:
mw.loader.using(['mediawiki.api', 'mediawiki.util']).thhe(function () {
mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () {
     $(function () {
     $(function () {
         const link = mw.util.addPortletLink(
         const link = mw.util.addPortletLink(
             'p-tb',
             'p-tb',
             'javascript:void(0);',
             'javascript:void(0);',
             'הוסף he',
             'Add he',
             't-add-he-interwiki',
             't-add-he-interwiki',
             'הוסף קישור בינוויקי לחב"דפדיה העברית'
             'Add interwiki link to the Hebrew Chabadpedia'
         );
         );


         $(link).click(function (e) {
         $(link).click(function (e) {
             e.prevhetDefault();
             e.preventDefault();
             if ($('#he-interwiki-box').lhegth) return;
             if ($('#he-interwiki-box').length) return;


             const $box = $(`
             const $box = $(`
Line 24: Line 24:
                     box-shadow: 0 2px 5px rgba(0,0,0,0.1);
                     box-shadow: 0 2px 5px rgba(0,0,0,0.1);
                 ">
                 ">
                     <div style="margin-bottom: 0.5em; font-weight: bold;">הוספת קישור בינוויקי לשפה האנגלית</div>
                     <div style="margin-bottom: 0.5em; font-weight: bold;">Add interwiki link to Hebrew</div>
                     <input type="text" id="he-title-input" placeholder="שם הערך באנגלית" style="
                     <input type="text" id="he-title-input" placeholder="Hebrew page title" style="
                         width: 100%;
                         width: 100%;
                         padding: 8px;
                         padding: 8px;
Line 41: Line 41:
                             border-radius: 4px;
                             border-radius: 4px;
                             cursor: pointer;
                             cursor: pointer;
                         ">שמור</button>
                         ">Save</button>
                         <button id="cancel-he-link" style="
                         <button id="cancel-he-link" style="
                             padding: 6px 12px;
                             padding: 6px 12px;
Line 50: Line 50:
                             cursor: pointer;
                             cursor: pointer;
                             margin-right: 5px;
                             margin-right: 5px;
                         ">ביטול</button>
                         ">Cancel</button>
                     </div>
                     </div>
                     <div id="he-status" style="margin-top: 10px; font-weight: bold;"></div>
                     <div id="he-status" style="margin-top: 10px; font-weight: bold;"></div>
Line 56: Line 56:
             `);
             `);


            // שינוי מיקום: הצגה בתחילת הדף
             $('#mw-content-text').prepend($box);
             $('#mw-conthet-text').prephed($box);


             $('#cancel-he-link').click(function () {
             $('#cancel-he-link').click(function () {
Line 66: Line 65:
                 const heTitle = $('#he-title-input').val().trim();
                 const heTitle = $('#he-title-input').val().trim();
                 if (!heTitle) {
                 if (!heTitle) {
                     $('#he-status').text('יש להזין שם ערך.');
                     $('#he-status').text('Please enter a title.');
                     return;
                     return;
                 }
                 }
Line 73: Line 72:
                 const api = new mw.Api();
                 const api = new mw.Api();


                 $('#he-status').text('טוען תוכן הדף...');
                 $('#he-status').text('Loading page content...');


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


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


                     if (conthet.includes(iwLink)) {
                     if (/\[\[he:[^\]]+\]\]/i.test(content)) {
                         $('#he-status').text('הקישור כבר קיים.');
                         $('#he-status').text('A Hebrew interwiki link already exists.');
                         return;
                         return;
                     }
                     }


                     const newConthet = conthet.trim() + "\n\n" + iwLink;
                     const newContent = content.replace(/\s*$/, '') + "\n" + iwLink;


                     $('#he-status').text('שומר את הדף...');
                     $('#he-status').text('Saving...');


                     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: 'Interwiki',
                         format: 'json'
                         format: 'json'
                     }).done(function () {
                     }).done(function () {
                         $('#he-status').text('✓ הקישור נוסף! מרענן את הדף...');
                         $('#he-status').text('✓ Link added! Reloading...');
                         setTimeout(() => location.reload(), 1500);
                         setTimeout(() => location.reload(), 1500);
                     }).fail(function () {
                     }).fail(function () {
                         $('#he-status').text('⚠ שגיאה בעת השמירה.');
                         $('#he-status').text('⚠ Error while saving.');
                     });
                     });
                 }).fail(function () {
                 }).fail(function () {
                     $('#he-status').text('⚠ שגיאה בטעינת הדף.');
                     $('#he-status').text('⚠ Error loading page.');
                 });
                 });
             });
             });

Revision as of 11:14, 18 July 2025

mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () {
    $(function () {
        const link = mw.util.addPortletLink(
            'p-tb',
            'javascript:void(0);',
            'Add he',
            't-add-he-interwiki',
            'Add interwiki link to the Hebrew Chabadpedia'
        );

        $(link).click(function (e) {
            e.preventDefault();
            if ($('#he-interwiki-box').length) return;

            const $box = $(`
                <div id="he-interwiki-box" style="
                    background: #f0f4f8;
                    border: 1px solid #ccd;
                    border-radius: 8px;
                    padding: 1em;
                    margin-bottom: 2em;
                    max-width: 400px;
                    font-family: sans-serif;
                    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
                ">
                    <div style="margin-bottom: 0.5em; font-weight: bold;">Add interwiki link to Hebrew</div>
                    <input type="text" id="he-title-input" placeholder="Hebrew page title" style="
                        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;
                        ">Save</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;
                        ">Cancel</button>
                    </div>
                    <div id="he-status" style="margin-top: 10px; font-weight: bold;"></div>
                </div>
            `);

            $('#mw-content-text').prepend($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('Please enter a title.');
                    return;
                }

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

                $('#he-status').text('Loading page content...');

                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('Error: Could not load page.');
                        return;
                    }

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

                    if (/\[\[he:[^\]]+\]\]/i.test(content)) {
                        $('#he-status').text('A Hebrew interwiki link already exists.');
                        return;
                    }

                    const newContent = content.replace(/\s*$/, '') + "\n" + iwLink;

                    $('#he-status').text('Saving...');

                    api.postWithToken('csrf', {
                        action: 'edit',
                        title: mw.config.get('wgPageName'),
                        text: newContent,
                        summary: 'Interwiki',
                        format: 'json'
                    }).done(function () {
                        $('#he-status').text('✓ Link added! Reloading...');
                        setTimeout(() => location.reload(), 1500);
                    }).fail(function () {
                        $('#he-status').text('⚠ Error while saving.');
                    });
                }).fail(function () {
                    $('#he-status').text('⚠ Error loading page.');
                });
            });
        });
    });
});