User:Colouratura/wikia.js

/** * This page provides wiki-based access to scripts. * * Some of this content requires access to an external server to use and as such * is unuseable without access. Please do not try to copy this verbatim. If you * are interested in whole or in part please leave a message on my Message Wall * and I will see if there is something I can work out for you. */ ( function ( mw, $, mainRoom ) {   /**     * Extend config options     *      * This snippet adds the wgWikiName variable to the mw.config object     *      * @return {string} wiki subdomain     */    mw.config.values[ 'wgWikiName' ] = function  {        return window.location.toString.split( '.wikia.com' )[ 0 ].slice( 7 );    };    /**     * Logging functions     *      * These functions provide more terse methods for logging information     * to the browser console.     *      * Methods:     *   - error: Used for specific script errors     *   - loaded: Used to log when external assets are loaded     */    const log = {        /**         * Logs a script error         *          * Logs a script error with the particular name of the script since         * all of them are concatenated in one file.         *          * @param {string} script         * @return {function}         */ error: function ( script, error ) { const context = '[SCRIPT ERROR]:'; return Function.prototype.bind.call(               console.error,                console,                context            ); },       /**         * Logs a loaded resource *         * Logs when a resource has been loaded from an external source *         * @param {string} res * @param {string} script * @return {function} */       loaded: function ( res, source, script ) { const context = '[LOADED RESOURCE]:'; return Function.prototype.bind.call(               console.log,                console,                context            ); },   };    /**     * YouTube info *     * When a YouTube link is posted to chat this script detects it and fetches * some basic information about it to display inline. *     * WARNING: This script relies on access to an external server you will not * have access to. Please do not attempt to use itas it will not function. */   ( function ( mw, $, mainRoom, log ) {        $.get( 'http://dikdik.life:5253/youtube_api_key' )            .then( function ( data ) { log.loaded( 'youtube_api_key FROM dikdik.life:5253 IN YouTube Info' );

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Chat' ) { NodeChatDiscussion.prototype.ytInfoCache = {}; NodeChatDiscussion.prototype.ytInfo = function ( chat ) { if ( mainRoom.isInitialized && !chat.attributes.isInlineAlert ) { var text = chat.attributes.text, search = /https?:\/\/(?:www\.|m\.)?youtube\.com[^ ]+v=([^&# ]*)|https?:\/\/(?:www\.)?youtu\.be\/([^&#\? ]*)/im, apikey = data, match = text.match( search );

if ( match ) { var $link = $( "a[href='" + match[0] + "']" ).last, id = match[1] ? match[1] : match[2];

if ( this.ytInfoCache.hasOwnProperty( id ) ) { return $link.attr( 'title', this.ytInfoCache[id] ); }

$.get( 'https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics,contentDetails&id=' + id + '&key=' + apikey, function( data ) {                                   try {                                        data = data[ 'items' ][ 0 ];                                        var info = "[Title]: " + data[ 'snippet' ][ 'title' ] + "\n[Channel]: " + data[ 'snippet' ][ 'channelTitle' ] + "\n[Views]: " + data[ 'statistics' ][ 'viewCount' ];                                        mainRoom.viewDiscussion.ytInfoCache[ id ] = info;                                        $link.attr( 'title', info );                                    } catch ( e ) {                                        log.error( 'YouTube Info: Error fetching data from YouTube API.' );                                    }                                } ); }                       }                    };

mainRoom.model.chats.bind( 'afteradd', $.proxy( mainRoom.viewDiscussion.ytInfo, mainRoom.viewDiscussion ) ); }           } )            .fail( function ( error ) { log.error( 'YouTube Info: ' + error ); } );   }( mw, $, mainRoom, log ) ); }( mediaWiki, jQuery, ( typeof mainRoom === undefined ) ? {} : mainRoom ) );