localav

Lightweight avfun video dumper

当前为 2014-09-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name localav
  3. // @namespace https://github.com/doomred
  4. // @description Lightweight avfun video dumper
  5. // @version 0.1
  6. // @encoding utf-8
  7. // @license GPLv3
  8. // @copyleft dye `Eric' jarhoo
  9. // @author dye `Eric' jarhoo
  10. // @homepageURL htpp://saltyremix.com
  11. // @icon https://raw.github.com/doomred/localav/master/localav_icon.png
  12. // @include http://*.acfun.*/v/ac*
  13. // @run-at document-end
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_xmlhttpRequest
  17. // @grant GM_openInTab
  18. // @grant GM_registerMenuCommand
  19. // ==/UserScript==
  20.  
  21. var lvDebug, lvLeft, lvBt, lvMain;
  22. lvDebug = 0; // debug usage
  23. lvLeft = GM_getValue('gm_lv_left', '1em');
  24. lvBt = GM_getValue('gm_lv_bottom', '1em');
  25.  
  26. function forceupdate() {
  27. 'use strict';
  28. GM_openInTab('https://raw.github.com/doomred/localav/master/localav.user.js');
  29. return 0;
  30. }
  31. GM_registerMenuCommand('localav| Force Update!', forceupdate, 'a');
  32.  
  33.  
  34. function lvMain() {
  35. 'use strict';
  36. function dynamicparts(obj) {
  37. var key, contentbox, contentSize, temp, i, contentURL, lvDiv;
  38. lvDiv = window.top.document.getElementById('lv-box');
  39. for(key in obj) {
  40. if(obj.hasOwnProperty(key)) {
  41. contentbox = document.createElement('div');
  42. contentbox.classList.add('lv-content');
  43. contentbox.style.margin = '1em 1.5em 0 1.5em';
  44. contentbox.style.cssFloat = 'left';
  45. contentbox.style.cursor = 'pointer';
  46. contentURL = obj[key].files[0].url;
  47. contentbox.setAttribute('data-lv', contentURL);
  48. temp = (obj[key].files[0].bytes / 1024 / 1024 * 100); // leave two float point
  49. contentSize = parseInt(temp, 10) / 100;
  50.  
  51. if(contentURL.search('.hlv?') === -1) { // sina hlv fix
  52. contentbox.appendChild(document.createTextNode(obj[key].quality + '[' + obj[key].files[0].type + ']'));
  53. } else {
  54. contentbox.appendChild(document.createTextNode(obj[key].quality + '[hlv]'));
  55. }
  56. contentbox.innerHTML += "<br />";
  57. if(!contentSize) {contentSize = '未知';}
  58. contentbox.appendChild(document.createTextNode(contentSize + "MB"));
  59. contentbox.addEventListener('click', function (e) { // to download stuff within pure js, midified via stackoverflow
  60. var iframe, eventSender, cSrc;
  61. if (!e && window.event) {e = window.event;}
  62. eventSender = (window.event) ? e.srcElement : e.target;
  63. cSrc = eventSender.getAttribute('data-lv');
  64. if(cSrc.search('.hlv?') !== -1) { // sina hlv fix
  65. GM_openInTab(cSrc);
  66. } else {
  67. iframe = document.createElement('iframe');
  68. iframe.style.display = 'none';
  69. iframe.style.position = 'absoulte';
  70. iframe.style.top = '0px';
  71. iframe.style.left = '0px';
  72. iframe.src = cSrc;
  73. document.body.appendChild(iframe);
  74. }
  75. }, false);
  76. if(contentURL.search('&id=tudou') !== -1 || 0) { // add broken parsed urls here, blame API
  77. contentbox.style.backgroundColor = 'grey';
  78. contentbox.innerHTML += "<br />可能失效";
  79. }
  80.  
  81. /* remove same links */
  82. temp = document.getElementsByClassName('lv-content');
  83. if(!temp.length) {lvDiv.appendChild(contentbox);}
  84. for(i = 0; i < temp.length; i++) {
  85. if(temp[i].getAttribute('data-lv') !== contentbox.getAttribute('data-lv')) {
  86. lvDiv.appendChild(contentbox);
  87. }
  88. }
  89. }
  90. }
  91. }
  92.  
  93. var idPlayer, temp, tempNum, playerVID, playerDLURL, playerData, lvDiv ;
  94. // idPlayer = document.getElementById("ACFlashPlayer-re"); This not work, cuz page has two same id name
  95. idPlayer = document.getElementsByTagName('iframe')[0];
  96. temp = idPlayer.getAttribute('src');
  97. temp = temp.substr(temp.search('vid')); // strip & temp store
  98. tempNum = temp.search(';');
  99. playerVID = temp.substring(4, tempNum);
  100. playerDLURL = "https://ssl.acfun.tv//aliyun/index.php?&type=mobileclient&vid=" + playerVID; // key core of localav
  101. if(lvDebug) {window.alert(playerDLURL);}
  102.  
  103. /* init lv-box */
  104. lvDiv = document.createElement('div');
  105. lvDiv.style.border = '2px solid';
  106. lvDiv.style.left = lvLeft;
  107. lvDiv.style.bottom = lvBt;
  108. lvDiv.style.lineHeight = '1em';
  109. lvDiv.style.zIndex = '9999';
  110. lvDiv.style.position = 'fixed';
  111. lvDiv.style.backgroundColor = '#ffe';
  112. lvDiv.id = 'lv-box';
  113. lvDiv.setAttribute('draggable', 'true');
  114. window.document.body.appendChild(lvDiv);
  115.  
  116. var lvCloseDiv, lvClose, lvFB;
  117. lvCloseDiv = document.createElement('div');
  118. lvCloseDiv.id = 'lv-topbar';
  119. lvCloseDiv.style.textAlign = 'right';
  120. lvCloseDiv.style.background = '#810400';
  121. lvClose = document.createElement('span');
  122. lvClose.innerHTML = '[CLOSE]';
  123. lvClose.style.cursor = 'pointer';
  124. lvClose.style.padding = '0 5px';
  125. lvClose.style.textDecoration = 'underline';
  126. lvClose.onclick = function() {
  127. lvDiv.style.display = 'none';
  128. };
  129. lvCloseDiv.appendChild(lvClose);
  130. lvDiv.appendChild(lvCloseDiv);
  131. lvFB = document.createElement('div');
  132. lvFB.innerHTML = 'feedback';
  133. lvFB.style.cursor = 'pointer';
  134. lvFB.style.padding = '0 5px';
  135. lvFB.style.textDecoration = 'underline';
  136. lvFB.onclick = function() {
  137. window.open('https://github.com/doomred/localav/issues', 'FEED_ME_BUG');
  138. };
  139. lvDiv.appendChild(lvFB);
  140.  
  141. GM_xmlhttpRequest({
  142. method: "GET",
  143. url: playerDLURL,
  144. headers: {
  145. "Accept": "application/json"
  146. },
  147. onload: function(response) {
  148. playerData = JSON.parse(response.responseText);
  149. dynamicparts(playerData.result);
  150. }
  151. });
  152. /* HTML5 drag & drop element
  153. * Modified via:http://jsfiddle.net/robertc/kKuqH/
  154. * More info: https://stackoverflow.com/questions/6230834/html5-drag-and-drop-anywhere-on-the-screen */
  155. function drag_start(event) {
  156. var style = window.getComputedStyle(event.target, null);
  157. event.dataTransfer.setData("text/plain",
  158. (parseInt(style.getPropertyValue("left"),10) - event.clientX) + ',' + (parseInt(style.getPropertyValue("bottom"),10) + event.clientY));
  159. }
  160. function drag_over(event) {
  161. event.preventDefault();
  162. return false;
  163. }
  164. function drop(event) {
  165. var offset = event.dataTransfer.getData("text/plain").split(',');
  166. var dg = document.getElementById('lv-box');
  167. dg.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
  168. dg.style.bottom = (-event.clientY + parseInt(offset[1],10)) + 'px';
  169. GM_setValue('gm_lv_left', dg.style.left);
  170. GM_setValue('gm_lv_bt', dg.style.bottom);
  171. event.preventDefault();
  172. return false;
  173. }
  174. document.getElementById('lv-box').addEventListener('dragstart',drag_start,false);
  175. document.body.addEventListener('dragover',drag_over,false);
  176. document.body.addEventListener('drop',drop,false);
  177.  
  178. }
  179. window.setTimeout(lvMain, 1500); /* src attribute of iframe is dynamic generated, needs time */