MediaWiki:Common.js: Difference between revisions
MediaWiki interface page
More actions
No edit summary |
No edit summary |
||
| Line 91: | Line 91: | ||
} | } | ||
})(); | })(); | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
var table = document. | var table = document.querySelector('.wikitable'); | ||
if (!table) return; | if (!table) return; | ||
function parseTime(t) { | function parseTime(t) { | ||
if (! | if (!t.includes(":")) return 999999; | ||
var parts = t.split(":"); | var parts = t.split(":"); | ||
return parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10 | return parseInt(parts[0],10) * 60 + parseInt(parts[1],10); | ||
} | } | ||
var rows = Array.from(table.rows).slice(1); | |||
rows.sort(function(a,b){ | |||
return parseTime(a.cells[3].innerText) - | |||
parseTime(b.cells[3].innerText); | |||
}); | |||
rows.forEach(row => table.appendChild(row)); | |||
rows.forEach(function(row, index){ | |||
row.cells[0].innerHTML = | |||
index === 0 ? "🥇 1" : | |||
index === 1 ? "🥈 2" : | |||
index === 2 ? "🥉 3" : | |||
index + 1; | |||
}); | |||
}); | }); | ||
Revision as of 02:27, 1 March 2026
/* Any JavaScript here will be loaded for all users on every page load. */// Load DataTables library
mw.hook('wikipage.content').add(function ($content) {
$content.find('.spoiler').on('click', function () {
$(this).toggleClass('revealed');
});
});
// Auto-embed X/Twitter status links into live tweets (no extension required)
(function () {
'use strict';
// Options you can tweak
var TW_OPTIONS = {
// theme: 'dark', // uncomment for dark
// conversation: 'none', // hide reply thread
// dnt: true, // "Do Not Track"
// align: 'center' // center the embed
};
function loadXWidgets (cb) {
// If the widget API is ready, go
if (window.twttr && twttr.widgets) { cb(); return; }
// Already loading?
if (document.querySelector('script[src*="platform.twitter.com/widgets.js"]')) {
// Poll until ready
var tries = 0, t = setInterval(function () {
if (window.twttr && twttr.widgets) { clearInterval(t); cb(); }
else if (++tries > 40) { clearInterval(t); }
}, 250);
return;
}
// Inject the widgets.js script
var s = document.createElement('script');
s.async = true;
s.src = 'https://platform.twitter.com/widgets.js';
s.onload = function () { cb(); };
document.head.appendChild(s);
}
// Find eligible links and embed them
function embedLinks(root) {
var scope = root || document;
var selector = [
'a[href^="https://x.com/"][href*="/status/"]',
'a[href^="http://x.com/"][href*="/status/"]',
'a[href^="https://twitter.com/"][href*="/status/"]',
'a[href^="http://twitter.com/"][href*="/status/"]'
].join(',');
var anchors = scope.querySelectorAll(selector);
if (!anchors.length) return;
loadXWidgets(function () {
anchors.forEach(function (a) {
if (a.dataset.tweetEmbedded === '1') return; // avoid duplicates
a.dataset.tweetEmbedded = '1';
// Extract the numeric tweet ID
var match = a.href.match(/\/status\/(\d+)/);
if (!match) return;
var id = match[1];
// Create a container after the link
var container = document.createElement('div');
container.className = 'tweet-embed';
a.insertAdjacentElement('afterend', container);
// Ask widgets API to render
if (window.twttr && twttr.widgets && twttr.widgets.createTweet) {
twttr.widgets.createTweet(id, container, TW_OPTIONS).catch(function(){});
}
});
// Ensure any dynamically added blockquotes also render
if (window.twttr && twttr.widgets && twttr.widgets.load) {
twttr.widgets.load(scope);
}
});
}
// Run on page load and whenever VE/LQT/etc. inject content
if (window.mw && mw.hook) {
mw.hook('wikipage.content').add(function ($content) {
embedLinks($content && $content[0]);
});
} else {
document.addEventListener('DOMContentLoaded', function () { embedLinks(document); });
}
})();
mw.hook('wikipage.content').add(function () {
var table = document.querySelector('.wikitable');
if (!table) return;
function parseTime(t) {
if (!t.includes(":")) return 999999;
var parts = t.split(":");
return parseInt(parts[0],10) * 60 + parseInt(parts[1],10);
}
var rows = Array.from(table.rows).slice(1);
rows.sort(function(a,b){
return parseTime(a.cells[3].innerText) -
parseTime(b.cells[3].innerText);
});
rows.forEach(row => table.appendChild(row));
rows.forEach(function(row, index){
row.cells[0].innerHTML =
index === 0 ? "🥇 1" :
index === 1 ? "🥈 2" :
index === 2 ? "🥉 3" :
index + 1;
});
});