Inject2Download

Simple media download script

目前為 2016-08-02 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Inject2Download
  3. // @namespace http://lkubuntu.wordpress.com/
  4. // @version 0.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. // Helper functions
  16. function i2d_show_url(namespace, url, description) {
  17. function get_absolute_url(url) {
  18. var a = document.createElement('a');
  19. a.href = url;
  20. return a.href;
  21. }
  22.  
  23. if (!description)
  24. description = "";
  25.  
  26. if (typeof url !== "string" || url.replace("\s", "").length === 0)
  27. return;
  28.  
  29. if (url.match(/^mediasource:/))
  30. return;
  31.  
  32. url = get_absolute_url(url);
  33.  
  34. if (!("i2d_url_list" in window))
  35. window.i2d_url_list = [];
  36.  
  37. for (var i = 0; i < i2d_url_list.length; i++) {
  38. if (i2d_url_list[i][0] === namespace &&
  39. i2d_url_list[i][1] === url &&
  40. i2d_url_list[i][2] === description)
  41. return;
  42. }
  43.  
  44.  
  45. i2d_url_list.push([namespace, url, description]);
  46.  
  47. var newurl = decodeURIComponent(url);
  48.  
  49. var text = "[" + namespace + "] " + description + ": ";
  50.  
  51. console.log("[i2d] " + text + newurl);
  52.  
  53. var el = document.getElementById("i2d-popup");
  54. if (!el) {
  55. el = document.createElement("div");
  56. //el.style.position = "absolute";
  57. el.style.width = "max(60%, 100em)";
  58. el.style.height = "max(60%, 100em)";
  59. el.style.maxWidth = "100%";
  60. el.style.maxHeight = "100%";
  61. el.style.background = "white";
  62. el.style.top = "0px";
  63. el.style.left = "0px";
  64. el.style.zIndex = 999999;
  65. el.style.color = "black";
  66. el.style.overflow = "scroll";
  67. el.ondblclick = function() {
  68. el.parentElement.removeChild(el);
  69. };
  70. el.innerHTML = "Double click to close<br />";
  71. el.id = "i2d-popup";
  72. document.body.appendChild(el);
  73. }
  74. el.innerHTML += text + "<a href='" + newurl + "' style='color:blue'>" + newurl + "</a><br />";
  75.  
  76. // XXX: why is this needed? test: http://playbb.me/embed.php?w=718&h=438&vid=at/nw/flying_witch_-_01.mp4, animeplus.tv
  77. document.body.removeChild(el);
  78. el.style.position = "absolute";
  79. document.body.appendChild(el);
  80. }
  81.  
  82.  
  83. // Injecting functions
  84. var get_script_str = function(f) {
  85. return f.toString().replace(/^function.*{|}$/g, '');
  86. };
  87.  
  88. function add_script(s, el) {
  89. var script_body = "(function() {\n" + s + "\n})();";
  90. var myscript = document.createElement('script');
  91. myscript.className = "i2d";
  92. myscript.innerHTML = script_body;
  93. if (el) {
  94. el.appendChild(myscript);
  95. } else {
  96. document.head.appendChild(myscript);
  97. }
  98. }
  99.  
  100. function inject(variable, newvalue, aliases) {
  101. console.log("[i2d] injecting " + variable);
  102. if (!aliases)
  103. aliases = [];
  104.  
  105. add_script(i2d_show_url.toString() + "\n" +
  106. "if (!(window." + variable + ".INJECTED)) {\n" +
  107. "var oldvariable = window." + variable + ";\n" +
  108. "var oldvariable_keys = Object.keys(oldvariable);\n" +
  109. "window." + variable + " = " + newvalue.toString() + ";\n" +
  110. "for (var i = 0; i < oldvariable_keys.length; i++) {\n" +
  111. " window." + variable + "[oldvariable_keys[i]] = oldvariable[oldvariable_keys[i]];\n" +
  112. "}\n" +
  113. "window." + variable + ".INJECTED = true;\n" +
  114. "var aliases = " + JSON.stringify(aliases) + ";\n" +
  115. "for (var i = 0; i < aliases.length; i++) {\n" +
  116. " if (aliases[i] in window && window[aliases[i]] == oldvariable)" +
  117. " window[aliases[i]] = window." + variable + "\n" +
  118. "}\n" +
  119. "}");
  120. }
  121.  
  122. function inject_jquery_plugin(name, value) {
  123. if (!("jQuery" in unsafeWindow) ||
  124. !("fn" in unsafeWindow.jQuery) ||
  125. !(name in unsafeWindow.jQuery.fn) ||
  126. unsafeWindow.jQuery.fn[name].INJECTED)
  127. return;
  128.  
  129. inject("jQuery.fn." + name, value);
  130. }
  131.  
  132.  
  133. // Main code
  134. function i2d_main(e) {
  135. if (e) {
  136. if (!e.tagName || e.tagName.toLowerCase() !== "script")
  137. return;
  138. if ((e.className === "i2d")) {
  139. return;
  140. }
  141. }
  142.  
  143. if ("soundManager" in unsafeWindow && !unsafeWindow.soundManager.INJECTED) {
  144. inject("soundManager.createSound", function(arg1, arg2) {
  145. if (typeof arg1 === "string")
  146. i2d_show_url("soundManager", arg2);
  147. else
  148. i2d_show_url("soundManager", arg1.url);
  149.  
  150. return oldvariable.apply(this, arguments);
  151. });
  152. }
  153.  
  154. if ("jwplayer" in unsafeWindow && !unsafeWindow.jwplayer.INJECTED) {
  155. inject("jwplayer", function() {
  156. var result = oldvariable.apply(this, arguments);
  157.  
  158. var check_sources = function(x) {
  159. if (typeof x === "object") {
  160. if (x instanceof Array) {
  161. for (var i = 0; i < x.length; i++) {
  162. check_sources(x[i]);
  163. }
  164. return;
  165. }
  166.  
  167. var label = "";
  168.  
  169. if ("title" in x)
  170. label += "[" + x.title + "]";
  171.  
  172. if ("label" in x)
  173. label += "[" + x.label + "]";
  174.  
  175. if ("streamer" in x) {
  176. i2d_show_url("jwplayer", x.streamer, "[stream]" + label);
  177. }
  178.  
  179. if ("file" in x) {
  180. i2d_show_url("jwplayer", x.file, label);
  181. }
  182.  
  183. if ("sources" in x) {
  184. check_sources(x.sources);
  185. }
  186.  
  187. if ("playlist" in x) {
  188. check_sources(x.playlist);
  189. }
  190. } else if (typeof x === "string") {
  191. i2d_show_url("jwplayer", x);
  192. }
  193. };
  194.  
  195. if ("setup" in result) {
  196. var old_jwplayer_setup = result.setup;
  197. result.setup = function() {
  198. if (typeof arguments[0] === "object") {
  199. var x = arguments[0];
  200.  
  201. if ("modes" in x) {
  202. for (var i = 0; i < x.modes.length; i++) {
  203. // TODO: support more?
  204. if ("type" in x.modes[i] && x.modes[i].type === "html5") {
  205. if ("config" in x.modes[i] && "file" in x.modes[i].config) {
  206. check_sources(x.modes[i].config);
  207. }
  208. }
  209. }
  210. }
  211.  
  212. check_sources(x);
  213. }
  214.  
  215. return old_jwplayer_setup.apply(this, arguments);
  216. };
  217. }
  218.  
  219. if ("load" in result) {
  220. var old_jwplayer_load = result.load;
  221. result.load = function() {
  222. check_sources(arguments[0]);
  223. return old_jwplayer_load.apply(this, arguments);
  224. };
  225. }
  226.  
  227. if ("on" in result) {
  228. result.on('playlistItem', function(item) {
  229. check_sources(item.item);
  230. });
  231.  
  232. var old_jwplayer_on = result.on;
  233. result.on = function() {
  234. if (arguments[0] === "adBlock")
  235. return;
  236.  
  237. return old_jwplayer_on.apply(this, arguments);
  238. }
  239. }
  240.  
  241. return result;
  242. });
  243. }
  244.  
  245. if ("flowplayer" in unsafeWindow && !unsafeWindow.flowplayer.INJECTED) {
  246. inject("flowplayer", function() {
  247. var obj_baseurl = null;
  248. var els = [];
  249.  
  250. function get_url(x) {
  251. x = decodeURIComponent(x);
  252.  
  253. if (obj_baseurl) {
  254. if (x.match(/^[a-z]*:\/\//)) {
  255. return x;
  256. } else {
  257. return obj_baseurl + "/" + x;
  258. }
  259. } else {
  260. return x;
  261. }
  262. };
  263.  
  264. function check_sources(x, els, label) {
  265. if (typeof x === "string") {
  266. if (!x.match(/\.xml$/))
  267. i2d_show_url("flowplayer", get_url(x), label);
  268.  
  269. return;
  270. }
  271.  
  272. if (x instanceof Array) {
  273. for (var i = 0; i < x.length; i++) {
  274. check_sources(x[i], els, label);
  275. }
  276. return;
  277. }
  278.  
  279. if (typeof x !== "object")
  280. return;
  281.  
  282. label = "";
  283.  
  284. if ("title" in x)
  285. label += "[" + x.title + "]";
  286.  
  287. if ("clip" in x) {
  288. if ("baseUrl" in x.clip) {
  289. obj_baseurl = x.clip.baseUrl;
  290.  
  291. for (var i = 0; i < els.length; i++) {
  292. els[i].i2d_baseurl = obj_baseurl;
  293. }
  294. }
  295.  
  296. check_sources(x.clip, els, label);
  297. }
  298.  
  299. if ("playlist" in x) {
  300. check_sources(x.playlist, els, label);
  301. }
  302.  
  303. if ("url" in x) {
  304. check_sources(x.url, els, label);
  305. }
  306.  
  307. if ("bitrates" in x) {
  308. for (var j = 0; j < x.bitrates.length; j++) {
  309. if ("url" in x.bitrates[j]) {
  310. var description = "";
  311. if (x.bitrates[j].isDefault)
  312. description += "default:";
  313. if (x.bitrates[j].sd)
  314. description += "sd:";
  315. if (x.bitrates[j].hd)
  316. description += "hd:";
  317. if (x.bitrates[j].bitrate)
  318. description += x.bitrates[j].bitrate;
  319.  
  320. i2d_show_url("flowplayer", get_url(x.bitrates[j].url), description);
  321. }
  322. }
  323. }
  324. }
  325.  
  326. if (arguments.length >= 1) {
  327. els = [null];
  328.  
  329. if (typeof arguments[0] === "string") {
  330. try {
  331. els[0] = document.getElementById(arguments[0]);
  332. } catch(e) {
  333. }
  334.  
  335. try {
  336. if (!els[0])
  337. els = document.querySelectorAll(arguments[0]);
  338. } catch(e) {
  339. els = [];
  340. }
  341. } else if (arguments[0] instanceof HTMLElement) {
  342. els = [arguments[0]];
  343. }
  344. }
  345.  
  346. for (var i = 0; i < els.length; i++) {
  347. if (!els[i] || !(els[i] instanceof HTMLElement))
  348. continue;
  349.  
  350. if ("i2d_baseurl" in els[i])
  351. obj_baseurl = els[i].i2d_baseurl;
  352. }
  353.  
  354. if (arguments.length >= 3 && typeof arguments[2] === "object") {
  355. check_sources(arguments[2], els);
  356. } else if (arguments.length >= 3 && typeof arguments[2] === "string") {
  357. i2d_show_url("flowplayer", get_url(arguments[2]));
  358. }
  359.  
  360. for (var i = 0; i < els.length; i++) {
  361. if (!els[i] || !(els[i] instanceof HTMLElement))
  362. continue;
  363.  
  364. var href = els[i].getAttribute("href");
  365. if (href) {
  366. i2d_show_url("flowplayer", get_url(href), "href");
  367. }
  368. }
  369.  
  370. var result = oldvariable.apply(this, arguments);
  371.  
  372. if (!result)
  373. return result;
  374.  
  375. if ("addClip" in result) {
  376. var old_fplayer_addclip = result.addClip;
  377. result.addClip = function() {
  378. if (arguments.length > 0)
  379. check_sources(arguments[0], els);
  380.  
  381. return old_fplayer_addclip.apply(this, arguments);
  382. };
  383. }
  384.  
  385. if ("setPlaylist" in result) {
  386. var old_fplayer_setplaylist = result.setPlaylist;
  387. result.setPlaylist = function() {
  388. if (arguments.length > 0)
  389. check_sources(arguments[0], els);
  390.  
  391. return old_fplayer_setplaylist.apply(this, arguments);
  392. };
  393. }
  394.  
  395. return result;
  396. });
  397. inject("$f", function() {return flowplayer.apply(this, arguments)});
  398. }
  399.  
  400. if ("videojs" in unsafeWindow && !unsafeWindow.videojs.INJECTED) {
  401. inject("videojs", function() {
  402. if (arguments.length > 0 && typeof arguments[0] === "string") {
  403. var my_el = document.getElementById(arguments[0]);
  404. if (!my_el)
  405. my_el = document.querySelector(arguments[0]);
  406.  
  407. if (my_el) {
  408. if (my_el.src) {
  409. i2d_show_url("videojs", my_el.src);
  410. }
  411.  
  412. for (var i = 0; i < my_el.children.length; i++) {
  413. if (my_el.children[i].tagName.toLowerCase() === "source") {
  414. if (my_el.children[i].src) {
  415. i2d_show_url("videojs", my_el.children[i].src, my_el.children[i].getAttribute("label"));
  416. }
  417. }
  418. }
  419. }
  420. }
  421.  
  422. var result = oldvariable.apply(this, arguments);
  423.  
  424. var old_videojs_src = result.src;
  425. result.src = function() {
  426. if (arguments.length > 0 && typeof arguments[0] === "object") {
  427. if ("src" in arguments[0]) {
  428. i2d_show_url("videojs", arguments[0].src);
  429. }
  430. }
  431.  
  432. return old_videojs_src.apply(this, arguments);
  433. };
  434.  
  435. return result;
  436. });
  437.  
  438. add_script(i2d_show_url.toString() +
  439. get_script_str(function() {
  440. document.addEventListener("DOMContentLoaded", function() {
  441. var els = document.getElementsByClassName("video-js");
  442. for (var i = 0; i < els.length; i++) {
  443. var my_el = els[i];
  444. if (my_el.tagName.toLowerCase() === "video") {
  445. if (!my_el.getAttribute('data-setup')) {
  446. continue;
  447. }
  448. if (my_el.src) {
  449. i2d_show_url("videojs", my_el.src);
  450. }
  451.  
  452. for (var i = 0; i < my_el.children.length; i++) {
  453. if (my_el.children[i].tagName.toLowerCase() === "source") {
  454. if (my_el.children[i].src) {
  455. i2d_show_url("videojs", my_el.children[i].src, my_el.children[i].getAttribute("label"));
  456. }
  457. }
  458. }
  459. }
  460. }
  461. });
  462. }));
  463. }
  464.  
  465. if ("amp" in unsafeWindow && !unsafeWindow.amp.INJECTED) {
  466. inject("amp", function() {
  467. function show_amp_source(sourceobj) {
  468. if ("protectionInfo" in sourceobj) {
  469. console.log("[amp] Cannot decode protection info");
  470. }
  471. if ("src" in sourceobj)
  472. i2d_show_url("amp", sourceobj.src);
  473. }
  474.  
  475. if (arguments.length >= 2 && typeof arguments[1] === "object") {
  476. if ("sourceList" in arguments[1]) {
  477. for (var i = 0; i < arguments[1].sourceList.length; i++) {
  478. show_amp_source(arguments[1].sourceList[i]);
  479. }
  480. }
  481. }
  482.  
  483. var result = oldvariable.apply(this, arguments);
  484.  
  485. if (!result)
  486. return result;
  487.  
  488. var old_amp_src = result.src;
  489. result.src = function() {
  490. for (var i = 0; i < arguments[0].length; i++) {
  491. show_amp_source(arguments[0][i]);
  492. }
  493.  
  494. return old_amp_src.apply(this, arguments);
  495. };
  496.  
  497. return result;
  498. });
  499. }
  500.  
  501. if (window.location.host.search("forvo") >= 0 && "createAudioObject" in unsafeWindow && !unsafeWindow.createAudioObject.INJECTED) {
  502. inject("createAudioObject", function(id, mp3, ogg) {
  503. i2d_show_url("forvo", mp3, "mp3");
  504. i2d_show_url("forvo", ogg, "ogg");
  505.  
  506. return oldvariable.apply(this, arguments);
  507. });
  508. }
  509.  
  510. if ("jQuery" in unsafeWindow) {
  511. inject_jquery_plugin("jPlayer", function() {
  512. if (arguments.length > 0 && arguments[0] === "setMedia") {
  513. if (arguments.length > 1) {
  514. if (typeof arguments[1] === "object") {
  515. for (var i in arguments[1]) {
  516. if (i === "title" ||
  517. i === "duration" ||
  518. i === "track" /* for now */ ||
  519. i === "artist" ||
  520. i === "free")
  521. continue;
  522.  
  523. i2d_show_url("jPlayer", arguments[1][i], i);
  524. }
  525. } else if (typeof arguments[1] === "string") {
  526. i2d_show_url("jPlayer", arguments[1]);
  527. }
  528. }
  529. }
  530.  
  531. return oldvariable.apply(this, arguments);
  532. });
  533. }
  534. }
  535.  
  536. i2d_main();
  537.  
  538. window.addEventListener("afterscriptexecute", function(e) {
  539. i2d_main(e.target);
  540. });
  541.  
  542. document.addEventListener("DOMContentLoaded", function() {
  543. var get_raws = function() {
  544. var audios = [].slice.call(document.getElementsByTagName("audio"));
  545. var videos = [].slice.call(document.getElementsByTagName("video"));
  546. var els = Array.concat(audios, videos);
  547.  
  548. for (var i = 0; i < els.length; i++) {
  549. var basename = "raw ";
  550. var el = els[i];
  551.  
  552. if (el.tagName.toLowerCase() === "video") {
  553. basename += "video";
  554. } else {
  555. basename += "audio";
  556. }
  557.  
  558. if (el.id)
  559. basename += ": #" + el.id;
  560.  
  561. var show_updates = function() {
  562. if (el.src)
  563. i2d_show_url(basename, el.src);
  564.  
  565. for (var x = 0; x < el.children.length; x++) {
  566. if (els[i].children[x].tagName.toLowerCase() !== "source") {
  567. continue;
  568. }
  569.  
  570. var type = null;
  571. if (el.children[x].type)
  572. type = el.children[x].type;
  573.  
  574. if (el.children[x].src)
  575. i2d_show_url(basename, el.children[x].src, type);
  576. }
  577. };
  578.  
  579. var observer = new MutationObserver(show_updates);
  580. observer.observe(el, { attributes: true, childList: true });
  581.  
  582. show_updates();
  583. }
  584. };
  585. add_script(i2d_show_url.toString() + "\n" +
  586. "(" + get_raws.toString() + ")();");
  587. });
  588. })();