WorldStar Flash to <video>

Replaces videos embedded with flash with the direct video

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         WorldStar Flash to <video>
// @namespace    http://kmcgurty.com
// @version      1.1
// @description  Replaces videos embedded with flash with the direct video
// @author       Kmc - [email protected]
// @match        *://*.worldstarhiphop.com/videos/*
// @noframes
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @resource     vjsCSS https://vjs.zencdn.net/5.4.6/video-js.min.css
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/video.js/5.5.3/video.min.js
// ==/UserScript==
/* jshint -W097 */

(function() {
	'use strict';

	unsafeWindow.VIDEOJS_NO_BASE_THEME = false;
	window.VIDEOJS_NO_BASE_THEME = false;
	VIDEOJS_NO_BASE_THEME = false;

	var vjsCSS = GM_getResourceText("vjsCSS");
	GM_addStyle(vjsCSS);
	var vjsSkin = GM_getResourceText("vjsSkin");
	GM_addStyle(vjsSkin);

	//easier to read than having indexOf everywhere
	String.prototype.contains = function(array) {
		var contains = false,
			i;

		for (i = 0; i < array.length; i++) {
			contains = this.indexOf(array[i]) != -1;

			return contains;
		}
	};


	findFlashPlayer();

	function findFlashPlayer() {
		var flashObject = $("embed");

		if (flashObject[0]) {
			var flashVars = flashObject.attr('flashvars').split('&');

			for (var i = 0; i < flashVars.length; i++) {
				if (flashVars[i].contains([".mp4", ".webm", ".ogg"])) {

					var directMP4url = flashVars[i].replace("file=", "");

					replaceFlashPlayer(flashObject, directMP4url);

					return;
				} else if (flashVars[i].contains(["youtube.com", "youtu.be"])) {
					var youtubeURL = flashVars[i].replace("file=", "");

					youtubeURL = youtubeURL.replace("/v/", "/embed/");

					embedYoutubeVideo(flashObject, youtubeURL);

					return;
				}
			}

			//html5 <video> tag only accepts mp4, webm, ogg
			console.log(i, "Unable to replace the flash player (file type not supported)");
		}
	}


	function embedYoutubeVideo(divToReplace, youtubeURL) {
		$(divToReplace).replaceWith('<iframe type="text/html" width="640" height="390" src="' + youtubeURL + '" frameborder="0"/>');
	}


	function replaceFlashPlayer(divToReplace, videoSource) {

		var defaultPlayerVolume = .5;

		$(divToReplace).replaceWith('<video class="video-js vjs-sublime-skin" width="640" height="390" controls="controls" data-setup=\'{}\'>\
										 <source src="' + videoSource + '" type="video/mp4">\
									 </video>');

		var videoPlayer = $("video")[0];
		var userVolume = GM_getValue("video_volume", defaultPlayerVolume);

		videoPlayer.volume = userVolume;

		addListeners(videoPlayer);
	}

	function addListeners(videoPlayer) {
		videoPlayer.onvolumechange = function(volume) {
			GM_setValue("video_volume", videoPlayer.volume);
		};
	}

	MutationObserver = unsafeWindow.MutationObserver || unsafeWindow.WebKitMutationObserver;

	var observer = new MutationObserver(function(mutations, observer) {
		findFlashPlayer();
	});

	observer.observe(document, {
		subtree: true,
		attributes: true
	});
}());