Inject2Download

Simple media download script

目前为 2018-02-19 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Inject2Download
  3. // @namespace http://lkubuntu.wordpress.com/
  4. // @version 0.2.8.3
  5. // @description Simple media download script
  6. // @author Anonymous Meerkat
  7. // @include *
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. "use strict";
  14.  
  15. var injected_set = {};
  16.  
  17. // Helper functions
  18. function i2d_show_url(namespace, url, description) {
  19. function get_absolute_url(url) {
  20. var a = document.createElement('a');
  21. a.href = url;
  22. return a.href;
  23. }
  24.  
  25. function run_on_load(f) {
  26. if (document.readyState === "complete" ||
  27. document.readyState === "interactive") {
  28. f();
  29. } else {
  30. var listener = function() {
  31. if (document.readyState === "complete" ||
  32. document.readyState === "interactive") {
  33. f();
  34.  
  35. document.removeEventListener("readystatechange", listener);
  36. }
  37. };
  38.  
  39. document.addEventListener("readystatechange", listener);
  40. //document.addEventListener('DOMContentLoaded', f, false);
  41. //window.addEventListener('load', f, false);
  42. }
  43. }
  44.  
  45. if (!description)
  46. description = "";
  47.  
  48. if (typeof url !== "string" || url.replace("\s", "").length === 0)
  49. return;
  50.  
  51. if (url.match(/^mediasource:/) || url.match(/^blob:/))
  52. return;
  53.  
  54. url = get_absolute_url(url);
  55.  
  56. if (!("i2d_url_list" in window))
  57. window.i2d_url_list = [];
  58.  
  59. for (var i = 0; i < i2d_url_list.length; i++) {
  60. if (i2d_url_list[i][0] === namespace &&
  61. i2d_url_list[i][1] === url &&
  62. i2d_url_list[i][2] === description)
  63. return;
  64. }
  65.  
  66.  
  67. i2d_url_list.push([namespace, url, description]);
  68.  
  69. var newurl = decodeURIComponent(url);
  70.  
  71. var text = "[" + namespace + "] " + description + ": ";
  72.  
  73. console.log("[i2d] " + text + newurl);
  74.  
  75. run_on_load(function() {
  76. var el = document.getElementById("i2d-popup");
  77. var elspan = document.getElementById("i2d-popup-x");
  78. var elspan1 = document.getElementById("i2d-popup-close");
  79. var eldiv = document.getElementById("i2d-popup-div");
  80. var eldivhold = document.getElementById("i2d-popup-div-holder");
  81. if (!el) {
  82. el = document.createElement("div");
  83. //el.style.position = "absolute";
  84. el.style.width = "max(60%, 100em)";
  85. el.style.height = "max(60%, 100em)";
  86. el.style.maxWidth = "100%";
  87. el.style.maxHeight = "100%";
  88. el.style.height = "auto";
  89. el.style.width = "auto";
  90. el.style.background = "white";
  91. el.style.top = "0px";
  92. el.style.left = "0px";
  93. el.style.zIndex = Number.MAX_SAFE_INTEGER - 1;
  94. el.style.color = "black";
  95. el.style.fontFamily = "sans-serif";
  96. el.style.fontSize = "16px";
  97. el.style.lineHeight = "normal";
  98. el.style.textAlign = "left";
  99. el.style.overflow = "scroll";
  100. /*el.ondblclick = function() {
  101. el.parentElement.removeChild(el);
  102. };*/
  103. eldivhold = document.createElement("div");
  104. eldivhold.id = "i2d-popup-span-holder";
  105. eldivhold.style.width = "100%";
  106. eldivhold.style.display = "block";
  107. eldivhold.style.overflow = "auto";
  108. eldivhold.style.paddingBottom = ".5em";
  109.  
  110. elspan = document.createElement("span");
  111. elspan.style.fontSize = "130%";
  112. elspan.style.cursor = "pointer";
  113. elspan.style.color = "#900";
  114. elspan.style.padding = ".1em";
  115. elspan.style.float = "left";
  116. elspan.style.display = "inline";
  117. elspan.id = "i2d-popup-x";
  118. elspan.innerHTML = '[hide]';
  119. elspan.style.textDecoration = "underline";
  120. eldivhold.appendChild(elspan);
  121.  
  122. elspan1 = document.createElement("span");
  123. elspan1.style.fontSize = "130%";
  124. elspan1.style.cursor = "pointer";
  125. elspan1.style.color = "#900";
  126. elspan1.style.padding = ".1em";
  127. elspan1.style.float = "right";
  128. //elspan1.style.display = "none";
  129. elspan1.style.display = "inline";
  130. elspan1.id = "i2d-popup-close";
  131. elspan1.innerHTML = '[close]';
  132. elspan1.style.textDecoration = "underline";
  133. eldivhold.appendChild(elspan1);
  134.  
  135. //el.innerHTML = "<br style='line-height:150%' />";
  136. el.id = "i2d-popup";
  137. eldiv = document.createElement("div");
  138. eldiv.id = "i2d-popup-div";
  139. //eldiv.style.display = "none";
  140. eldiv.style.display = "block";
  141. el.appendChild(eldiv);
  142. el.insertBefore(eldivhold, el.firstChild);
  143. document.body.appendChild(el);
  144.  
  145. elspan.onclick = function() {
  146. /*var el = document.getElementById("i2d-popup");
  147. el.parentElement.removeChild(el);*/
  148. var eldiv = document.getElementById("i2d-popup-div");
  149. var elspan = document.getElementById("i2d-popup-x");
  150. if (eldiv.style.display === "none") {
  151. elspan.innerHTML = '[hide]';
  152. eldiv.style.display = "block";
  153. elspan1.style.display = "inline";
  154. } else {
  155. elspan.innerHTML = '[show]';
  156. eldiv.style.display = "none";
  157. elspan1.style.display = "none";
  158. }
  159. };
  160.  
  161. elspan1.onclick = function() {
  162. var el = document.getElementById("i2d-popup");
  163. el.parentElement.removeChild(el);
  164. };
  165. }
  166. var shorturl = newurl;
  167. if (shorturl.length > 100) {
  168. shorturl = shorturl.substring(0, 99) + "&hellip;";
  169. }
  170. eldiv.innerHTML += text + "<a href='" + newurl + "' style='color:blue' title='" + newurl + "'>" + shorturl + "</a><br />";
  171.  
  172. // XXX: why is this needed? test: http://playbb.me/embed.php?w=718&h=438&vid=at/nw/flying_witch_-_01.mp4, animeplus.tv
  173. document.body.removeChild(el);
  174. el.style.position = "absolute";
  175. document.body.appendChild(el);
  176.  
  177. /*if (document.getElementById("i2d-popup-x"))
  178. document.getElementById("i2d-popup-x").parentElement.removeChild(document.getElementById("i2d-popup-x"));*/
  179.  
  180. /*el.insertBefore(elspan, el.firstChild);
  181. el.insertBefore(elspan1, el.firstChild);*/
  182. //el.insertBefore(eldivhold, el.firstChild);
  183. });
  184. }
  185.  
  186.  
  187. // Injecting functions
  188. var get_script_str = function(f) {
  189. return f.toString().replace(/^function.*{|}$/g, '');
  190. };
  191.  
  192. function add_script(s, el) {
  193. var script_body = "(function() {\n" + s + "\n})();";
  194. var myscript = document.createElement('script');
  195. myscript.className = "i2d";
  196. myscript.innerHTML = script_body;
  197. if (el) {
  198. el.appendChild(myscript);
  199. } else {
  200. document.head.appendChild(myscript);
  201. }
  202. }
  203.  
  204. function inject(variable, newvalue, aliases) {
  205. if (variable instanceof Array) {
  206. for (var i = 0; i < variable.length; i++) {
  207. inject(variable[i], newvalue, aliases);
  208. }
  209. return;
  210. }
  211.  
  212. console.log("[i2d] injecting " + variable);
  213. if (!aliases)
  214. aliases = [];
  215.  
  216. var initobjects = "";
  217. var subvariable = variable;
  218. var subindex = 0;
  219. while (true) {
  220. var index = subvariable.indexOf(".");
  221. var breakme = false;
  222. if (index < 0) {
  223. index = subvariable.length;
  224. breakme = true;
  225. }
  226. subvariable = subvariable.substr(index + 1);
  227. subindex += index + 1;
  228. var subname = variable.substr(0, subindex - 1);
  229. initobjects += "if (!" + subname + ") {" + subname + " = {};}\n";
  230. if (breakme)
  231. break;
  232. }
  233.  
  234. add_script(i2d_show_url.toString() + "\n" +
  235. initobjects + "\n" +
  236. "if ((window." + variable + " !== undefined) && !(window." + variable + ".INJECTED)) {\n" +
  237. "var oldvariable = window." + variable + ";\n" +
  238. "var oldvariable_keys = Object.keys(oldvariable);\n" +
  239. "window." + variable + " = " + newvalue.toString() + ";\n" +
  240. "for (var i = 0; i < oldvariable_keys.length; i++) {\n" +
  241. " window." + variable + "[oldvariable_keys[i]] = oldvariable[oldvariable_keys[i]];\n" +
  242. "}\n" +
  243. "window." + variable + ".INJECTED = true;\n" +
  244. "var aliases = " + JSON.stringify(aliases) + ";\n" +
  245. "for (var i = 0; i < aliases.length; i++) {\n" +
  246. " if (aliases[i] in window && window[aliases[i]] == oldvariable)" +
  247. " window[aliases[i]] = window." + variable + "\n" +
  248. "}\n" +
  249. "}");
  250. }
  251.  
  252. function jquery_plugin_exists(name) {
  253. if (!("jQuery" in window) ||
  254. typeof window.jQuery !== "function" ||
  255. !("fn" in window.jQuery) ||
  256. !(name in window.jQuery.fn))
  257. return false;
  258.  
  259.  
  260. return true;
  261. }
  262.  
  263. function inject_jquery_plugin(name, value) {
  264. if (!jquery_plugin_exists(name) ||
  265. window.jQuery.fn[name].INJECTED)
  266. return;
  267.  
  268. inject("jQuery.fn." + name, value);
  269. }
  270.  
  271. function inject_url(pattern, func) {
  272. // https://stackoverflow.com/questions/629671/how-can-i-intercept-xmlhttprequests-from-a-greasemonkey-script
  273. console.log("[i2d] injecting URL: " + pattern);
  274. (function(open) {
  275. window.XMLHttpRequest.prototype.open = function() {
  276. if (arguments[1] &&
  277. arguments[1].match(pattern)) {
  278. var url = arguments[1];
  279. this.addEventListener("readystatechange", function() {
  280. if (this.readyState === 4) {
  281. func.bind(this)(url);
  282. }
  283. });
  284. }
  285. open.apply(this, arguments);
  286. };
  287. })(window.XMLHttpRequest.prototype.open);
  288. }
  289.  
  290. function i2d_onload(f) {
  291. if (document.readyState === "loading") {
  292. document.addEventListener("DOMContentLoaded", f);
  293. } else {
  294. f();
  295. }
  296. }
  297.  
  298.  
  299. // Main code
  300. function i2d_main(e) {
  301. if (e) {
  302. if (!e.tagName || e.tagName.toLowerCase() !== "script")
  303. return;
  304. if ((e.className === "i2d")) {
  305. return;
  306. }
  307. }
  308.  
  309. if ("soundManager" in window && !window.soundManager.INJECTED) {
  310. inject("soundManager.createSound", function(arg1, arg2) {
  311. if (typeof arg1 === "string")
  312. i2d_show_url("soundManager", arg2);
  313. else
  314. i2d_show_url("soundManager", arg1.url);
  315.  
  316. return oldvariable.apply(this, arguments);
  317. });
  318. }
  319.  
  320. if ("jwplayer" in window && !window.jwplayer.INJECTED) {
  321. inject("jwplayer", function() {
  322. var result = oldvariable.apply(this, arguments);
  323.  
  324. var check_sources = function(x) {
  325. if (typeof x === "object") {
  326. if (x instanceof Array) {
  327. for (var i = 0; i < x.length; i++) {
  328. check_sources(x[i]);
  329. }
  330. return;
  331. }
  332.  
  333. var label = "";
  334.  
  335. if ("title" in x)
  336. label += "[" + x.title + "]";
  337.  
  338. if ("label" in x)
  339. label += "[" + x.label + "]";
  340.  
  341. if ("kind" in x)
  342. label += "(" + x.kind + ")";
  343.  
  344. if ("streamer" in x) {
  345. i2d_show_url("jwplayer", x.streamer, "[stream]" + label);
  346. }
  347.  
  348. if ("file" in x) {
  349. i2d_show_url("jwplayer", x.file, label);
  350. }
  351.  
  352. if ("sources" in x) {
  353. check_sources(x.sources);
  354. }
  355.  
  356. if ("playlist" in x) {
  357. check_sources(x.playlist);
  358. }
  359.  
  360. if ("tracks" in x) {
  361. check_sources(x.tracks);
  362. }
  363. } else if (typeof x === "string") {
  364. i2d_show_url("jwplayer", x);
  365. }
  366. };
  367.  
  368. if ("setup" in result) {
  369. var old_jwplayer_setup = result.setup;
  370. result.setup = function() {
  371. if (typeof arguments[0] === "object") {
  372. var x = arguments[0];
  373.  
  374. if ("modes" in x) {
  375. for (var i = 0; i < x.modes.length; i++) {
  376. // TODO: support more?
  377. if ("type" in x.modes[i] && x.modes[i].type === "html5") {
  378. if ("config" in x.modes[i] && "file" in x.modes[i].config) {
  379. check_sources(x.modes[i].config);
  380. }
  381. }
  382. }
  383. }
  384.  
  385. check_sources(x);
  386. }
  387.  
  388. return old_jwplayer_setup.apply(this, arguments);
  389. };
  390. }
  391.  
  392. if ("load" in result) {
  393. var old_jwplayer_load = result.load;
  394. result.load = function() {
  395. check_sources(arguments[0]);
  396. return old_jwplayer_load.apply(this, arguments);
  397. };
  398. }
  399.  
  400. if ("on" in result) {
  401. result.on('playlistItem', function(item) {
  402. check_sources(item.item);
  403. });
  404.  
  405. var old_jwplayer_on = result.on;
  406. result.on = function() {
  407. if (arguments[0] === "adBlock")
  408. return;
  409.  
  410. return old_jwplayer_on.apply(this, arguments);
  411. };
  412. }
  413.  
  414. return result;
  415. });
  416. }
  417.  
  418. if ("flowplayer" in window && !window.flowplayer.INJECTED) {
  419. inject("flowplayer", function() {
  420. var obj_baseurl = null;
  421. var els = [];
  422.  
  423. function get_url(x) {
  424. x = decodeURIComponent(x);
  425.  
  426. if (obj_baseurl) {
  427. if (x.match(/^[a-z]*:\/\//)) {
  428. return x;
  429. } else {
  430. return obj_baseurl + "/" + x;
  431. }
  432. } else {
  433. return x;
  434. }
  435. }
  436.  
  437. function check_sources(x, els, label) {
  438. if (typeof x === "string") {
  439. if (!x.match(/\.xml$/))
  440. i2d_show_url("flowplayer", get_url(x), label);
  441.  
  442. return;
  443. }
  444.  
  445. if (x instanceof Array) {
  446. for (var i = 0; i < x.length; i++) {
  447. check_sources(x[i], els, label);
  448. }
  449. return;
  450. }
  451.  
  452. if (typeof x !== "object")
  453. return;
  454.  
  455. label = "";
  456.  
  457. if ("title" in x)
  458. label += "[" + x.title + "]";
  459.  
  460. if ("clip" in x) {
  461. if ("baseUrl" in x.clip) {
  462. obj_baseurl = x.clip.baseUrl;
  463.  
  464. for (var i = 0; i < els.length; i++) {
  465. els[i].i2d_baseurl = obj_baseurl;
  466. }
  467. }
  468.  
  469. check_sources(x.clip, els, label);
  470. }
  471.  
  472. if ("sources" in x) {
  473. check_sources(x.sources, els, label);
  474. }
  475.  
  476. if ("playlist" in x) {
  477. check_sources(x.playlist, els, label);
  478. }
  479.  
  480. if ("url" in x) {
  481. check_sources(x.url, els, label);
  482. }
  483.  
  484. if ("src" in x) {
  485. check_sources(x.src, els. label);
  486. }
  487.  
  488. if ("bitrates" in x) {
  489. for (var j = 0; j < x.bitrates.length; j++) {
  490. if ("url" in x.bitrates[j]) {
  491. var description = "";
  492. if (x.bitrates[j].isDefault)
  493. description += "default:";
  494. if (x.bitrates[j].sd)
  495. description += "sd:";
  496. if (x.bitrates[j].hd)
  497. description += "hd:";
  498. if (x.bitrates[j].bitrate)
  499. description += x.bitrates[j].bitrate;
  500.  
  501. i2d_show_url("flowplayer", get_url(x.bitrates[j].url), description);
  502. }
  503. }
  504. }
  505. }
  506.  
  507. if (arguments.length >= 1) {
  508. els = [null];
  509.  
  510. if (typeof arguments[0] === "string") {
  511. try {
  512. els[0] = document.getElementById(arguments[0]);
  513. } catch(e) {
  514. }
  515.  
  516. try {
  517. if (!els[0])
  518. els = document.querySelectorAll(arguments[0]);
  519. } catch(e) {
  520. els = [];
  521. }
  522. } else if (arguments[0] instanceof HTMLElement) {
  523. els = [arguments[0]];
  524. }
  525. }
  526.  
  527. for (var i = 0; i < els.length; i++) {
  528. if (!els[i] || !(els[i] instanceof HTMLElement))
  529. continue;
  530.  
  531. if ("i2d_baseurl" in els[i])
  532. obj_baseurl = els[i].i2d_baseurl;
  533. }
  534.  
  535. if (arguments.length >= 3 && typeof arguments[2] === "object") {
  536. check_sources(arguments[2], els);
  537. } else if (arguments.length >= 3 && typeof arguments[2] === "string") {
  538. i2d_show_url("flowplayer", get_url(arguments[2]));
  539. } else if (arguments.length === 2 && typeof arguments[1] === "object") {
  540. check_sources(arguments[1], els);
  541. } else if (arguments.length === 2 && typeof arguments[1] === "string") {
  542. i2d_show_url("flowplayer", get_url(arguments[1]));
  543. }
  544.  
  545. for (var i = 0; i < els.length; i++) {
  546. if (!els[i] || !(els[i] instanceof HTMLElement))
  547. continue;
  548.  
  549. var href = els[i].getAttribute("href");
  550. if (href) {
  551. i2d_show_url("flowplayer", get_url(href), "href");
  552. }
  553. }
  554.  
  555. var result = oldvariable.apply(this, arguments);
  556.  
  557. if (!result || typeof result !== "object")
  558. return result;
  559.  
  560. if ("addClip" in result) {
  561. var old_fplayer_addclip = result.addClip;
  562. result.addClip = function() {
  563. if (arguments.length > 0)
  564. check_sources(arguments[0], els);
  565.  
  566. return old_fplayer_addclip.apply(this, arguments);
  567. };
  568. }
  569.  
  570. if ("setPlaylist" in result) {
  571. var old_fplayer_setplaylist = result.setPlaylist;
  572. result.setPlaylist = function() {
  573. if (arguments.length > 0)
  574. check_sources(arguments[0], els);
  575.  
  576. return old_fplayer_setplaylist.apply(this, arguments);
  577. };
  578. }
  579.  
  580. if ("load" in result) {
  581. var old_fplayer_load = result.load;
  582. result.load = function() {
  583. if (arguments.length > 0)
  584. check_sources(arguments[0], els);
  585.  
  586. return old_fplayer_load.apply(this, arguments);
  587. };
  588. }
  589.  
  590. /*if ("on" in result) {
  591. result.on("load", function(e, api, video) {
  592. console.log(e);
  593. check_sources(video || api.video, els);
  594. });
  595. }*/
  596.  
  597. return result;
  598. }, ["$f"]);
  599.  
  600. add_script(get_script_str(function() {
  601. flowplayer(function(api, root) {
  602. api.on("load", function(e, api, video) {
  603. flowplayer().load(video || api.video);
  604. });
  605. });
  606. }));
  607. }
  608.  
  609. if ("videojs" in window && !window.videojs.INJECTED) {
  610. inject("videojs", function() {
  611. if (arguments.length > 0 && typeof arguments[0] === "string") {
  612. var my_el = document.getElementById(arguments[0]);
  613. if (!my_el)
  614. my_el = document.querySelector(arguments[0]);
  615.  
  616. if (my_el) {
  617. if (my_el.src) {
  618. i2d_show_url("videojs", my_el.src);
  619. }
  620.  
  621. for (var i = 0; i < my_el.children.length; i++) {
  622. if (my_el.children[i].tagName.toLowerCase() === "source") {
  623. if (my_el.children[i].src) {
  624. i2d_show_url("videojs", my_el.children[i].src, my_el.children[i].getAttribute("label"));
  625. }
  626. }
  627. }
  628. }
  629. }
  630.  
  631. var result = oldvariable.apply(this, arguments);
  632.  
  633. var old_videojs_src = result.src;
  634. result.src = function() {
  635. if (arguments.length > 0 && typeof arguments[0] === "object") {
  636. if ("src" in arguments[0]) {
  637. i2d_show_url("videojs", arguments[0].src);
  638. }
  639. }
  640.  
  641. return old_videojs_src.apply(this, arguments);
  642. };
  643.  
  644. return result;
  645. });
  646.  
  647. add_script(i2d_show_url.toString() +
  648. i2d_onload.toString() +
  649. get_script_str(function() {
  650. i2d_onload(function() {
  651. var els = document.getElementsByClassName("video-js");
  652. for (var i = 0; i < els.length; i++) {
  653. var my_el = els[i];
  654. if (my_el.tagName.toLowerCase() === "video") {
  655. if (!my_el.getAttribute('data-setup')) {
  656. continue;
  657. }
  658. if (my_el.src) {
  659. i2d_show_url("videojs", my_el.src);
  660. }
  661.  
  662. for (var j = 0; j < my_el.children.length; j++) {
  663. if (my_el.children[j].tagName.toLowerCase() === "source") {
  664. if (my_el.children[j].src) {
  665. i2d_show_url("videojs", my_el.children[j].src, my_el.children[j].getAttribute("label"));
  666. }
  667. }
  668. }
  669. }
  670. }
  671. });
  672. }));
  673. }
  674.  
  675. if ("amp" in window && !window.amp.INJECTED) {
  676. inject("amp", function() {
  677. function show_amp_source(sourceobj) {
  678. if ("protectionInfo" in sourceobj) {
  679. console.log("[amp] Cannot decode protection info");
  680. }
  681. if ("src" in sourceobj)
  682. i2d_show_url("amp", sourceobj.src);
  683. }
  684.  
  685. if (arguments.length >= 2 && typeof arguments[1] === "object") {
  686. if ("sourceList" in arguments[1]) {
  687. for (var i = 0; i < arguments[1].sourceList.length; i++) {
  688. show_amp_source(arguments[1].sourceList[i]);
  689. }
  690. }
  691. }
  692.  
  693. var result = oldvariable.apply(this, arguments);
  694.  
  695. if (!result)
  696. return result;
  697.  
  698. var old_amp_src = result.src;
  699. result.src = function() {
  700. for (var i = 0; i < arguments[0].length; i++) {
  701. show_amp_source(arguments[0][i]);
  702. }
  703.  
  704. return old_amp_src.apply(this, arguments);
  705. };
  706.  
  707. return result;
  708. });
  709. }
  710.  
  711. if ("DJPlayer" in window && !window.DJPlayer.INJECTED) {
  712. add_script(i2d_show_url.toString() + "\n" +
  713. "var oldsetmedia = window.DJPlayer.prototype.setMedia;\n" +
  714. "window.DJPlayer.prototype.setMedia = function() {\n" +
  715. " if (arguments.length > 0 && typeof arguments[0] === 'string') {\n" +
  716. " i2d_show_url('DJPlayer', arguments[0]);\n" +
  717. " }\n" +
  718. " return oldsetmedia.apply(this, arguments);\n" +
  719. "}");
  720. }
  721.  
  722. if ("bitmovin" in window && !window.bitmovin.INJECTED) {
  723. inject(["bitmovin.player", "bitdash", "bitmovinPlayer"], function() {
  724. var result = oldvariable.apply(this, arguments);
  725.  
  726. var check_progressive = function(progressive) {
  727. if (typeof progressive === "string") {
  728. i2d_show_url("bitmovin", progressive, "progressive");
  729. } else if (progressive instanceof Array) {
  730. for (var i = 0; i < progressive.length; i++) {
  731. check_progressive(progressive[i]);
  732. }
  733. } else if (typeof progressive === "object") {
  734. var str = "";
  735. if (progressive.label)
  736. str += "[" + progressive.label + "] ";
  737. if (progressive.bitrate)
  738. str += progressive.bitrate;
  739.  
  740. i2d_show_url("bitmovin", progressive.url, str);
  741. }
  742. };
  743.  
  744. var check_sources = function(x) {
  745. if (typeof x === "object") {
  746. if ("source" in x) {
  747. var sourceobj = x.source;
  748.  
  749. if (sourceobj.progressive) {
  750. check_progressive(sourceobj.progressive);
  751. }
  752.  
  753. if (sourceobj.dash) {
  754. i2d_show_url("bitmovin", sourceobj.dash, "dash");
  755. }
  756.  
  757. if (sourceobj.hls) {
  758. i2d_show_url("bitmovin", sourceobj.hls, "hls");
  759. }
  760. }
  761. }
  762. };
  763.  
  764. if ("setup" in result) {
  765. var old_bitmovin_setup = result.setup;
  766. result.setup = function() {
  767. check_sources(arguments[0]);
  768.  
  769. return old_bitmovin_setup.apply(this, arguments);
  770. };
  771. }
  772.  
  773. if ("load" in result) {
  774. var old_bitmovin_load = result.load;
  775. result.load = function() {
  776. check_sources({source: arguments[0]});
  777.  
  778. return old_bitmovin_load.apply(this, arguments);
  779. };
  780. }
  781.  
  782. return result;
  783. });
  784. }
  785.  
  786. if ("dashjs" in window && window.dashjs.MediaPlayer && !window.dashjs.MediaPlayer.INJECTED) {
  787. inject("dashjs.MediaPlayer", function() {
  788. var outer_result = oldvariable.apply(this, arguments);
  789.  
  790. var oldcreate = outer_result.create;
  791. outer_result.create = function() {
  792. var result = oldcreate.apply(this, arguments);
  793.  
  794. var old_attachsource = result.attachSource;
  795. result.attachSource = function(url) {
  796. i2d_show_url("dash.js", url);
  797. return old_attachsource.apply(this, arguments);
  798. };
  799.  
  800. return result;
  801. };
  802.  
  803. return outer_result;
  804. });
  805. }
  806.  
  807. if ("Hls" in window && window.Hls && !window.Hls.INJECTED) {
  808. console.log("[i2d] injecting Hls");
  809. var old_loadsource = window.Hls.prototype.loadSource;
  810. window.Hls.prototype.loadSource = function(url) {
  811. i2d_show_url("hls.js", url);
  812. return old_loadsource.apply(this, arguments);
  813. };
  814. window.Hls.INJECTED = true;
  815. }
  816.  
  817. if ("flvjs" in window && window.flvjs.createPlayer && !window.flvjs.createPlayer.INJECTED) {
  818. inject("flvjs.createPlayer", function(options) {
  819. if (options) {
  820. if ("url" in options) {
  821. i2d_show_url("flv.js", options.url);
  822. }
  823. }
  824. return oldvariable.apply(this, arguments);
  825. });
  826. }
  827.  
  828. if (("location" in window) && window.location && window.location.host.search("soundcloud.com") >= 0 && !("soundcloud" in injected_set)) {
  829. injected_set["soundcloud"] = true;
  830.  
  831. inject_url(/api\.soundcloud\.com\/.*?\/tracks\/[0-9]*\/streams/, function(url) {
  832. var track = url.match(/\/tracks\/([0-9]*)\//);
  833. var parsed = JSON.parse(this.responseText);
  834. for (var item in parsed) {
  835. i2d_show_url("soundcloud", parsed[item], "[" + item + "] " + track[1]);
  836. }
  837. });
  838. }
  839.  
  840. if (("location" in window) && window.location && window.location.host.search("forvo") >= 0 && "createAudioObject" in window && !window.createAudioObject.INJECTED) {
  841. inject("createAudioObject", function(id, mp3, ogg) {
  842. i2d_show_url("forvo", mp3, "mp3");
  843. i2d_show_url("forvo", ogg, "ogg");
  844.  
  845. return oldvariable.apply(this, arguments);
  846. });
  847. }
  848.  
  849. if (("location" in window) && window.location && window.location.href.search("twitter.com/i/videos") >= 0 && !("twitter" in injected_set)) {
  850. injected_set["twitter"] = true;
  851.  
  852. i2d_onload(function() {
  853. var pc = document.getElementById('playerContainer');
  854. if (!pc) {
  855. return;
  856. }
  857.  
  858. var config = pc.getAttribute('data-config');
  859. if (!config) {
  860. return;
  861. }
  862.  
  863. var config_parsed = JSON.parse(config);
  864.  
  865. if ("video_url" in config_parsed) {
  866. i2d_show_url('twitter', config_parsed.video_url);
  867. }
  868. });
  869. }
  870.  
  871. if (("location" in window) && window.location && window.location.href.search("vine.co/v/") >= 0) {
  872. var show_video_urls = function(videourls) {
  873. for (var i = 0; i < videourls.length; i++) {
  874. var videourl = videourls[i];
  875.  
  876. var formatstr = "[";
  877. formatstr += videourl.format + ":";
  878. formatstr += videourl.idStr;
  879.  
  880. if (videourl.rate > 0)
  881. formatstr += ", " + videourl.rate;
  882.  
  883. formatstr += "]";
  884.  
  885. i2d_show_url('vine', videourl.videoUrl, formatstr);
  886. }
  887. };
  888.  
  889. if (window.location.href.search("/card" ) >= 0 && !("vine_card" in injected_set)) {
  890. injected_set["vine_card"] = true;
  891.  
  892. i2d_onload(function() {
  893. var config_el = document.getElementById("configuration");
  894. if (!config_el) {
  895. return;
  896. }
  897.  
  898. var config = JSON.parse(config_el.innerHTML);
  899. if (!config || typeof config !== "object") {
  900. return;
  901. }
  902.  
  903. if (!("post" in config) || !("videoUrls" in config.post)) {
  904. return;
  905. }
  906.  
  907. show_video_urls(config.post.videoUrls);
  908. });
  909. } else if (!("vine_video" in injected_set)) {
  910. injected_set["vine_video"] = true;
  911.  
  912. i2d_onload(function() {
  913. var scripts = document.getElementsByTagName("script");
  914. if (scripts.length === 0)
  915. return;
  916.  
  917. var thescript = null;
  918. for (var i = 0; i < scripts.length; i++) {
  919. if (scripts[i].innerHTML.search("window.POST_DATA") < 0) {
  920. continue;
  921. }
  922.  
  923. thescript = scripts[i];
  924. break;
  925. }
  926.  
  927. var config;
  928. eval(thescript.innerHTML.replace("window.POST_DATA", "config"));
  929.  
  930. var videourls = config[Object.keys(config)[0]].videoUrls;
  931. show_video_urls(videourls);
  932. });
  933. }
  934. }
  935.  
  936. if ("jQuery" in window) {
  937. inject_jquery_plugin("jPlayer", function() {
  938. if (arguments.length > 0 && arguments[0] === "setMedia") {
  939. if (arguments.length > 1) {
  940. if (typeof arguments[1] === "object") {
  941. for (var i in arguments[1]) {
  942. if (i === "title" ||
  943. i === "duration" ||
  944. i === "track" /* for now */ ||
  945. i === "artist" ||
  946. i === "free")
  947. continue;
  948.  
  949. i2d_show_url("jPlayer", arguments[1][i], i);
  950. }
  951. } else if (typeof arguments[1] === "string") {
  952. i2d_show_url("jPlayer", arguments[1]);
  953. }
  954. }
  955. }
  956.  
  957. return oldvariable.apply(this, arguments);
  958. });
  959.  
  960. inject_jquery_plugin("amazingaudioplayer", function() {
  961. var result = oldvariable.apply(this, arguments);
  962.  
  963. function add_source_obj(x) {
  964. type = "";
  965. if ("type" in x) {
  966. type = x.type;
  967. }
  968.  
  969. i2d_show_url("amazingaudioplayer", x.src, type);
  970. }
  971.  
  972. function add_source(x) {
  973. if (x instanceof Array) {
  974. for (var i = 0; i < x.length; i++) {
  975. add_source_obj(x[i]);
  976. }
  977. } else {
  978. add_source_obj(x);
  979. }
  980. }
  981.  
  982. var audioplayer = jQuery(this).data("object").audioPlayer;
  983. if (audioplayer.audioItem) {
  984. add_source(audioplayer.audioItem.source);
  985. }
  986.  
  987. var oldload = audioplayer.load;
  988. audioplayer.load = function(item) {
  989. if ("source" in item) {
  990. add_source(item.source);
  991. }
  992.  
  993. return oldload.apply(this, arguments);
  994. };
  995.  
  996. return result;
  997. });
  998.  
  999. if (jquery_plugin_exists("jPlayerAudio") && !window.jQuery.fn.jPlayerAudio.INJECTED) {
  1000. inject_jquery_plugin("jPlayerAudio", function() {
  1001. return oldvariable.apply(this, arguments);
  1002. });
  1003.  
  1004. add_script(i2d_show_url.toString() + "\n" +
  1005. "var oldmedia=jQuery.jPlayerAudio.prototype.setMedia;\n" +
  1006. "jQuery.jPlayerAudio.prototype.setMedia = function(e) {\n" +
  1007. " var absolute = this._absoluteMediaUrls(e);\n" +
  1008. " jQuery.each(this.formats, function(a, o) {\n" +
  1009. " i2d_show_url('cleanaudioplayer', absolute[o]);\n" +
  1010. " });\n" +
  1011. " return oldmedia.apply(this, arguments);\n" +
  1012. "}");
  1013. }
  1014.  
  1015. if (jquery_plugin_exists("jPlayerVideo") && !window.jQuery.fn.jPlayerVideo.INJECTED) {
  1016. inject_jquery_plugin("jPlayerVideo", function() {
  1017. return oldvariable.apply(this, arguments);
  1018. });
  1019.  
  1020. add_script(i2d_show_url.toString() + "\n" +
  1021. "var oldmedia=jQuery.jPlayerVideo.prototype.setMedia;\n" +
  1022. "jQuery.jPlayerVideo.prototype.setMedia = function(e) {\n" +
  1023. " var absolute = this._absoluteMediaUrls(e);\n" +
  1024. " jQuery.each(this.formats, function(a, o) {\n" +
  1025. " i2d_show_url('cleanvideoplayer', absolute[o]);\n" +
  1026. " });\n" +
  1027. " return oldmedia.apply(this, arguments);\n" +
  1028. "}");
  1029. }
  1030. }
  1031. }
  1032.  
  1033. i2d_main();
  1034.  
  1035. window.addEventListener("afterscriptexecute", function(e) {
  1036. i2d_main(e.target);
  1037. });
  1038.  
  1039. var process_raw_tag = function(el) {
  1040. var basename = "raw ";
  1041.  
  1042. if (el.tagName.toLowerCase() === "video") {
  1043. basename += "video";
  1044. } else {
  1045. basename += "audio";
  1046. }
  1047.  
  1048. if (el.id)
  1049. basename += ": #" + el.id;
  1050.  
  1051. var show_updates = function() {
  1052. if (el.src)
  1053. i2d_show_url(basename, el.src);
  1054.  
  1055. for (var x = 0; x < el.children.length; x++) {
  1056. if (el.children[x].tagName.toLowerCase() !== "source" &&
  1057. el.children[x].tagName.toLowerCase() !== "track") {
  1058. continue;
  1059. }
  1060.  
  1061. var type = "";
  1062. if (el.children[x].type)
  1063. type += "[" + el.children[x].type + "]";
  1064.  
  1065. if (el.children[x].label)
  1066. type += "[" + el.children[x].label + "]";
  1067.  
  1068. if (el.children[x].srclang)
  1069. type += "[" + el.children[x].srclang + "]";
  1070.  
  1071. if (el.children[x].kind)
  1072. type += "(" + el.children[x].kind + ")";
  1073.  
  1074. if (el.children[x].src)
  1075. i2d_show_url(basename, el.children[x].src, type);
  1076. }
  1077. };
  1078.  
  1079. var observer = new MutationObserver(show_updates);
  1080. observer.observe(el, { attributes: true, childList: true });
  1081.  
  1082. show_updates();
  1083. };
  1084.  
  1085. var process_el = function(el) {
  1086. if (el.nodeName === "SCRIPT") {
  1087. i2d_main(el);
  1088. } else if (el.nodeName === "VIDEO" ||
  1089. el.nodeName === "AUDIO") {
  1090. process_raw_tag(el);
  1091. }
  1092.  
  1093. if (el.children) {
  1094. for (var i = 0; i < el.children.length; i++) {
  1095. process_el(el.children[i]);
  1096. }
  1097. }
  1098. };
  1099.  
  1100. var script_observer_cb = function(mutations, observer) {
  1101. for (var i = 0; i < mutations.length; i++) {
  1102. if (mutations[i].addedNodes) {
  1103. for (var x = 0; x < mutations[i].addedNodes.length; x++) {
  1104. var addednode = mutations[i].addedNodes[x];
  1105. process_el(addednode);
  1106. }
  1107. }
  1108. if (mutations[i].removedNodes) {
  1109. for (var x = 0; x < mutations[i].removedNodes.length; x++) {
  1110. if (mutations[i].removedNodes[x].nodeName === "SCRIPT")
  1111. i2d_main(mutations[i].removedNodes[x]);
  1112. }
  1113. }
  1114. }
  1115. };
  1116.  
  1117. var script_observer = new MutationObserver(script_observer_cb);
  1118.  
  1119. script_observer.observe(document, {childList: true, subtree: true});
  1120.  
  1121. i2d_onload(function() {
  1122. var get_raws = function() {
  1123. var audios = [].slice.call(document.getElementsByTagName("audio"));
  1124. var videos = [].slice.call(document.getElementsByTagName("video"));
  1125. var els = Array.prototype.concat(audios, videos);
  1126.  
  1127. for (var i = 0; i < els.length; i++) {
  1128. var basename = "raw ";
  1129. var el = els[i];
  1130.  
  1131. if (el.tagName.toLowerCase() === "video") {
  1132. basename += "video";
  1133. } else {
  1134. basename += "audio";
  1135. }
  1136.  
  1137. if (el.id)
  1138. basename += ": #" + el.id;
  1139.  
  1140. var show_updates = function() {
  1141. if (el.src)
  1142. i2d_show_url(basename, el.src);
  1143.  
  1144. for (var x = 0; x < el.children.length; x++) {
  1145. if (els[i].children[x].tagName.toLowerCase() !== "source" &&
  1146. els[i].children[x].tagName.toLowerCase() !== "track") {
  1147. continue;
  1148. }
  1149.  
  1150. var type = "";
  1151. if (el.children[x].type)
  1152. type += "[" + el.children[x].type + "]";
  1153.  
  1154. if (el.children[x].label)
  1155. type += "[" + el.children[x].label + "]";
  1156.  
  1157. if (el.children[x].srclang)
  1158. type += "[" + el.children[x].srclang + "]";
  1159.  
  1160. if (el.children[x].kind)
  1161. type += "(" + el.children[x].kind + ")";
  1162.  
  1163. if (el.children[x].src)
  1164. i2d_show_url(basename, el.children[x].src, type);
  1165. }
  1166. };
  1167.  
  1168. var observer = new MutationObserver(show_updates);
  1169. observer.observe(el, { attributes: true, childList: true });
  1170.  
  1171. show_updates();
  1172. }
  1173. };
  1174.  
  1175. //get_raws();
  1176.  
  1177. i2d_main();
  1178. });
  1179. })();