Inject2Download

Simple media download script

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

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