isnichwahr HTML5 player

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);