User:M.robin/Interwiki.js: Difference between revisions
Appearance
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;..." |
No edit summary |
||
| Line 1: | Line 1: | ||
mw.loader.using(['mediawiki.api', 'mediawiki.util']). | 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);', | ||
' | '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. | e.preventDefault(); | ||
if ($('#he-interwiki-box'). | 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 style="margin-bottom: 0.5em; font-weight: bold;">Add interwiki link to Hebrew</div> | ||
<input type="text" id="he-title-input" placeholder=" | <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; | ||
"> | ">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; | ||
"> | ">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- | |||
$('#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(' | titles: mw.config.get('wgPageName'), | ||
rvslots: 'main', | rvslots: 'main', | ||
rvprop: ' | 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. | if (!page || !page.revisions || !page.revisions.length) { | ||
$('#he-status').text(' | $('#he-status').text('Error: Could not load page.'); | ||
return; | return; | ||
} | } | ||
let | let content = page.revisions[0].slots.main.content; | ||
if ( | if (/\[\[he:[^\]]+\]\]/i.test(content)) { | ||
$('#he-status').text(' | $('#he-status').text('A Hebrew interwiki link already exists.'); | ||
return; | return; | ||
} | } | ||
const | const newContent = content.replace(/\s*$/, '') + "\n" + iwLink; | ||
$('#he-status').text(' | $('#he-status').text('Saving...'); | ||
api. | api.postWithToken('csrf', { | ||
action: 'edit', | action: 'edit', | ||
title: mw.config.get(' | title: mw.config.get('wgPageName'), | ||
text: | 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.');
});
});
});
});
});