AutoUnlock

自动跳转并解锁百度网盘、Mega分享

当前为 2017-07-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AutoUnlock
  3. // @namespace https://greasyfork.org/scripts/31324-autounlock
  4. // @version 0.2.2
  5. // @description 自动跳转并解锁百度网盘、Mega分享
  6. // @author MaiJZ
  7. // @homepageURL https://github.com/maijz128/AutoUnlock
  8. // @supportURL https://github.com/maijz128/AutoUnlock
  9. // @match http://maijz128.github.io/AutoUnlock/AutoUnlock/?open=*
  10. // @match https://maijz128.github.io/AutoUnlock/AutoUnlock/?open=*
  11. // @match http://pan.baidu.com/share/init?shareid=*
  12. // @match https://pan.baidu.com/share/init?shareid=*
  13. // @match http://localhost:8094/AutoUnlock/?open=*
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @grant GM_getTab
  17. // @grant GM_saveTab
  18. // @grant GM_getTabs
  19. // @grant unsafeWindow
  20. // ==/UserScript==
  21.  
  22. const SITE_WAIT_TIME = 500;
  23. const DATA_OVER_TIME = 10 * 1000;
  24.  
  25. (function () {
  26. 'use strict';
  27. run();
  28. })();
  29.  
  30. function run() {
  31. var isAutoUnlockSite = location.href.indexOf("AutoUnlock") > -1;
  32. var isBaiduShareInitSite = location.href.indexOf("pan.baidu.com/share/init") > -1;
  33.  
  34. if (isAutoUnlockSite) {
  35. var inter = setInterval(function () {
  36. if (AutoUnlock.done) {
  37. clearInterval(inter);
  38. handle(AutoUnlock);
  39. }
  40. }, 50);
  41. } else if (isBaiduShareInitSite) {
  42. unlock_baidu();
  43. }
  44. }
  45.  
  46. function handle(autoUnlock) {
  47. const url = autoUnlock.url;
  48. const password = autoUnlock.password;
  49. const isMega = url.indexOf("mega.nz") > -1;
  50. const isBaiduPan = url.indexOf("pan.baidu.com") > -1;
  51.  
  52.  
  53. if (isBaiduPan) {
  54. handleBaidu(url, password);
  55. } else if (isMega) {
  56. handleMega(url, password);
  57. }
  58. }
  59.  
  60. function handleBaidu(url, password) {
  61. var autoUnlock = {
  62. updateTime: Date.now(),
  63. url: url,
  64. password: password
  65. };
  66. console.group("AutoUnlockSite >> pan.baiu.com:");
  67. console.info(autoUnlock);
  68. console.groupEnd();
  69.  
  70. // init tab
  71. GM_getTab(function (o) {
  72. var this_tab_data = o;
  73. this_tab_data[TAB_ID] = true;
  74. this_tab_data.AutoUnlock = autoUnlock;
  75. GM_saveTab(this_tab_data);
  76.  
  77. jumpSite(url);
  78. });
  79.  
  80. }
  81.  
  82. function handleMega(url, password) {
  83. var targetURL = url + password;
  84. jumpSite(targetURL);
  85. }
  86.  
  87. // 更新数据后跳转到网盘
  88. function jumpSite(url) {
  89. var targetURL = url;
  90. if (targetURL.indexOf("http") !== 0) {
  91. targetURL = "http://" + targetURL;
  92. }
  93. setTimeout(function () {
  94. if (targetURL) {
  95. location.href = targetURL;
  96. }
  97. }, SITE_WAIT_TIME);
  98. }
  99.  
  100. function unlock_baidu() {
  101. var autoUnlock = null;
  102.  
  103. GM_getTab(function (o) {
  104. var tab = o;
  105. if (tab[TAB_ID]) {
  106. autoUnlock = tab.AutoUnlock;
  107. console.group("AutoUnlock:");
  108. console.info(autoUnlock);
  109. console.groupEnd();
  110. }
  111.  
  112. if (autoUnlock) {
  113. const nowTime = Date.now();
  114. const updateTime = parseInt(autoUnlock.updateTime) || 0;
  115. const notOvertime = (nowTime - updateTime) < DATA_OVER_TIME;
  116.  
  117. if (notOvertime) {
  118. _unlock_baidu(autoUnlock.password);
  119. } else {
  120. console.error("数据已超时!");
  121. }
  122. }
  123. });
  124.  
  125. }
  126.  
  127. function _unlock_baidu(password, count) {
  128. const MAX_TIME = 10 * 1000;
  129. const INTERVAL = 50;
  130. const MAX_COUNT = MAX_TIME / INTERVAL;
  131. count = count || 1;
  132.  
  133. console.log("password: " + password + " count: " + count);
  134. if (count < MAX_COUNT && password) {
  135.  
  136. var input = document.getElementById(PAN_BAIDU_COM.InputID);
  137. var submitBtn = document.getElementById(PAN_BAIDU_COM.SubmitBtnID);
  138. if (input && submitBtn) {
  139. input.value = password;
  140. submitBtn.click();
  141. }
  142.  
  143. setTimeout(function () {
  144. _unlock_baidu(password, count + 1);
  145. }, INTERVAL);
  146. }
  147. }
  148.  
  149.  
  150. const TAB_ID = "Tab_AutoUnlock";
  151.  
  152. const PAN_BAIDU_COM = {
  153. SubmitBtnID: "submitBtn",
  154. InputID: "accessCode"
  155. };
  156.  
  157.  
  158. //
  159. // GM_getTabs(function (db) {
  160. // var all_tabs = db;
  161. // var tab = null;
  162. // // for (var i in all_tabs) {
  163. // // tab = all_tabs[i];
  164. // //
  165. // // if (tab[TAB_ID]) {
  166. // // autoUnlock = tab.AutoUnlock;
  167. // // console.info(autoUnlock);
  168. // // console.info(tab);
  169. // // break;
  170. // // }
  171. // // }
  172. // });
  173.  
  174. function setPref(name, value) { // cross-browser GM_setValue
  175. var a = '', b = '';
  176. try {
  177. a = typeof GM_setValue.toString;
  178. b = GM_setValue.toString()
  179. } catch (e) {
  180. }
  181. if (typeof GM_setValue === 'function' &&
  182. (a === 'undefined' || b.indexOf('not supported') === -1)) {
  183. GM_setValue(name, value); // Greasemonkey, Tampermonkey, Firefox extension
  184. } else {
  185. var ls = null;
  186. try {
  187. ls = window.localStorage || null
  188. } catch (e) {
  189. }
  190. if (ls) {
  191. return ls.setItem(name, value); // Chrome script, Opera extensions
  192. }
  193. }
  194. }
  195.  
  196. function getPref(name) { // cross-browser GM_getValue
  197. var a = '', b = '';
  198. try {
  199. a = typeof GM_getValue.toString;
  200. b = GM_getValue.toString()
  201. } catch (e) {
  202. }
  203. if (typeof GM_getValue === 'function' &&
  204. (a === 'undefined' || b.indexOf('not supported') === -1)) {
  205. return GM_getValue(name, null); // Greasemonkey, Tampermonkey, Firefox extension
  206. } else {
  207. var ls = null;
  208. try {
  209. ls = window.localStorage || null
  210. } catch (e) {
  211. }
  212. if (ls) {
  213. return ls.getItem(name); // Chrome script, Opera extensions
  214. }
  215. }
  216. }