Inject2Download

Simple media download script

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

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