MediaWiki:Common.js
Utseende
Merk: Etter publisering vil det kanskje være nødvendig å slette mellomlageret i nettleseren din for å se endringene.
- Firefox / Safari: Hold Shift mens du klikker på Oppdater, eller trykk enten Ctrl+F5 eller Ctrl+R (⌘+R på Mac)
- Google Chrome: Trykk Ctrl+Shift+R (⌘+Shift+R på Mac)
- Edge: Hold Ctrl mens du trykker på Oppdater eller trykk Ctrl+F5
/* Javascript i denne fila vil gjelde for alle drakter. */
/**
* Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
* loaded for all users on every wiki page. If possible create a gadget that is
* enabled by default instead of adding it here (since gadgets are fully
* optimized ResourceLoader modules with possibility to add dependencies etc.)
*
* Since Common.js isn't a gadget, there is no place to declare its
* dependencies, so we have to lazy load them with mw.loader.using on demand and
* then execute the rest in the callback. In most cases these dependencies will
* be loaded (or loading) already and the callback will not be delayed. In case a
* dependency hasn't arrived yet it'll make sure those are loaded before this.
*/
/* global mw, $ */
/* jshint strict:false, browser:true */
// CommentStreams relative dates
( function () {
var ONE_MONTH_SEC = 30 * 86400;
function formatRelative( diffSec ) {
var units = [
{ name: 'år', s: 365 * 86400 },
{ name: 'mnd', s: 30 * 86400 },
{ name: 'dag', s: 86400, plural: 'dager' },
{ name: 'time', s: 3600, plural: 'timer' },
{ name: 'minutt', s: 60, plural: 'minutter' },
{ name: 'sekund', s: 1, plural: 'sekunder' }
];
for ( var i = 0; i < units.length; i++ ) {
var u = units[ i ];
var n = Math.floor( diffSec / u.s );
if ( n >= 1 ) {
var label = ( n > 1 && u.plural ) ? u.plural : u.name;
return 'for ' + n + '\u00a0' + label + ' siden';
}
}
return 'akkurat nå';
}
function formatAbsolute( date ) {
return date.toLocaleString( 'nb-NO', {
day: 'numeric', month: 'long', year: 'numeric',
hour: '2-digit', minute: '2-digit', hour12: false
} );
}
function convertSpan( span ) {
if ( span.dataset.csConverted ) return;
span.dataset.csConverted = '1';
var original = span.textContent;
var clean = original
.replace( /^\s*\|?\s*Pos(?:t(?:et|ed)):?\s*/i, '' )
.trim()
.replace( /\s+at\s+/i, ', ' );
var date = new Date( clean );
if ( isNaN( date.getTime() ) ) return;
var diffSec = ( Date.now() - date.getTime() ) / 1000;
var formatted = diffSec < ONE_MONTH_SEC
? formatRelative( diffSec )
: formatAbsolute( date );
span.textContent = original.replace(
/(\|\s*Pos(?:t(?:et|ed)):?\s*).*$/i,
'$1' + formatted
);
}
function convertAll( root ) {
( root || document ).querySelectorAll( '.cs-comment-details' ).forEach( convertSpan );
}
var observer = new MutationObserver( function ( mutations ) {
mutations.forEach( function ( mutation ) {
mutation.addedNodes.forEach( function ( node ) {
if ( node.nodeType !== 1 ) return;
if ( node.matches( '.cs-comment-details' ) ) {
convertSpan( node );
} else {
convertAll( node );
}
} );
} );
} );
mw.hook( 'wikipage.content' ).add( function () {
convertAll();
observer.observe( document.body, { childList: true, subtree: true } );
} );
}() );
// toppgir
if (document.querySelector('#boxToppgir')) {
// for mobil
if (document.querySelector('.skin-minerva')) {
const mwbody = document.querySelector(".mw-body")
mwbody.style.backgroundImage = "url(https://www.tromsleksikon.org/w/images/b/b5/Car_crash_2tromsleksikon.jpg)"
const headingholder = document.querySelector(".heading-holder")
headingholder.style.backgroundColor = "rgb(255 255 255 / 85%)"
headingholder.style.padding = "4px"
const bodycontent = document.querySelector("#bodyContent")
bodycontent.style.backgroundColor = "rgb(255 255 255 / 85%)"
bodycontent.style.padding = "4px"
} else {
const pagecontainer = document.querySelector(".mw-page-container")
pagecontainer.style.backgroundImage = "url(https://www.tromsleksikon.org/w/images/b/b5/Car_crash_2tromsleksikon.jpg)"
document.body.style.backgroundImage = "url(https://www.tromsleksikon.org/w/images/b/b5/Car_crash_2tromsleksikon.jpg)"
const contentcontainer = document.querySelector(".mw-content-container")
contentcontainer.style.backgroundColor = "rgb(255 255 255 / 85%)"
contentcontainer.style.padding = "5px"
const sitenotice = document.querySelector("#siteNotice")
sitenotice.style.visibility = "hidden"
}
}
// main menu visible code from https://sanat.csc.fi/
if (!mw.config.get('wgUserName')) {
const observer = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.target.classList.contains('vector-animations-ready')) {
document.querySelector( '[data-event-name="pinnable-header.vector-main-menu.pin"]' ).click()
//document.querySelector( '[data-event-name="pinnable-header.vector-appearance.unpin"]' ).click()
document.querySelector( '[data-event-name="pinnable-header.vector-page-tools.pin"]' ).click()
document.body.focus()
document.body.click()
observer.disconnect();
// refocus body or ordle
document.body.focus()
document.body.click()
if (window.location.pathname == '/wiki/Ordle') {
window.frames['iframe-Ordle'].focus()
document.body.addEventListener('click', () => { window.frames['iframe-Ordle'].focus() }, true);
}
break;
}
}
});
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
}
// Gjør nyere troms klikkbart. Gjør kartet på forsiden sånn at du klikker på den går den i fullskjerm og viser artikler i nærheten
if (window.location.pathname == '/wiki/Forside') {
var iframe = $("#iframe-Nyheter")
iframe.css("pointer-events", "none")
iframe[0].scrolling = "no"
document.getElementsByClassName('nyereTroms')[0].onclick = () => {window.open('https://nyheter.tromsleksikon.org/')}
// debounce for clicking on kartographer map. clicking on it clicks it twice for some reason
var debounce
document.getElementsByClassName('mw-kartographer-map')[0].onclick = () => {
clearTimeout(debounce)
debounce = setTimeout(() => {
// click the fullscreen button
var fullscreenBtn = document.querySelector( '.leaflet-control .oo-ui-icon-fullScreen' );
if ( fullscreenBtn ) {
fullscreenBtn.click();
clearTimeout(debounce)
setTimeout( function () {
// click the Nearby Articles button
var nearbyBtn = document.querySelector( '.mw-kartographer-buttonfoot .oo-ui-buttonElement-button' );
if ( nearbyBtn ) {
nearbyBtn.click();
clearTimeout(debounce)
} else {
// in case of slow internet
setTimeout( function () {
var nearbyBtn = document.querySelector( '.mw-kartographer-buttonfoot .oo-ui-buttonElement-button' );
if ( nearbyBtn ) {
nearbyBtn.click();
clearTimeout(debounce)
}
}, 6000)
}
}, 1000 );
}
}, 50)
}
}
if (window.location.pathname == '/wiki/Ordle') {
window.frames['iframe-Ordle'].focus()
document.body.addEventListener('click', () => { window.frames['iframe-Ordle'].focus() }, true);
}
// scroll mujaffa into view
if (window.location.pathname == '/wiki/Mujaffa-spillet') {
document.getElementById("iframe-Mujaffa").scrollIntoView();
window.scrollBy(0, -100);
setTimeout(() => {
document.getElementById("iframe-Mujaffa").scrollIntoView();
window.scrollBy(0, -100);
setTimeout(() => {
document.getElementById("iframe-Mujaffa").scrollIntoView();
window.scrollBy(0, -100);
setTimeout(() => {
document.getElementById("iframe-Mujaffa").scrollIntoView();
window.scrollBy(0, -100);
}, 3500);
}, 1200);
}, 800);
}
// tilfeldig redigertutorialpopupsjanse
// if not front page or any page with colon (likely another namespace)
if (window.location.pathname != '/wiki/Forside' && !window.location.pathname.includes(':') && !window.location.pathname.includes('/w/index.php') && window.location.pathname != '/wiki/Mujaffa-spillet' && window.location.pathname != '/wiki/Ordle' && window.location.pathname != '/wiki/KRIG_I_INDRE_TROMS') {
mw.loader.using( 'ext.guidedTour' ).then( function () {
// Helper: random chance, here 10% of pageviews
function randomChance(prob) {
return Math.random() < prob;
}
// Check: is user logged in?
var isAnon = mw.config.get('wgUserName') === null;
// TODO: Add "inactive user" check if desired
// For now: run for anons only
if ( isAnon && randomChance(0.1) ) {
// Redirect into tour, or launch explicitly
mw.guidedTour.launchTour('redigertutorial');
}
});
}
// Custom code ends here
mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
/* Begin of mw.loader.using callback */
/**
* Map addPortletLink to mw.util
* @deprecated: Use mw.util.addPortletLink instead.
*/
mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );
/**
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
* @rev 6
*/
var extraCSS = mw.util.getParamValue( 'withCSS' ),
extraJS = mw.util.getParamValue( 'withJS' );
if ( extraCSS ) {
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
}
}
if ( extraJS ) {
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
}
}
/**
* Collapsible tables; reimplemented with mw-collapsible
* Styling is also in place to avoid FOUC
*
* Allows tables to be collapsed, showing only the header. See GammelLenke:[<nowiki />.
* @version 3.0.0 (2018-05-20)
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
* @author GammelLenke:[<nowiki />
* @author GammelLenke:[<nowiki />
* @author GammelLenke:[<nowiki />
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
* is supported in MediaWiki core. Shimmable since MediaWiki 1.32
*
* @param {jQuery} $content
*/
function makeCollapsibleMwCollapsible( $content ) {
var $tables = $content
.find( 'table.collapsible:not(.mw-collapsible)' )
.addClass( 'mw-collapsible' );
$.each( $tables, function ( index, table ) {
// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
if ( $( table ).hasClass( 'collapsed' ) ) {
$( table ).addClass( 'mw-collapsed' );
// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
}
} );
if ( $tables.length > 0 ) {
mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
$tables.makeCollapsible();
} );
}
}
mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );
/**
* Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
*
* Maintainers: TheDJ
*/
function mwCollapsibleSetup( $collapsibleContent ) {
var $element,
$toggle,
autoCollapseThreshold = 2;
$.each( $collapsibleContent, function ( index, element ) {
$element = $( element );
if ( $element.hasClass( 'collapsible' ) ) {
$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
}
if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
$element.data( 'mw-collapsible' ).collapse();
} else if ( $element.hasClass( 'innercollapse' ) ) {
if ( $element.parents( '.outercollapse' ).length > 0 ) {
$element.data( 'mw-collapsible' ).collapse();
}
}
// because of colored backgrounds, style the link in the text color
// to ensure accessible contrast
$toggle = $element.find( '.mw-collapsible-toggle' );
if ( $toggle.length ) {
// Make the toggle inherit text color (Updated for T333357 2023-04-29)
if ( $toggle.parent()[ 0 ].style.color ) {
$toggle.css( 'color', 'inherit' );
$toggle.find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
}
}
} );
}
mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );
/* End of mw.loader.using callback */
} );
/* DO NOT ADD CODE BELOW THIS LINE */