MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
No edit summary Tag: Manual revert |
||
(19 intermediate revisions by the same user not shown) | |||
Line 16: | Line 16: | ||
findAndExecute(".rgwiki-main-negative-tabindex", (element) => { | findAndExecute(".rgwiki-main-negative-tabindex", (element) => { | ||
element.tabIndex = -1; | element.tabIndex = -1; | ||
}); | |||
/* Links without href/links to "javascript:void(0)" are not allowed by MediaWiki, this is a workaround */ | |||
/* Use Template:EmptyLink to activate this piece of code. */ | |||
findAndExecute(".rgwiki-main-emptylink", (span) => { | |||
const a = span.querySelector("a"); | |||
for (const className of span.classList) { | |||
if (className !== "rgwiki-main-emptylink") { | |||
a.classList.add(className); | |||
} | |||
} | |||
a.href = "javascript:void(0)"; | |||
const parent = span.parentNode; | |||
parent.replaceChild(a, span); | |||
}); | }); | ||
Line 26: | Line 40: | ||
}); | }); | ||
/* | /* OS tabs */ | ||
findAndExecute(".rgwiki-main- | findAndExecute(".tabber.rgwiki-main-os-tabber", (tabber) => { | ||
const osString = (window.navigator.oscpu || window.navigator.platform || window.navigator.userAgent).toLowerCase(); | |||
let userOs = null; | |||
if (osString.includes("linux")) userOs = "linux"; | |||
if (osString.includes("mac")) userOs = "mac"; | |||
if (osString.includes("win")) userOs = "win"; | |||
const oldAnchor = window.location.hash; | |||
const | tabber.querySelectorAll(".tabber__header > .tabber__tabs > a.tabber__tab").forEach(tab => { | ||
const tabName = tab.textContent.toLowerCase(); | |||
if (userOs === "linux" && tabName.includes("linux")) tab.click(); | |||
if (userOs === "mac" && tabName.includes("macos")) tab.click(); | |||
if (userOs === "win" && tabName.includes("windows")) tab.click(); | |||
}); | }); | ||
setTimeout(() => { | |||
window.location.hash = oldAnchor; | |||
}, 100); | |||
}); | }); |
Latest revision as of 07:52, 11 March 2025
/* Any JavaScript here will be loaded for all users on every page load. */ /* Finds elements by a selector and executes a function for each element */ function findAndExecute(selector, callback) { const elements = document.querySelectorAll(selector); elements.forEach((element) => { try { callback(element); } catch (e) { console.error(`Error while initializing "${selector}"`, e); } }); } /* tabindex="-1" is not allowed by MediaWiki, this is a workaround */ findAndExecute(".rgwiki-main-negative-tabindex", (element) => { element.tabIndex = -1; }); /* Links without href/links to "javascript:void(0)" are not allowed by MediaWiki, this is a workaround */ /* Use Template:EmptyLink to activate this piece of code. */ findAndExecute(".rgwiki-main-emptylink", (span) => { const a = span.querySelector("a"); for (const className of span.classList) { if (className !== "rgwiki-main-emptylink") { a.classList.add(className); } } a.href = "javascript:void(0)"; const parent = span.parentNode; parent.replaceChild(a, span); }); /* Localized date */ findAndExecute(".rgwiki-main-date-localized", (span) => { const inputDateString = span.dataset.datetime !== undefined ? span.dataset.datetime : span.textContent; const date = new Date(inputDateString); const outputDateString = date.toString(); span.title = `This date in your timezone: ${outputDateString}`; }); /* OS tabs */ findAndExecute(".tabber.rgwiki-main-os-tabber", (tabber) => { const osString = (window.navigator.oscpu || window.navigator.platform || window.navigator.userAgent).toLowerCase(); let userOs = null; if (osString.includes("linux")) userOs = "linux"; if (osString.includes("mac")) userOs = "mac"; if (osString.includes("win")) userOs = "win"; const oldAnchor = window.location.hash; tabber.querySelectorAll(".tabber__header > .tabber__tabs > a.tabber__tab").forEach(tab => { const tabName = tab.textContent.toLowerCase(); if (userOs === "linux" && tabName.includes("linux")) tab.click(); if (userOs === "mac" && tabName.includes("macos")) tab.click(); if (userOs === "win" && tabName.includes("windows")) tab.click(); }); setTimeout(() => { window.location.hash = oldAnchor; }, 100); });