User:Andrybak/Scripts/Vectron.js
Appearance
< User:Andrybak | Scripts
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Andrybak/Scripts/Vectron. |
// <nowiki>
(function() {
'use strict';
const USERSCRIPT_NAME = 'Vectron';
const config = {
wikipage: '[[:en:User:Andrybak/${USERSCRIPT_NAME}|${USERSCRIPT_NAME}]]',
version: '3'
};
const LOG_PREFIX = `[${USERSCRIPT_NAME} v${config.version}]:`;
const mw = window.mw; // a hack to trick my JS editor into believing that `mw` exists
function error(...toLog) {
console.error(LOG_PREFIX, ...toLog);
}
function warn(...toLog) {
console.warn(LOG_PREFIX, ...toLog);
}
function info(...toLog) {
console.info(LOG_PREFIX, ...toLog);
}
function debug(...toLog) {
console.debug(LOG_PREFIX, ...toLog);
}
function getLimitedWidthToggleButton() {
return document.querySelector('#p-dock-bottom button.vector-limited-width-toggle');
}
function isWideVector(button) {
return button.getAttribute('data-event-name').endsWith('on');
}
function isNarrowVector(button) {
return !isWideVector(button);
}
function maybeShowHashTarget() {
/*
* Because we are messing with the layout of the page
* on the fly, we need to ensure that the user sees
* the linked section.
*/
if (document.location.hash === "") {
return;
}
const targetId = document.location.hash.slice(1).replaceAll(' ', '_');
document.getElementById(targetId)?.scrollIntoView();
}
function ensureWide() {
const button = getLimitedWidthToggleButton();
if (isWideVector(button)) {
debug('Already wide.');
return;
}
info('Widening.');
button.click();
maybeShowHashTarget();
}
function ensureNarrow() {
const button = getLimitedWidthToggleButton();
if (isNarrowVector(button)) {
debug('Already narrow.');
return;
}
info('Narrowing.');
button.click();
maybeShowHashTarget();
}
/*
* The main function of the script.
*/
function runScript() {
/*
* Reference documentation about keys and values in mw.config:
* https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#mw.config
*/
if (!mw.config.get('wgIsArticle')) { // This variable is badly named -- it is not related to a page being a main namespace "article".
info('Not a wiki page.');
ensureWide();
return;
}
if (mw.config.get('wgDiffNewId') != null || mw.config.get('wgDiffOldId') != null) {
info('Diff view.');
ensureWide();
return;
}
const namespaceNumber = mw.config.get('wgNamespaceNumber');
if (namespaceNumber == -1) {
info('This is a "Special:" page.');
ensureWide();
return;
}
const contentModel = mw.config.get("wgPageContentModel");
// ['javascript', 'css', 'sanitized-css', 'Scribunto'].includes(contentModel)
if (contentModel !== 'wikitext') {
info('Content model of the page is for source code (Lua, JS, CSS, etc).');
ensureWide();
return;
}
info('Assuming wiki page.');
ensureNarrow();
}
function wait(message) {
info(message);
setTimeout(lazyLoadVectron, 200);
}
/*
* Infrastructure to ensure the script can run.
*/
function lazyLoadVectron() {
debug('Loading...');
const skinId = mw.config.get("skin");
if (skinId === null) {
wait('Skin is not loaded yet. Waiting...');
return;
}
if (skinId !== 'vector-2022') {
warn(`Skin ${skinId} is not supported by the script. Aborting.`);
return;
}
const button = getLimitedWidthToggleButton();
if (button == null) {
wait('The toggle button is not loaded yet. Waiting...');
return;
}
const theAttribute = button.getAttribute('data-event-name');
if (theAttribute == null) {
wait('Attribute "data-event-name" is not loaded yet. Waiting...');
return;
}
runScript();
}
if (document.readyState !== 'loading') {
lazyLoadVectron();
} else {
warn('Cannot load yet. Setting up a listener...');
document.addEventListener('DOMContentLoaded', lazyLoadVectron);
}
})();
// </nowiki>