לדלג לתוכן

משתמש:מ. רובין/ניסוי.js

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () {
    $(function () {
        // הוספת כפתור בתפריט הצד
        const link = mw.util.addPortletLink(
            'p-tb',
            '#',
            'הוסף תבנית',
            't-add-maint-template',
            'הוסף תבנית תחזוקה לדף'
        );

        $(link).click(function (e) {
            e.preventDefault();

            if ($('#template-box').length) return; // לא לפתוח פעמיים

            const $box = $(`
                <div id="template-box" style="
                    background: #f9f9f9;
                    border: 1px solid #ccc;
                    padding: 10px;
                    margin-top: 10px;
                    border-radius: 8px;
                    max-width: 300px;
                    font-family: sans-serif;
                ">
                    <label for="template-select" style="display: block; margin-bottom: 5px;">בחר תבנית להוספה:</label>
                    <select id="template-select" style="width: 100%; padding: 5px;">
                        <option value="עריכה דחופה">עריכה דחופה</option>
                    </select>
                    <div style="margin-top: 10px;">
                        <button id="add-template-button" style="padding: 5px 10px;">הוסף</button>
                        <button id="cancel-template-button" style="padding: 5px 10px; margin-right: 5px;">ביטול</button>
                    </div>
                    <div id="template-status" style="margin-top: 10px; font-weight: bold;"></div>
                </div>
            `);

            $(link).after($box);

            $('#cancel-template-button').click(() => $box.remove());

            $('#add-template-button').click(function () {
                const template = $('#template-select').val();
                if (!template) return;

                const api = new mw.Api();
                const title = mw.config.get('wgPageName');

                $('#template-status').text('טוען תוכן...');

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

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

                    const templateRegex = new RegExp(`{{\\s*${template}\\s*}}`, 'i');
                    if (templateRegex.test(content)) {
                        $('#template-status').text('כבר קיימת התבנית בדף.');
                        return;
                    }

                    const newContent = `{{${template}}}\n` + content;

                    $('#template-status').text('שומר...');

                    api.postWithToken('csrf', {
                        action: 'edit',
                        title: title,
                        text: newContent,
                        summary: `הוספת תבנית {{${template}}}`,
                        format: 'json'
                    }).done(function () {
                        $('#template-status').text('✓ נוסף! מרענן...');
                        setTimeout(() => location.reload(), 1500);
                    }).fail(function () {
                        $('#template-status').text('⚠ שגיאה בעת השמירה.');
                    });
                }).fail(function () {
                    $('#template-status').text('⚠ שגיאה בטעינת הדף.');
                });
            });
        });
    });
});