User:K599/Niconico.js

//Author: K599 / Kelvs599

//Code based on https://dev.fandom.com/wiki/MediaWiki:YoutubePlayer/code.js

//Loading this JavaScript allows Niconico videos to be embedded. Go to your MediaWiki:Common.js, or wherever you're loading your JS, and either copy this page's code or insert the following line: //mw.loader.load('https://meta.miraheze.org/w/index.php?title=User:K599/Niconico.js&action=raw&ctype=text/javascript');

//You embed by inserting the following code: // //The full list of parameters is as follows: // //data-id: The ID of the video, which is found after the "watch/" in the video's URL. //data-from: Set when the video starts in seconds from the start. //Default width and height is 640x360.

mw.hook('wikipage.content').add(function($content) {   $content.find('.niconico:not(.loaded)').each(function { var $this = $(this), data = $this.data, uri = new mw.Uri('https://embed.nicovideo.jp/watch/'), width = typeof data.width === 'number' ? data.width + 'px' : String(data.width || '').trim, height = typeof data.height === 'number' ? data.height + 'px' : String(data.height || '').trim;

uri.path += String(data.id || '').trim; uri.query = { from: String(data.from || '').trim };

$this.html(           $(' ', { src: uri.toString, style: 'width: ' + (width ? width : height ? 'calc(' + height + ' * 16 / 9)' : '640px') + '; height: ' + (height ? height : width ? 'calc(' + width + ' * 9 / 16)' : '360px') + ';', frameborder: '0', allowfullscreen: 'true' })       ).addClass('loaded'); }); });