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 ) {   // Global object    window.colour = {};    /**     * 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 ) ); /**    * Conflictagories *     * Runs a category check against every page on a wiki *     * WARNING WARNING WARNING WARNING WARNING WARNING * DO NOT UNDER ANY CIRCUMSTANCES USE THIS EVER * WARNING WARNING WARNING WARNING WARNING WARNING */   ( function ( $, mw ) {        if ( mw.config.get( 'wgWikiName' ) !== 'creepypasta' ) return;        var conflicts = [            [ 'Monster', 'Ghosts', 'Demon/Devil', 'Beings', 'Gods', 'Cryptids' ],            [ 'Books', 'Diary/Journal', 'Items/Objects' ],            [ 'Lost Episodes', 'Television' ],            [ 'Pokemon', 'Zelda', 'Video Games' ],            [ 'Holders', 'Ritual' ],            [ 'Holders', 'Items/Objects' ],            [ 'Holders', 'Places' ],            [ 'HPL', 'Lovecraftian', 'Suggested Reading' ]        ];

var containsMultiple = function ( categories, conflictagories ) { var contains = 0;

categories.forEach( function ( item ) {               if ( conflictagories.includes( item ) )                    contains++;            } );

return ( contains > 2 ); };

var getCategories = function ( page ) { return new Promise( function ( resolve, reject ) {               var uri = 'http://creepypasta.wikia.com/api.php',                    url = uri + '?action=query&titles=' + encodeURIComponent( page ) + '&prop=categories&format=json';                    url += '&cb=' + new Date.toUTCString;

$.get( url ) .then( function ( data ) {                       if ( data.error ) reject( data );

data = data.query.pages;

for ( var id in data ) { if ( typeof data[ id ].categories !== undefined ) resolve( data[ id ].categories ); else resolve( [ 0 ] ); }                   } )                    .fail( function ( error ) { reject( error ); } );           } );        };

var checkConflictagories = function ( pages ) { if ( pages.length > -1 ) { pages.forEach( function ( page ) {                   setTimeout( function  { getCategories( page ) .then( function ( cats ) {                               try {                                    cats = cats.map( function ( n ) { return n.title.split( ':' )[ 1 ]; } );

conflicts.forEach( function ( confSet ) {                                       if ( cats.length > -1 && containsMultiple( cats, confSet ) ) {                                            console.log( page + ' has conflictagories!' );                                        }                                    } ); } catch ( e ) { } } );                   }, 1000 );                } );            }        };

var getAllPages = function ( cont ) { var uri = 'http://creepypasta.wikia.com/api.php', url = uri + '?action=query&list=allpages&apfrom=A&aplimit=500&apnamespace=0&format=json'; if ( cont ) { url += '&apfrom=' + cont; }

$.get( url ) .then( function ( data ) {                   var pages = data.query.allpages.map( function ( n ) { return n.title; } );

checkConflictagories( pages ); setTimeout( function {                        getAllPages( data[ 'query-continue' ].allpages.apfrom );                    }, 60000 ); } )               .fail( function ( err ) { console.log( err.status ); } );       };        window.colour.conflictagories = {            'start': getAllPages        };    }( $, mw ) ); }( mediaWiki, jQuery, ( typeof mainRoom === undefined ) ? mainRoom : {} ) );