AutoUnlock

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

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

  1. // ==UserScript==
  2. // @name AutoUnlock
  3. // @namespace https://github.com/maijz128
  4. // @version 0.1
  5. // @description 自动跳转并解锁百度网盘分享
  6. // @author MaiJZ
  7. // @supportURL https://github.com/maijz128/AutoUnlock
  8. // @match http://*/AutoUnlock/?open=*
  9. // @match https://*/AutoUnlock/?open=*
  10. // @match http://pan.baidu.com/share/init?shareid=*
  11. // @match https://pan.baidu.com/share/init?shareid=*
  12. // @grant GM_setValue
  13. // @grant GM_getValue
  14. // @grant GM_getTab
  15. // @grant GM_saveTab
  16. // @grant GM_getTabs
  17. // @grant unsafeWindow
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22.  
  23. setTimeout(function () {
  24. run();
  25. }, 1000);
  26. })();
  27.  
  28. function run() {
  29. var isAutoUnlockSite = location.href.indexOf("AutoUnlock") > -1;
  30. var isBaiduShareInitSite = location.href.indexOf("pan.baidu.com/share/init") > -1;
  31.  
  32. if (isAutoUnlockSite) {
  33. updateData();
  34. }
  35.  
  36. if (isBaiduShareInitSite) {
  37. unlock();
  38. }
  39. }
  40.  
  41. function updateData() {
  42. if (AutoUnlock) {
  43.  
  44. var autoUnlock = {
  45. updateTime: Date.now(),
  46. url: AutoUnlock.url,
  47. password: AutoUnlock.password
  48. };
  49. console.info(autoUnlock);
  50.  
  51.  
  52. // init tab
  53. GM_getTab(function (o) {
  54. var this_tab_data = o;
  55. this_tab_data[TAB_ID] = true;
  56. this_tab_data.AutoUnlock = autoUnlock;
  57. GM_saveTab(this_tab_data);
  58. });
  59.  
  60. // 更新数据后跳转到网盘
  61. setTimeout(function () {
  62. location.href = "http://" + AutoUnlock.url;
  63. }, 1000);
  64. }
  65.  
  66. }
  67.  
  68. function unlock() {
  69. var autoUnlock = null;
  70. //
  71. // GM_getTabs(function (db) {
  72. // var all_tabs = db;
  73. // var tab = null;
  74. // // for (var i in all_tabs) {
  75. // // tab = all_tabs[i];
  76. // //
  77. // // if (tab[TAB_ID]) {
  78. // // autoUnlock = tab.AutoUnlock;
  79. // // console.info(autoUnlock);
  80. // // console.info(tab);
  81. // // break;
  82. // // }
  83. // // }
  84. // });
  85.  
  86. GM_getTab(function (o) {
  87. tab = o;
  88. if (tab[TAB_ID]) {
  89. autoUnlock = tab.AutoUnlock;
  90. console.info(autoUnlock);
  91. }
  92.  
  93. if (autoUnlock) {
  94. const nowTime = Date.now();
  95. const updateTime = parseInt(autoUnlock.updateTime) || 0;
  96. const notOvertime = (nowTime - updateTime) < 30 * 1000;
  97.  
  98. if (notOvertime) {
  99. _unlock(autoUnlock.password);
  100.  
  101. // 使用之后设置为超时,令其不再可用
  102. // setPref(KEY_UpdateTime, 0);
  103. autoUnlock.updateTime = 0;
  104. tab.AutoUnlock = null;
  105. GM_saveTab(tab);
  106. }
  107. }
  108.  
  109. });
  110.  
  111. }
  112.  
  113. function _unlock(passowrd) {
  114. if (passowrd) {
  115. var input = document.getElementById(PAN_BAIDU_COM.InputID);
  116. var submitBtn = document.getElementById(PAN_BAIDU_COM.SubmitBtnID);
  117.  
  118. if (input && submitBtn) {
  119. input.value = passowrd;
  120. submitBtn.click();
  121. } else {
  122. setTimeout(function () {
  123. _unlock(passowrd);
  124. }, 100);
  125. }
  126. }
  127. }
  128.  
  129.  
  130. const TAB_ID = "Tab_AutoUnlock";
  131.  
  132. const PAN_BAIDU_COM = {
  133. SubmitBtnID: "submitBtn",
  134. InputID: "accessCode"
  135. };
  136.  
  137.  
  138. function setPref(name, value) { // cross-browser GM_setValue
  139. var a = '', b = '';
  140. try {
  141. a = typeof GM_setValue.toString;
  142. b = GM_setValue.toString()
  143. } catch (e) {
  144. }
  145. if (typeof GM_setValue === 'function' &&
  146. (a === 'undefined' || b.indexOf('not supported') === -1)) {
  147. GM_setValue(name, value); // Greasemonkey, Tampermonkey, Firefox extension
  148. } else {
  149. var ls = null;
  150. try {
  151. ls = window.localStorage || null
  152. } catch (e) {
  153. }
  154. if (ls) {
  155. return ls.setItem(name, value); // Chrome script, Opera extensions
  156. }
  157. }
  158. }
  159.  
  160. function getPref(name) { // cross-browser GM_getValue
  161. var a = '', b = '';
  162. try {
  163. a = typeof GM_getValue.toString;
  164. b = GM_getValue.toString()
  165. } catch (e) {
  166. }
  167. if (typeof GM_getValue === 'function' &&
  168. (a === 'undefined' || b.indexOf('not supported') === -1)) {
  169. return GM_getValue(name, null); // Greasemonkey, Tampermonkey, Firefox extension
  170. } else {
  171. var ls = null;
  172. try {
  173. ls = window.localStorage || null
  174. } catch (e) {
  175. }
  176. if (ls) {
  177. return ls.getItem(name); // Chrome script, Opera extensions
  178. }
  179. }
  180. }