MediaWiki:Common.js: Perbedaan antara revisi
Tampilan
Tidak ada ringkasan suntingan Tanda: Suntingan perangkat seluler Suntingan peramban seluler |
Tidak ada ringkasan suntingan Tanda: Suntingan perangkat seluler Suntingan peramban seluler |
||
| Baris 1: | Baris 1: | ||
(function() { | (function() { | ||
$(document).ready(function() { | $(document).ready(function() { | ||
var $descSection = $('#mip-desc-section') | var $descSection = $('#mip-desc-section'), $descBox = $('#mip-auto-description'), | ||
$sourceInfo = $('#mip-source-info'), $portalLinks = $('#mip-portal-links'), | |||
$projectPortal = $('#mip-project-portal'); | |||
if (!$descBox.length) return; | if (!$descBox.length) return; | ||
var pageTitle = mw.config.get('wgPageName').replace(/_/g, ' ') | var pageTitle = mw.config.get('wgPageName').replace(/_/g, ' '), | ||
cacheKey = 'mip_ultimate_' + pageTitle, now = new Date().getTime(), | |||
currentYear = 2026; | |||
var projects = [ | var projects = [ | ||
| Baris 23: | Baris 17: | ||
]; | ]; | ||
// --- SEMUA FUNGSI AWAL (TIDAK ADA YANG DIHAPUS) --- | |||
function applyAutoBold(text) { | function applyAutoBold(text) { | ||
var regex = new RegExp('(' + pageTitle + ')', 'gi'); | var regex = new RegExp('(' + pageTitle + ')', 'gi'); | ||
return text.replace(regex, '<strong>$1</strong>'); | return text.replace(regex, '<strong>$1</strong>'); | ||
} | } | ||
function cleanExtract(text) { return text.replace(/\[\d+\]/g, '').replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim(); } | function cleanExtract(text) { return text.replace(/\[\d+\]/g, '').replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim(); } | ||
function applyAutoAge(text) { | function applyAutoAge(text) { | ||
return text.replace(/(\d{1,2}\s(?:Januari|Februari|Maret|April|Mei|Juni|Juli|Agustus|September|Oktober|November|Desember)\s(\d{4}))/gi, function(m, f, y) { | return text.replace(/(\d{1,2}\s(?:Januari|Februari|Maret|April|Mei|Juni|Juli|Agustus|September|Oktober|November|Desember)\s(\d{4}))/gi, function(m, f, y) { | ||
| Baris 35: | Baris 28: | ||
}); | }); | ||
} | } | ||
function applySummary(text) { | function applySummary(text) { | ||
var limit = 250; | var limit = 250; | ||
| Baris 41: | Baris 33: | ||
return '<span>' + text.substring(0, limit) + '</span><span class="mip-dots">... </span><span class="mip-more" style="display:none;">' + text.substring(limit) + '</span><span class="mip-read-btn" style="color: #6a5acd; cursor: pointer; font-weight: bold; margin-left: 5px;">Baca selengkapnya</span>'; | return '<span>' + text.substring(0, limit) + '</span><span class="mip-dots">... </span><span class="mip-more" style="display:none;">' + text.substring(limit) + '</span><span class="mip-read-btn" style="color: #6a5acd; cursor: pointer; font-weight: bold; margin-left: 5px;">Baca selengkapnya</span>'; | ||
} | } | ||
function buildSmartLinks(currentId) { | function buildSmartLinks(currentId) { | ||
$portalLinks.empty(); | $portalLinks.empty(); | ||
| Baris 52: | Baris 43: | ||
if (validLinks.length > 0) { $portalLinks.html(validLinks.join(' <span style="color:#ccc; margin: 0 5px;">•</span> ')); $projectPortal.fadeIn(); } | if (validLinks.length > 0) { $portalLinks.html(validLinks.join(' <span style="color:#ccc; margin: 0 5px;">•</span> ')); $projectPortal.fadeIn(); } | ||
}); | }); | ||
} | |||
// --- SISTEM SMART CHECK --- | |||
var cached = JSON.parse(localStorage.getItem(cacheKey) || "{}"); | |||
function init() { | |||
// Pilih satu sumber acak hanya untuk cek timestamp (sangat ringan) | |||
var pCheck = projects[Math.floor(Math.random() * projects.length)]; | |||
$.ajax({ | |||
url: pCheck.url, | |||
data: { action: 'query', prop: 'revisions', rvprop: 'timestamp', titles: pageTitle, format: 'json', origin: '*' }, | |||
dataType: 'json', | |||
success: function(res) { | |||
var pg = res.query.pages, id = Object.keys(pg)[0]; | |||
var latestTS = (id != "-1") ? pg[id].revisions[0].timestamp : "0"; | |||
if (cached.content && cached.ts === latestTS) { | |||
// DATA SAMA: Langsung render dari cache (Instan) | |||
renderAll(cached.p, cached.content, cached.isTrans, cached.orig); | |||
} else { | |||
// DATA BEDA / BARU: Jalankan Race Mode | |||
fetchFreshData(latestTS); | |||
} | |||
}, | |||
error: function() { | |||
if (cached.content) renderAll(cached.p, cached.content, cached.isTrans, cached.orig); | |||
} | |||
}); | |||
} | |||
function fetchFreshData(newTS) { | |||
var found = false; | |||
var shuffled = projects.sort(() => Math.random() - 0.5); | |||
shuffled.forEach(function(p) { | |||
if (found) return; | |||
$.ajax({ | |||
url: p.url, | |||
data: { action: 'query', prop: 'extracts', exintro: 1, explaintext: 1, titles: pageTitle, format: 'json', origin: '*' }, | |||
dataType: 'json', | |||
success: function(data) { | |||
if (found) return; | |||
var pages = data.query.pages, pageId = Object.keys(pages)[0]; | |||
if (pageId != "-1" && pages[pageId].extract) { | |||
found = true; | |||
var extract = cleanExtract(pages[pageId].extract); | |||
if (p.id === 'en') { | |||
$.ajax({ url: "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=id&dt=t&q=" + encodeURIComponent(extract), success: function(res) { | |||
var trans = ""; res[0].forEach(s => { if (s[0]) trans += s[0]; }); | |||
finalize(p, trans, true, extract, newTS); | |||
}}); | |||
} else { finalize(p, extract, false, "", newTS); } | |||
} | |||
} | |||
}); | |||
}); | |||
} | |||
function finalize(p, c, t, o, ts) { | |||
localStorage.setItem(cacheKey, JSON.stringify({p:p, content:c, isTrans:t, orig:o, ts:ts})); | |||
renderAll(p, c, t, o); | |||
} | } | ||
function renderAll(p, currentText, isTranslated, originalText) { | function renderAll(p, currentText, isTranslated, originalText) { | ||
var processedText = applyAutoBold(applyAutoAge(currentText)); | var processedText = applyAutoBold(applyAutoAge(currentText)); | ||
$descSection.show(); | |||
$descBox.show().html(applySummary(processedText)); | $descBox.show().html(applySummary(processedText)); | ||
var footer = '<div style="font-size: 0.9em; color: #777;">Sumber Dari : <a href="' + p.base + encodeURIComponent(pageTitle) + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + '.</a>'; | var footer = '<div style="font-size: 0.9em; color: #777;">Sumber Dari : <a href="' + p.base + encodeURIComponent(pageTitle) + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + '.</a>'; | ||
if (isTranslated) { footer += '<br><span style="font-size: 0.85em; font-style: italic;">(Diterjemahkan secara otomatis)</span> <span id="mip-toggle-orig" style="color: #6a5acd; cursor: pointer; text-decoration: underline; margin-left: 5px;">Tampilkan versi asli</span>'; } | if (isTranslated) { footer += '<br><span style="font-size: 0.85em; font-style: italic;">(Diterjemahkan secara otomatis)</span> <span id="mip-toggle-orig" style="color: #6a5acd; cursor: pointer; text-decoration: underline; margin-left: 5px;">Tampilkan versi asli</span>'; } | ||
$sourceInfo.show().html(footer); | $sourceInfo.show().html(footer); | ||
buildSmartLinks(p.id); | buildSmartLinks(p.id); | ||
// Event Handlers | |||
$(document).off('click', '.mip-read-btn').on('click', '.mip-read-btn', function() { $(this).hide(); $('.mip-dots').hide(); $('.mip-more').fadeIn(); }); | $(document).off('click', '.mip-read-btn').on('click', '.mip-read-btn', function() { $(this).hide(); $('.mip-dots').hide(); $('.mip-more').fadeIn(); }); | ||
$(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() { | $(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() { | ||
| Baris 70: | Baris 126: | ||
} | } | ||
init(); | |||
}); | }); | ||
})(); | })(); | ||
Revisi per 15 April 2026 15.29
(function() {
$(document).ready(function() {
var $descSection = $('#mip-desc-section'), $descBox = $('#mip-auto-description'),
$sourceInfo = $('#mip-source-info'), $portalLinks = $('#mip-portal-links'),
$projectPortal = $('#mip-project-portal');
if (!$descBox.length) return;
var pageTitle = mw.config.get('wgPageName').replace(/_/g, ' '),
cacheKey = 'mip_ultimate_' + pageTitle, now = new Date().getTime(),
currentYear = 2026;
var projects = [
{ id: 'id', name: 'Mippedia bahasa Indonesia', url: 'https://id.mippedia.org/api.php', base: 'https://id.mippedia.org/wiki/', label: 'Bahasa Indonesia' },
{ id: 'en', name: 'Mippedia bahasa Inggris', url: 'https://en.mippedia.org/api.php', base: 'https://en.mippedia.org/wiki/', label: 'Bahasa Inggris' },
{ id: 'concise', name: 'Mippedia bahasa Indonesia ringkas', url: 'https://concise.mippedia.org/api.php', base: 'https://concise.mippedia.org/wiki/', label: 'Versi Ringkas' }
];
// --- SEMUA FUNGSI AWAL (TIDAK ADA YANG DIHAPUS) ---
function applyAutoBold(text) {
var regex = new RegExp('(' + pageTitle + ')', 'gi');
return text.replace(regex, '<strong>$1</strong>');
}
function cleanExtract(text) { return text.replace(/\[\d+\]/g, '').replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim(); }
function applyAutoAge(text) {
return text.replace(/(\d{1,2}\s(?:Januari|Februari|Maret|April|Mei|Juni|Juli|Agustus|September|Oktober|November|Desember)\s(\d{4}))/gi, function(m, f, y) {
return f + " – usia " + (currentYear - parseInt(y)) + " tahun";
});
}
function applySummary(text) {
var limit = 250;
if (text.length <= limit) return '<span>' + text + '</span>';
return '<span>' + text.substring(0, limit) + '</span><span class="mip-dots">... </span><span class="mip-more" style="display:none;">' + text.substring(limit) + '</span><span class="mip-read-btn" style="color: #6a5acd; cursor: pointer; font-weight: bold; margin-left: 5px;">Baca selengkapnya</span>';
}
function buildSmartLinks(currentId) {
$portalLinks.empty();
var validLinks = [];
var checkRequests = projects.filter(p => p.id !== currentId).map(function(p) {
return $.ajax({ url: p.url, data: { action: 'query', titles: pageTitle, format: 'json', origin: '*' }, dataType: 'json' })
.then(function(data) { if (data.query.pages["-1"] === undefined) { validLinks.push('<a href="' + p.base + encodeURIComponent(pageTitle) + '" target="_blank" style="color: #6a5acd; text-decoration: underline; font-weight: bold;">' + p.label + '</a>'); } });
});
$.when.apply($, checkRequests).done(function() {
if (validLinks.length > 0) { $portalLinks.html(validLinks.join(' <span style="color:#ccc; margin: 0 5px;">•</span> ')); $projectPortal.fadeIn(); }
});
}
// --- SISTEM SMART CHECK ---
var cached = JSON.parse(localStorage.getItem(cacheKey) || "{}");
function init() {
// Pilih satu sumber acak hanya untuk cek timestamp (sangat ringan)
var pCheck = projects[Math.floor(Math.random() * projects.length)];
$.ajax({
url: pCheck.url,
data: { action: 'query', prop: 'revisions', rvprop: 'timestamp', titles: pageTitle, format: 'json', origin: '*' },
dataType: 'json',
success: function(res) {
var pg = res.query.pages, id = Object.keys(pg)[0];
var latestTS = (id != "-1") ? pg[id].revisions[0].timestamp : "0";
if (cached.content && cached.ts === latestTS) {
// DATA SAMA: Langsung render dari cache (Instan)
renderAll(cached.p, cached.content, cached.isTrans, cached.orig);
} else {
// DATA BEDA / BARU: Jalankan Race Mode
fetchFreshData(latestTS);
}
},
error: function() {
if (cached.content) renderAll(cached.p, cached.content, cached.isTrans, cached.orig);
}
});
}
function fetchFreshData(newTS) {
var found = false;
var shuffled = projects.sort(() => Math.random() - 0.5);
shuffled.forEach(function(p) {
if (found) return;
$.ajax({
url: p.url,
data: { action: 'query', prop: 'extracts', exintro: 1, explaintext: 1, titles: pageTitle, format: 'json', origin: '*' },
dataType: 'json',
success: function(data) {
if (found) return;
var pages = data.query.pages, pageId = Object.keys(pages)[0];
if (pageId != "-1" && pages[pageId].extract) {
found = true;
var extract = cleanExtract(pages[pageId].extract);
if (p.id === 'en') {
$.ajax({ url: "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=id&dt=t&q=" + encodeURIComponent(extract), success: function(res) {
var trans = ""; res[0].forEach(s => { if (s[0]) trans += s[0]; });
finalize(p, trans, true, extract, newTS);
}});
} else { finalize(p, extract, false, "", newTS); }
}
}
});
});
}
function finalize(p, c, t, o, ts) {
localStorage.setItem(cacheKey, JSON.stringify({p:p, content:c, isTrans:t, orig:o, ts:ts}));
renderAll(p, c, t, o);
}
function renderAll(p, currentText, isTranslated, originalText) {
var processedText = applyAutoBold(applyAutoAge(currentText));
$descSection.show();
$descBox.show().html(applySummary(processedText));
var footer = '<div style="font-size: 0.9em; color: #777;">Sumber Dari : <a href="' + p.base + encodeURIComponent(pageTitle) + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + '.</a>';
if (isTranslated) { footer += '<br><span style="font-size: 0.85em; font-style: italic;">(Diterjemahkan secara otomatis)</span> <span id="mip-toggle-orig" style="color: #6a5acd; cursor: pointer; text-decoration: underline; margin-left: 5px;">Tampilkan versi asli</span>'; }
$sourceInfo.show().html(footer);
buildSmartLinks(p.id);
// Event Handlers
$(document).off('click', '.mip-read-btn').on('click', '.mip-read-btn', function() { $(this).hide(); $('.mip-dots').hide(); $('.mip-more').fadeIn(); });
$(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() {
var isOrig = ($(this).text() === 'Tampilkan versi asli');
var textToDisplay = applyAutoBold(applyAutoAge(isOrig ? originalText : currentText));
$descBox.html(applySummary(textToDisplay));
$(this).text(isOrig ? 'Tampilkan terjemahan' : 'Tampilkan versi asli');
});
}
init();
});
})();