User:M.robin/common.js
Appearance
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
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 נטען");