Lazy Embedded Video

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

当前为 2016-05-11 提交的版本,查看 最新版本

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

QingJ © 2025

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