User:M.robin/Interwiki.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']).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.');
});
});
});
});
});