MediaWiki:Common.js

From Comprehensible Input Wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
//Wait for page to be parsed
$(document).ready( function () {
	if(window.location.hostname == 'comprehensibleinput.miraheze.org'){
		
		currentUrl = new RegExp("(^.*)" + window.location.hostname + "(.*$)", 'g').exec(window.location);
		newUrl = currentUrl[1] + "comprehensibleinputwiki.org" + currentUrl[2];
		
		$('#bodyContent').prepend('<div id="newUrlDiv" style="display:none">New URL! Go to <a href="' + newUrl + '"><b>comprehensibleinputwiki.org</b></a>!</div>');
		document.querySelector('#newUrlDiv').style.display="block"	
	}

    mw.loader.load( '/w/index.php?title=Mediawiki:Datatables.js&action=raw&ctype=text/javascript' );
    mw.loader.load( '/w/index.php?title=Mediawiki:Datatables_custom.js&action=raw&ctype=text/javascript' );
    
    console.log("test4");
    
    // Insert random video on main page
    $( function () {
    	
    	if(document.querySelector('.mw-page-title-main').innerHTML == "Main Page"){
    
		    api = new mw.Api();
		    
		    api.parse(
		        '{{#invoke:Random Video|randomVideo}}' )
		    .done( function ( response ) {
		        regex = /(\[.+\])/g;
		        
		        console.log(response.match(regex)[0]);
		        
		        vidData = JSON.parse(response.match(regex)[0]);
		        
		        nextButtonCode= '<div style="text-align:center"><img class="nextbutton-image" src="https://upload.wikimedia.org/wikipedia/commons/1/1e/Refresh_%28259956%29_-_The_Noun_Project.svg"></div>';
		
		        fullHtml = '<div id="vidHolder"><div style="text-align:center"><b>Random video from the <a href="/wiki/Library">Library</a></b></div>' + nextButtonCode;
		       
		
		        for(g=0; g<vidData.length; g++) {
		            activeClass = (g==0) ? 'random-active' : 'random-hidden';
		            
		            evHtml = '<figure class="embedvideo" data-service="youtube" data-iframeconfig="{&quot;class&quot;:&quot;embedvideo-player&quot;,&quot;loading&quot;:&quot;lazy&quot;,&quot;frameborder&quot;:0,&quot;allow&quot;:&quot;accelerometer; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; autoplay&quot;,&quot;modestbranding&quot;:1,&quot;width&quot;:320,&quot;height&quot;:180,&quot;src&quot;:&quot;//www.youtube-nocookie.com/embed/' + vidData[g].id + '?autoplay=1&quot;}" style="width:320px; margin:auto"><span class="embedvideo-wrapper" style="height:180px"><div class="embedvideo-consent" data-show-privacy-notice="1"><div class="embedvideo-overlay"><div class="embedvideo-loader" role="button"><div class="embedvideo-loader__fakeButton">Load video</div><div class="embedvideo-loader__footer"><div class="embedvideo-loader__service">YouTube</div></div></div><div class="embedvideo-privacyNotice hidden"><div class="embedvideo-privacyNotice__content">YouTube might collect personal data. <a href="https://www.youtube.com/howyoutubeworks/user-settings/privacy/" rel="nofollow,noopener" target="_blank" class="embedvideo-privacyNotice__link">Privacy Policy</a></div><div class="embedvideo-privacyNotice__buttons"><button class="embedvideo-privacyNotice__continue">Continue</button><button class="embedvideo-privacyNotice__dismiss">Dismiss</button></div></div></div></div></span></figure>';
		
		            fullHtml += '<div id="random-vid" class="' + activeClass + '"><div style="text-align:center"><a href="/wiki/' + vidData[g].language + '/Library">' + vidData[g].language + '</a> (' + vidData[g].difficulty + ')</div><div style="width:min-content;margin:auto">' + evHtml + '</div><div style="text-align:center">' + vidData[g].title + '<br>Channel: ' + vidData[g].channel + '</div></div>';
		
		            
		        }
		
		        fullHtml += '</div>';
		
		        document.querySelector('#summary_link').insertAdjacentHTML('afterend' , fullHtml);
		
		        mw.loader.using( 'ext.embedVideo.styles' , function() {
		            mw.loader.load( 'ext.embedVideo.overlay' );
		            mw.loader.load( 'ext.embedVideo.consent' );
		        });
		
		            nextButton = document.querySelector('.nextbutton-image');
		        
		            nextButton.addEventListener('click', function(e) {
		                current = document.querySelector('.random-active');
		                console.log(current);
		                current.classList.remove('random-active');
		                current.classList.add('random-hidden');
		                current.nextSibling.classList.remove('random-hidden');
		                current.nextSibling.classList.add('random-active');
		            });
		
		        
		    } );
    	}
	
	} );

});