Inject2Download

Simple media download script

当前为 2018-02-12 提交的版本,查看 最新版本

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