Inject2Download

Simple media download script

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

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