MediaWiki:Common.js: Difference between revisions

From Good Creations! MC Wiki
Jump to navigation Jump to search
mNo edit summary
Tag: Reverted
m Fix
Tag: Reverted
Line 26: Line 26:


if ( extraCSS ) {
if ( extraCSS ) {
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
Line 35: Line 34:


if ( extraJS ) {
if ( extraJS ) {
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
Line 52: Line 50:
});
});


$(document).ready(function() {
// ---- WidgetBot Crate Widget ----
    // Dynamically load the external script for WidgetBot Crate
$.getScript('https://cdn.jsdelivr.net/npm/@widgetbot/crate@3', function () {
    $.getScript('https://cdn.jsdelivr.net/npm/@widgetbot/crate@3', function() {
    new Crate({
        new Crate({
        server: '1163030464903258122', // The 'Good Creations!' Minecraft Server
            server: '1163030464903258122', // The 'Good Creations!' Minecraft Server
        channel: '1163030468111908986', // #general-💬
            channel: '1163030468111908986', // #general-💬
        allChannelNotifications: true,
            allChannelNotifications: true,
        color: 'black'
            color: 'black'
        });
     });
     });
});


    // Load the Custom Elements Built-in polyfill for Safari
// ---- Custom Elements Built-in polyfill (Safari support) ----
(function () {
     const ceBuiltinScript = document.createElement('script');
     const ceBuiltinScript = document.createElement('script');
     ceBuiltinScript.src = 'https://unpkg.com/@ungap/custom-elements-builtin';
     ceBuiltinScript.src = 'https://unpkg.com/@ungap/custom-elements-builtin';
     ceBuiltinScript.async = false;
     ceBuiltinScript.async = false;
     document.head.appendChild(ceBuiltinScript);
     document.head.appendChild(ceBuiltinScript);
})();


    // Load the x-frame-bypass script and inject as classic script
// ---- X-Frame-Bypass loader ----
    fetch('https://unpkg.com/x-frame-bypass')
fetch('https://unpkg.com/x-frame-bypass')
        .then(res => res.text())
    .then(res => res.text())
        .then(code => {
    .then(code => {
            const script = document.createElement('script');
        const script = document.createElement('script');
            script.textContent = code;
        script.textContent = code;
            document.head.appendChild(script);
        document.head.appendChild(script);
        })
    })
        .catch(err => console.error('Failed to load x-frame-bypass:', err));
    .catch(err => console.error('Failed to load x-frame-bypass:', err));
});

Revision as of 18:20, 2 June 2025

/*
 * This is the JS for all skins (for both mobile and desktop) on MediaWiki.org.
 * Consider whether you can use a gadget with one of the options set to limit
 * where it is loaded. See [[Extension:Gadgets#Options]].
 */
/* global mw, $ */
/**
 * Hide prefix in category
 *
 * @source https://www.mediawiki.org/wiki/Snippets/Hide_prefix_in_category
 * @rev 5
 */
var prefix = $.trim( $( '#mw-cat-hideprefix' ).text() ) || ( mw.config.get( 'wgTitle' ) + '/' );
$( '#mw-pages' ).find( 'a' ).text( function ( i, val ) {
    return val.slice( 0, prefix.length ) === prefix ? val.slice( prefix.length ) : val;
} );

/**
 * @source https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
 * @revision 2017-05-16
 */
mw.loader.using( ['mediawiki.util'], function () {
	var extraCSS = mw.util.getParamValue( 'withCSS' ),
		extraJS = mw.util.getParamValue( 'withJS' ),
		extraModule = mw.util.getParamValue( 'withModule' );

	if ( extraCSS ) {
		if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
			mw.loader.load( '/w/index.php?title=' + encodeURIComponent( 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 ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
			mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
		}
	}

	if ( extraModule ) {
		if ( /^ext\.gadget\.[^,\|]+$/.test( extraModule ) ) {
			mw.loader.load( extraModule );
		} else {
			mw.notify( 'Only gadget modules are allowed.', { title: 'Invalid withModule value' } );
		}
	}
});

// ---- WidgetBot Crate Widget ----
$.getScript('https://cdn.jsdelivr.net/npm/@widgetbot/crate@3', function () {
    new Crate({
        server: '1163030464903258122', // The 'Good Creations!' Minecraft Server
        channel: '1163030468111908986', // #general-💬
        allChannelNotifications: true,
        color: 'black'
    });
});

// ---- Custom Elements Built-in polyfill (Safari support) ----
(function () {
    const ceBuiltinScript = document.createElement('script');
    ceBuiltinScript.src = 'https://unpkg.com/@ungap/custom-elements-builtin';
    ceBuiltinScript.async = false;
    document.head.appendChild(ceBuiltinScript);
})();

// ---- X-Frame-Bypass loader ----
fetch('https://unpkg.com/x-frame-bypass')
    .then(res => res.text())
    .then(code => {
        const script = document.createElement('script');
        script.textContent = code;
        document.head.appendChild(script);
    })
    .catch(err => console.error('Failed to load x-frame-bypass:', err));