Lazy Embedded Video

Lazy load embedded videos from Youtube/Dailymotion/Vimeo/Rutube/Twitch/Ustream

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

  1. // ==UserScript==
  2. // @name Lazy Embedded Video
  3. // @namespace zeusex81@gmail.com
  4. // @description Lazy load embedded videos from Youtube/Dailymotion/Vimeo/Rutube/Twitch/Ustream
  5. // @version 2.2
  6. // @include *
  7. // @resource playIcon http://i.imgur.com/1aybyWN.png
  8. // @grant GM_getResourceURL
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. try {
  13. if(/youtube|dailymotion|vimeo|rutube|twitch|ustream/i.test(top.location.hostname))
  14. return;
  15. } catch(e) {}
  16. var testCSP = function() {
  17. if(!document.getElementById("zeusCSP")) {
  18. var script = document.createElement("SCRIPT");
  19. script.id = "zeusCSP";
  20. script.text = "CSP_AllowInlineScript = true;";
  21. document.head.appendChild(script);
  22. }
  23. return unsafeWindow.CSP_AllowInlineScript;
  24. };
  25. var html, a;
  26. var createHtml = function(url, iframe, api, background_img) {
  27. /(.*\/\/(?:[^.\/]+\.)?([^.\/]+)\.[^.\/]+)\//i.test(url);
  28. var provider_url = RegExp.$1, provider_name = RegExp.$2, data_convert = "", extra_script = "", button_hsb = [];
  29. if(api.includes("yahooapis"))
  30. data_convert += "data = data.query.results.json;";
  31. switch(provider_name) {
  32. case "youtube" :
  33. button_hsb.push( 0, 100, 100);
  34. data_convert += "delete data.thumbnail_url;";
  35. extra_script += "var img = new Image();"+
  36. "img.onload = function() {"+
  37. "if(this.naturalWidth > 480)"+
  38. "document.body.style.backgroundImage = 'url('+this.src+')';"+
  39. "else if(this.src.includes('maxres'))"+
  40. "this.src = this.src.replace('maxres', 'sd');"+
  41. "};"+
  42. "img.src = '"+background_img.match(/\/(.+)\//)[0]+"maxresdefault.jpg';";
  43. break;
  44. case "dailymotion" :
  45. button_hsb.push( 60, 30, 300);
  46. data_convert += "var img = new Image();"+
  47. "img.onload = function() { document.body.style.backgroundImage = 'url('+this.src+')'; };"+
  48. "img.src = data.thumbnail_url.replace(/\\/x240[^.]+/i, '');"+
  49. "delete data.thumbnail_url;";
  50. break;
  51. case "vimeo" :
  52. button_hsb.push(220, 50, 220);
  53. data_convert += "data.thumbnail_url = data.thumbnail_url.replace(/_\\d+/i, '');";
  54. break;
  55. case "rutube" :
  56. button_hsb.push( 0, 0, 250);
  57. data_convert += "data.thumbnail_url = data.thumbnail_url.replace(/\\?.+/i, '');";
  58. break;
  59. case "twitch" :
  60. button_hsb.push(270, 50, 100);
  61. if(background_img) { // channel live
  62. data_convert += "data.title = data.status || 'Untitled Broadcast';"+
  63. "data.author_url = '"+provider_url+"/'+data.name+'/profile';"+
  64. "data.author_name = data.display_name;"+
  65. "data.duration = data.game && 'playing <a target=_blank href=\""+provider_url+"/directory/game/'+data.game+'\">'+data.game+'</b>';"+
  66. "offline_image = data.video_banner;";
  67. extra_script += "function jsonpCallback2(data) {"+
  68. "if(data.streams.length != 0) return;"+
  69. "document.body.style.backgroundImage = 'url('+offline_image+')';"+
  70. "document.getElementById('duration').textContent = 'offline';"+
  71. "}"+
  72. "</script>"+
  73. "<script defer src='https://api.twitch.tv/kraken/streams?channel="+url.match(/[^\/]+$/)[0]+"&callback=jsonpCallback2'>";
  74. } else { // video recorded
  75. data_convert += "data.thumbnail_url = data.preview.replace(/\\d+x\\d+/i, '0x0');"+
  76. "data.author_url = '"+provider_url+"/'+data.channel.name+'/profile';"+
  77. "data.author_name = data.channel.display_name;"+
  78. "data.duration = data.length;";
  79. }
  80. break;
  81. case "ustream" :
  82. button_hsb.push( 40, 50, 230);
  83. if(background_img) // channel live
  84. data_convert += "delete data.thumbnail_url;";
  85. break;
  86. }
  87. if(!html) html = [
  88. "<!doctype html>"+
  89. "<html>"+
  90. "<head>"+
  91. "<title>Lazy Embedded Video</title>"+
  92. "<script defer src='", api, "&callback=jsonpCallback'></script>"+
  93. "<script>"+
  94. "function jsonpCallback(data) {",
  95. data_convert,
  96. "if(data.thumbnail_url) document.body.style.backgroundImage = 'url('+data.thumbnail_url+')';"+
  97. "if(data.url) document.getElementById('title').href = data.url;"+
  98. "if(data.title) document.getElementById('title').textContent = "+
  99. "document.getElementById('title').title = data.title;"+
  100. "if(data.author_url) document.getElementById('author').href = data.author_url;"+
  101. "if(data.author_name) document.getElementById('author').textContent = data.author_name;"+
  102. "if(data.duration) {"+
  103. "if(Number(data.duration))"+
  104. "document.getElementById('duration').textContent = new Date(data.duration*1000).toISOString().substr(11,8);"+
  105. "else document.getElementById('duration').innerHTML = data.duration;"+
  106. "}"+
  107. "}",
  108. extra_script,
  109. "</script>"+
  110. "<style>"+
  111. "html { height: 100%; }"+
  112. "body {"+
  113. "margin: 0;"+
  114. "height: 100%;"+
  115. "background: black ", background_img, " center/100% no-repeat;"+
  116. "color: white;"+
  117. "font: 14px sans-serif;"+
  118. "}"+
  119. "a {"+
  120. "color: inherit;"+
  121. "font-weight: bold;"+
  122. "text-decoration: none;"+
  123. "}"+
  124. "a:hover { text-decoration: underline; }"+
  125. "ul {"+
  126. "margin: 0;"+
  127. "padding: 0;"+
  128. "list-style: none;"+
  129. "}"+
  130. "#infobar {"+
  131. "position: absolute;"+
  132. "top: 0px;"+
  133. "width: 100%;"+
  134. "padding: 8px 16px;"+
  135. "box-sizing: border-box;"+
  136. "background: rgba(0,0,0,0.5);"+
  137. "word-wrap: break-word;"+
  138. "}"+
  139. "#infobar_right {"+
  140. "float: right;"+
  141. "margin-left: 16px;"+
  142. "text-align: right;"+
  143. "text-transform: capitalize;"+
  144. "}"+
  145. "#button {"+
  146. "height: 100%;"+
  147. "cursor: pointer;"+
  148. "background-position: 0px 50%;"+
  149. "}"+
  150. "#button:hover {"+
  151. "background-position: -70px 50%;"+
  152. "filter: hue-rotate(", button_hsb[0], "deg) saturate(", button_hsb[1], "%) brightness(", button_hsb[2], "%);"+
  153. "-webkit-filter: hue-rotate(", button_hsb[0], "deg) saturate(", button_hsb[1], "%) brightness(", button_hsb[2], "%);"+
  154. "}"+
  155. "#button > div {"+
  156. "width: 70px;"+
  157. "height: 100%;"+
  158. "margin: auto;"+
  159. "background: url("+GM_getResourceURL("playIcon")+") no-repeat;"+
  160. "background-position: inherit;"+
  161. "}"+
  162. "#titleBlock {"+
  163. "overflow: hidden;"+
  164. "max-height: 34px;"+
  165. "}"+
  166. "</style>"+
  167. "</head>"+
  168. "<body>"+
  169. "<div id=button onclick='location.replace(\"", iframe, "\");'><div></div></div>"+
  170. "<div id=infobar>"+
  171. "<ul id=infobar_right>"+
  172. "<li><a id=author target=_blank></a></li>"+
  173. "<li><a id=provider target=_blank href='", provider_url, "'>", provider_name, "</a></li>"+
  174. "</ul>"+
  175. "<ul>"+
  176. "<li id=titleBlock><a id=title target=_blank href='", url, "'>", url, "</a></li>"+
  177. "<li id=duration></li>"+
  178. "</ul>"+
  179. "</div>"+
  180. "</body>"+
  181. "</html>"
  182. ];
  183. html[ 1] = api;
  184. html[ 3] = data_convert;
  185. html[ 5] = extra_script;
  186. html[ 7] = background_img;
  187. html[ 9] = button_hsb[0];
  188. html[11] = button_hsb[1];
  189. html[13] = button_hsb[2];
  190. html[15] = button_hsb[0];
  191. html[17] = button_hsb[1];
  192. html[19] = button_hsb[2];
  193. html[21] = iframe;
  194. html[23] = provider_url;
  195. html[25] = provider_name;
  196. html[27] = url;
  197. html[29] = url;
  198. };
  199. var createOembed = function(api, url) { return api+encodeURIComponent(url); };
  200. var createNOembed = function(api, url) { return createOembed("//noembed.com/embed?url=", location.protocol+url); };
  201. var createYOembed = function(api, url) { return createOembed("//query.yahooapis.com/v1/public/yql?format=json&q=",
  202. 'SELECT * FROM json WHERE url="'+createOembed(location.protocol+api,url)+'"'); };
  203. var createLazyVideo = function(elem) {
  204. if(elem.tagName == "IFRAME" && elem.srcdoc) return true;
  205. var id, args, url = elem.src || elem.data || elem.dataset.src;
  206. if(!url) return true;
  207. if(!a) a = document.createElement("A");
  208. a.href = url;
  209. /([^.]+)\.[^.]+$/i.test(a.hostname);
  210. switch(RegExp.$1) {
  211. case "youtube" :
  212. if(/\/(?:v|embed)\/([^&]*)/i.test(a.pathname))
  213. id = RegExp.$1 || (/[?&]v=([^&]+)/i.test(a.search) && RegExp.$1);
  214. if(!id || !testCSP()) return !id;
  215. args = "?autoplay=1";
  216. if(/[?&](list=[^&]+)/i.test(a.search)) args += "&"+RegExp.$1;
  217. if(/[?&](start=[^&]+)/i.test(a.search)) args += "&"+RegExp.$1;
  218. createHtml(url =
  219. "//www.youtube.com/watch"+args+"&v="+id,
  220. "//www.youtube.com/embed/"+id+args,
  221. createNOembed("//www.youtube.com/oembed?format=json&url=", url),
  222. "url(//i.ytimg.com/vi/"+id+"/hqdefault.jpg)"
  223. );
  224. break;
  225. case "dailymotion" :
  226. if(/\/(?:swf|embed)\/video\/([^&_]+)/i.test(a.pathname)) id = RegExp.$1;
  227. if(!id || !testCSP()) return !id;
  228. args = "?autoplay=1";
  229. if(/[?&](mute=[^&]+)/i.test(a.search)) args += "&"+RegExp.$1;
  230. if(/[?&](start=[^&]+)/i.test(a.search)) args += "&"+RegExp.$1;
  231. createHtml(url =
  232. "//www.dailymotion.com/video/"+id+args,
  233. "//www.dailymotion.com/embed/video/"+id+args,
  234. createOembed("//www.dailymotion.com/services/oembed?format=json&url=", location.protocol+url),
  235. "url(//www.dailymotion.com/thumbnail/video/"+id+")"
  236. );
  237. break;
  238. case "vimeo" :
  239. if(/\/(?:moogaloop\.swf|video\/)([^&]*)/i.test(a.pathname))
  240. id = RegExp.$1 || (/[?&]clip_id=([^&]+)/i.test(a.search) && RegExp.$1);
  241. if(!id || !testCSP()) return !id;
  242. args = "?autoplay=1";
  243. if(/[?&](loop=[^&]+)/i.test(a.search)) args += "&"+RegExp.$1;
  244. if(/(\#t=[\dhms]+)/i.test(a.hash)) args += RegExp.$1;
  245. createHtml(url =
  246. "//vimeo.com/"+id+args,
  247. "//player.vimeo.com/video/"+id+args,
  248. createOembed("//vimeo.com/api/oembed.json?url=", url)
  249. );
  250. break;
  251. case "rutube" :
  252. if(/\/play\/embed\/([^&.\/]+)/i.test(a.pathname)) id = RegExp.$1;
  253. if(!id || !testCSP()) return !id;
  254. args = "?autoStart=1";
  255. if(/[?&](bmstart=[^&]+)/i.test(a.search)) args += "&"+RegExp.$1;
  256. createHtml(url =
  257. "//rutube.ru/"+(isNaN(id) ? "video/"+id+"/" : "tracks/"+id+".html/")+args,
  258. "//rutube.ru/play/embed/"+id+args,
  259. createOembed("//rutube.ru/api/oembed/?format=jsonp&url=", url)
  260. );
  261. break;
  262. case "twitch" :
  263. if(/[?&](channel|video)=([^&]+)/i.test(a.search)) {args = RegExp.$1; id = RegExp.$2;}
  264. else if(/\/(.+)\/embed/i.test(a.pathname)) {args = "channel"; id = RegExp.$1;}
  265. if(!id || !testCSP()) return !id;
  266. createHtml(url =
  267. "//www.twitch.tv/"+(args=="video" ? id.replace("v","c/v/") : id),
  268. "//player.twitch.tv/?autoplay=true&"+args+"="+id,
  269. "https://api.twitch.tv/kraken/"+args+"s/"+id+"?",
  270. args=="channel" ? "url(//static-cdn.jtvnw.net/previews-ttv/live_user_"+id+"-0x0.jpg)" : null
  271. );
  272. break;
  273. case "ustream" :
  274. if(/(?:\/embed)?\/(channel\/|recorded\/)?([^&]+)/i.test(a.pathname)) {args = RegExp.$1 || "channel/"; id = RegExp.$2;}
  275. if(!id || !testCSP()) return !id;
  276. createHtml(url =
  277. "//www.ustream.tv/"+args+id,
  278. "//www.ustream.tv/embed/"+(args=="channel/" ? "" : args)+id+"?html5ui=1&autoplay=1",
  279. createYOembed("//www.ustream.tv/oembed?format=json&url=", url),
  280. args=="channel/" && !isNaN(id) ? "url(//static-cdn1.ustream.tv/i/channel/live/1_"+id+",640x360,b:0.jpg)" : null
  281. );
  282. break;
  283. default :
  284. return true;
  285. }
  286. if(elem.tagName != "IFRAME") {
  287. var iframe = document.createElement("IFRAME");
  288. iframe.id = elem.id;
  289. iframe.className = elem.className;
  290. iframe.style.cssText = elem.style.cssText;
  291. if(!iframe.style.width && elem.width ) iframe.style.width = elem.width+"px";
  292. if(!iframe.style.height && elem.height) iframe.style.height = elem.height+"px";
  293. if(elem.parentNode.tagName == "OBJECT") {
  294. elem = elem.parentNode;
  295. iframe.style.cssText = elem.style.cssText + iframe.style.cssText;
  296. if(!iframe.style.width && elem.width ) iframe.style.width = elem.width+"px";
  297. if(!iframe.style.height && elem.height) iframe.style.height = elem.height+"px";
  298. }
  299. if(!iframe.style.borderWidth) iframe.style.borderWidth = (elem.border||0)+"px";
  300. switch(elem.align) {
  301. case "left" : case "right" :
  302. if(!iframe.style.float) iframe.style.float = elem.align; break;
  303. case "top" : case "middle" : case "bottom" :
  304. if(!iframe.style.verticalAlign) iframe.style.verticalAlign = elem.align; break;
  305. }
  306. elem.parentNode.replaceChild(iframe, elem);
  307. elem = iframe;
  308. }
  309. elem.allowFullscreen = true;
  310. elem.srcdoc = html.join("");
  311. return true;
  312. };
  313. // convert NodeList to Array because for some reason sometimes I wasn't able to read src when iterating directly through NodeList
  314. var nodes = ["IFRAME", "EMBED", "OBJECT"].reduce(function(sum, value) {
  315. return sum.concat([].slice.call(document.getElementsByTagName(value)));
  316. }, frameElement ? [frameElement] : []);
  317. for(var i = 0; i < nodes.length && createLazyVideo(nodes[i]); i++) {}
  318. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址