isnichwahr HTML5 player

Use HTML5 JW/youtube player instead of Flash ones; works with new design as of September 2015

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        isnichwahr HTML5 player
// @namespace   nick.FirebirdDE
// @description Use HTML5 JW/youtube player instead of Flash ones; works with new design as of September 2015
// @include     http://www.isnichwahr.de/r*.html
// @version     1
// @grant       none
// @run-at      document-start
// ==/UserScript==

var jwplayer_match          = /^(?:\r?\n|\s)*jwplayer\(/;
var jwplayer_primary_match  = /primary:\s+'flash'/;
var youtube_match           = /^(?:\r?\n|\s)*SFYouTubePlayer\.embedPlayer\("([^"]+)",\s+([0-9]+),\s+([0-9]+),\s+"([^"]+)"/;

function node_replace(e, new_code) {
  // Replace the script pointed to by beforescriptexecute event e with the code
  // from new_code
  e.preventDefault();
  var new_script = document.createElement("script");
  new_script.textContent = new_code;
  e.target.parentNode.insertBefore(new_script, e.target);  
  e.target.parentNode.removeChild(e.target);
  e.stopProgagation();
}

window.addEventListener("beforescriptexecute", function(e) {
  // jwPlayer
  if(e.target.textContent.match(jwplayer_match) && e.target.textContent.match(jwplayer_primary_match)) {
    var new_code = e.target.textContent.replace(jwplayer_primary_match, "primary: 'html5'");
    node_replace(e, new_code);   
  }
  
  // youtube
  var yt_match = e.target.textContent.match(youtube_match);
  if(yt_match) {
    var new_code = "document.getElementById('" + yt_match[4] + "').innerHTML = '<iframe allowfullscreen width=\"" + yt_match[2] +
      "\" height=\"" + yt_match[3] + "\" frameborder=0 src=\"http://www.youtube.com/embed/" + yt_match[1] + "\"></iframe>';";
    console.log(new_code);
    node_replace(e, new_code);
  }
}, true);