Inject2Download

Simple media download script

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

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