Twitter HTML5 Video Error to Link

Replaces error message with a link to load the media in a new tab where hopefully a plugin will play it

目前為 2016-05-12 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Twitter HTML5 Video Error to Link
// @description Replaces error message with a link to load the media in a new tab where hopefully a plugin will play it
// @namespace   JeffersonScher
// @include     https://amp.twimg.com/*
// @include     https://video.twimg.com/*
// @include     https://twitter.com/*
// @include     https://mobile.twitter.com/*
// @version     1.3
// @grant       none
// ==/UserScript==

var t_errcheck;
t_errcheck = window.setInterval(function (){errCheck()}, 500);
function errCheck(){
  var errdiv = document.querySelector('div.error-msg');
  if (errdiv){
    if (errdiv.textContent.indexOf("does not support video playback") > -1){
      window.clearInterval(t_errcheck);
      gotoLink();
    }
  }
}
var t_mp4check;
t_mp4check = window.setInterval(function (){mp4Check()}, 500);
function mp4Check(){
  var mp4v = document.querySelector('video[src*=".mp4"]');
  if (mp4v){
    window.clearInterval(t_mp4check);
    gotoLink();
  }
}
function gotoLink(){
  var vsrc = document.querySelector('video source');
  if (!vsrc) vsrc = document.querySelector('video[src]');
  if (vsrc){
    var t = vsrc.getAttribute("type");
    if (t){
      var mt = navigator.mimeTypes[t];
      if (mt === undefined || mt.enabledPlugin === null) var p = "the applicable plugin (NOT DETECTED)";
      else {
        var pn = mt.enabledPlugin.name;
        if (pn.toLowerCase().indexOf("plugin") < 0 && pn.toLowerCase().indexOf("plug-in") < 0) pn += " plugin";
        var p = "the " +  pn;
      }
    } else {
      var p = "the applicable plugin (if any)";
    }
    var ih = 'Link to launch the media in a new tab where it should be handled by ' + p + ': <a href="' + vsrc.getAttribute("src") + '" target="_blank" style="text-decoration:underline">' + vsrc.getAttribute("src") + '</a>';
  } else {
    var ih = "SORRY! Cannot read video source.";
  }
  var pw = document.querySelector('div#playerContainer');
  if (!pw) pw = document.querySelector('div.MediaDetail-itemWrapper');
  pw.innerHTML = '<p style="color:#00f;background:#ffc">' + ih + '</p>';
  /* This is fail 
  if (vsrc.getAttribute("src").indexOf('.m3u8') > -1){
    pw.querySelector('a').addEventListener('click', makeVLC, false);
  }
  */
}
/*
function makeVLC(evt){
  // Derive target element
  var m3uLink = evt.target;
  // Create object
  var obj = document.createElement('object');
  obj.setAttribute('type', "application/x-vlc-plugin"); // not working with M3U8 content type
  obj.setAttribute('data', m3uLink.href);
  obj.setAttribute('autoplay', 'true');
  obj.setAttribute('style', 'width:100%; height:100%; display:block; margin-left:auto; margin-right:auto;');
  // Replace container
  document.body.innerHTML = "";
  document.body.appendChild(obj);
  // Cancel link navigation
  evt.preventDefault();
  evt.stopPropagation();
  return false;
}
*/