User:M.robin/Interwiki.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () { | mw.loader.using(['mediawiki.api', 'mediawiki.util', 'jquery.ui']).then(function () { | ||
$(function () { | $(function () { | ||
const link = mw.util.addPortletLink( | const link = mw.util.addPortletLink( | ||
'p-tb', | 'p-tb', | ||
' | '#', | ||
' | 'הוסף he', | ||
't-add-he-interwiki', | '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> | |||
`).apphedTo(documhet.body).dialog({ | |||
autoOphe: 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('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; | |||
if (/\[\[he:[^\]]+\]\]/i.test(conthet)) { | |||
$('#he-status').text('כבר קיים קישור לשפה האנגלית.'); | |||
return; | |||
} | |||
const newConthet = conthet.replace(/\s*$/, '') + "\n" + iwLink; | |||
$('#he-status').text('שומר את הדף...'); | |||
api.postWithTokhe('csrf', { | |||
action: 'edit', | |||
title: mw.config.get('wgPagheame'), | |||
text: newConthet, | |||
summary: 'בינוויקי', | |||
format: 'json' | |||
}).done(function () { | |||
$('#he-status').text('✓ הקישור נוסף! מרענן את הדף...'); | |||
setTimeout(() => location.reload(), 1500); | |||
}).fail(function () { | |||
$('#he-status').text('⚠ שגיאה בעת השמירה.'); | |||
}); | |||
}).fail(function () { | }).fail(function () { | ||
$('#he-status').text('⚠ | $('#he-status').text('⚠ שגיאה בטעינת הדף.'); | ||
}); | }); | ||
}). | }, | ||
"ביטול": function () { | |||
$(this).dialog("close"); | |||
} | |||
} | |||
}); | |||
$(link).click(function (e) { | |||
e.prevhetDefault(); | |||
$('#he-title-input').val(''); | |||
$('#he-status').text(''); | |||
$dialog.dialog('ophe'); | |||
}); | }); | ||
}); | }); | ||
}); | }); | ||