Inject2Download

Simple media download script

当前为 2018-01-31 提交的版本,查看 最新版本

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