Custom aliyundrive

阿里云直链导出

  1. // ==UserScript==
  2. // @name Custom aliyundrive
  3. // @namespace https://github.com/invobzvr
  4. // @version 1.18
  5. // @description 阿里云直链导出
  6. // @author invobzvr
  7. // @match *://www.aliyundrive.com/drive*
  8. // @match *://www.aliyundrive.com/s/*
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_xmlhttpRequest
  12. // @connect 127.0.0.1
  13. // @connect localhost
  14. // @connect *
  15. // @require https://gf.qytechs.cn/scripts/443030-hook-js/code/Hookjs.js
  16. // @require https://gf.qytechs.cn/scripts/447483-box-js/code/Boxjs.js
  17. // @homepageURL https://github.com/invobzvr/invotoys.js/tree/main/aliyundrive
  18. // @supportURL https://github.com/invobzvr/invotoys.js/issues
  19. // @license GPL-3.0
  20. // ==/UserScript==
  21.  
  22. (function () {
  23. const that = {
  24. Toast: Box.mixin({ toast: true, time: 3e3 }),
  25. a2config: GM_getValue('a2config', {
  26. host: '127.0.0.1',
  27. port: 6800,
  28. dir: 'Download',
  29. }),
  30. xhr: function (details) {
  31. return new Promise((res, rej) => {
  32. GM_xmlhttpRequest(Object.assign(details, {
  33. onerror: rej,
  34. onload: res,
  35. }));
  36. });
  37. },
  38. wait: function (selectors, key) {
  39. return new Promise(res => {
  40. let el = document.querySelector(selectors),
  41. iid = setInterval(() => (el ? true : el = document.querySelector(selectors)) && (key ? el[key] : true) && (clearInterval(iid), res(el)), 100);
  42. });
  43. },
  44. install: async function () {
  45. that.inithook();
  46. addEventListener('pushstate', that.onPushState);
  47. that.rk = `__reactFiber$${Object.keys(await that.wait('#root', '_reactRootContainer')).find(ii => ii.startsWith('__reactContainer$')).split('$')[1]}`;
  48. that.tbwmo = new MutationObserver(that.tbwmc);
  49. that.ddmmo = new MutationObserver(that.ddmmc);
  50. that.ddmmo.observe(document.body, { childList: true });
  51. that.mmmo = new MutationObserver(that.mmmc);
  52. that.init();
  53. },
  54. inithook: function () {
  55. History.prototype.pushState.hook({
  56. scope: History.prototype,
  57. before: function () {
  58. dispatchEvent(new CustomEvent('pushstate', { detail: arguments[2] }));
  59. },
  60. });
  61. },
  62. init: async function () {
  63. that.listModel = (await that.wait('[class*=node-list--]'))[that.rk].return.memoizedProps.listModel;
  64. that.tbwmo.observe(document.querySelector('[class*=page-content--]'), { childList: true });
  65. },
  66. tbwmc: function ([mr]) {
  67. if (mr.addedNodes.length && (that.tbwel = mr.addedNodes[0].querySelector('[class*=toolbar-wrapper]'))) {
  68. let btn = that.tbwel.firstChild;
  69. that.tbwel.insertAdjacentHTML('afterbegin', '<div style="background:#fff;height:30px;margin-left:8px;width:.1px"></div>');
  70. let dlBtn = that.tbwel.insertAdjacentElement('afterbegin', btn.cloneNode(true)),
  71. a2Btn = that.tbwel.insertAdjacentElement('afterbegin', btn.cloneNode(true));
  72. dlBtn.title = 'Download';
  73. dlBtn.addEventListener('click', () => that.download(that.listModel.selectedItems, that.normal));
  74. a2Btn.title = 'Aria2';
  75. a2Btn.addEventListener('click', () => that.download(that.listModel.selectedItems, that.aria2, () => [...that.listModel.selectedIds].forEach(that.listModel.removeSelect)));
  76. a2Btn.addEventListener('contextmenu', evt => (evt.preventDefault(), that.configa2(true)));
  77. }
  78. },
  79. ddmmc: function ([mr]) {
  80. let ddm = mr.addedNodes.length && mr.addedNodes[0].querySelector('[class*=dropdown-menu--]');
  81. ddm && that.listModel && that.mmmo.observe(ddm, { attributes: true, attributeFilter: ['class'] });
  82. },
  83. mmmc: function ([mr]) {
  84. let el = mr.target;
  85. if (el.className.includes('-prepare')) {
  86. let props = el[that.rk].child.memoizedProps,
  87. list = location.pathname.startsWith('/s/') ? [props.model] : props.fileModel ? [props.fileModel] : props.fileListModel ? props.fileListModel.selectedItems : null;
  88. if (!list) {
  89. return;
  90. }
  91. let dlBtn, a2Btn,
  92. ul = el.firstChild;
  93. if (!el.querySelector('[custom]')) {
  94. let btn = ul.firstChild;
  95. ul.insertAdjacentHTML('afterbegin', '<li class="ant-dropdown-menu-item-divider" custom></li>');
  96. dlBtn = ul.insertAdjacentElement('afterbegin', btn.cloneNode(true));
  97. dlBtn.setAttribute('custom', 'normal')
  98. dlBtn.querySelector('[class*=menu-name--]').innerText = 'Download';
  99. a2Btn = ul.insertAdjacentElement('afterbegin', btn.cloneNode(true));
  100. a2Btn.setAttribute('custom', 'aria2')
  101. a2Btn.querySelector('[class*=menu-name--]').innerText = 'Aria2';
  102. } else {
  103. dlBtn = ul.querySelector('[custom=normal]');
  104. a2Btn = ul.querySelector('[custom=aria2]');
  105. }
  106. dlBtn.onclick = () => (that.closeMenu(), that.download(list, that.normal));
  107. a2Btn.onclick = () => (that.closeMenu(), that.download(list, that.aria2));
  108. a2Btn.oncontextmenu = evt => (evt.preventDefault(), evt.stopPropagation(), that.closeMenu(), that.configa2(true));
  109. }
  110. },
  111. closeMenu: function () {
  112. setTimeout(() => document.body.dispatchEvent(new MouseEvent('mousedown', { bubbles: true })));
  113. },
  114. onPushState: function (evt) {
  115. if (evt.detail === '/drive/' || evt.detail.startsWith('/drive/folder') || evt.detail.startsWith('/s/')) {
  116. !that.listModel && that.init();
  117. } else {
  118. that.listModel = null;
  119. that.tbwmo.disconnect();
  120. }
  121. },
  122. download: async function (list, func, callback) {
  123. list.length !== (list = list.filter(ii => ii.type == 'file')).length && new that.Toast({
  124. type: 'warning',
  125. title: 'Folders are skipped',
  126. });
  127. list.length && await func(list) && callback && callback();
  128. },
  129. normal: async function (list) {
  130. if (list.length === 1) {
  131. location.href = await that.urlOf(list[0]);
  132. } else {
  133. new Box({
  134. title: 'Urls',
  135. html: `<textarea style="height:${window.innerHeight * .5}px;width:100%;white-space:nowrap"></textarea>`,
  136. didBuild: async modal => {
  137. modal.style.width = '50%';
  138. modal.querySelector('textarea').value = (await Promise.all(list.map(ii => that.urlOf(ii)))).join('\n');
  139. },
  140. });
  141. }
  142. return true;
  143. },
  144. aria2: async function (list) {
  145. let a2config;
  146. if (!that.a2config.remember) {
  147. let ret = await that.configa2(false, modal => {
  148. let names = [...document.querySelectorAll('#root [class*=breadcrumb-item--]')];
  149. names = names.slice(1, names.length / 2).map(ii => ii.dataset.label);
  150. modal.querySelector('[name=dir]').insertAdjacentHTML('afterend', `<details>
  151. <summary><label class="box-option-item"><input class="box-input" name="wds" type="checkbox" onchange="this.closest('details').open = this.checked"><span class="box-label">with directory structure</span></label></summary>
  152. <input class="box-input" name="struct" value="${names.join('/')}">
  153. </details>`);
  154. });
  155. if (ret) {
  156. a2config = ret;
  157. } else {
  158. new that.Toast({
  159. type: 'warning',
  160. title: 'Cancelled',
  161. });
  162. return false;
  163. }
  164. }
  165. !a2config && (a2config = that.a2config);
  166. let dir = a2config.dir;
  167. a2config.wds && (dir = `${dir}/${a2config.struct}`);
  168. let data = {
  169. id: 'INVOTOYS',
  170. jsonrpc: '2.0',
  171. method: 'system.multicall',
  172. params: [await Promise.all(list.map(async ii => ({
  173. methodName: 'aria2.addUri',
  174. params: [[await that.urlOf(ii)], {
  175. dir: dir,
  176. out: ii.name,
  177. referer: 'https://www.aliyundrive.com/',
  178. 'user-agent': navigator.userAgent,
  179. }],
  180. })))],
  181. };
  182. if (a2config.token) {
  183. let token = `token:${a2config.token}`;
  184. data.params[0].forEach(ii => ii.params.unshift(token));
  185. }
  186. let res = await that.xhr({
  187. method: 'post',
  188. responseType: 'json',
  189. url: `http${a2config.https ? 's' : ''}://${a2config.host}:${a2config.port}/jsonrpc`,
  190. data: JSON.stringify(data),
  191. }).catch(err => err);
  192. new that.Toast(res.status == 200 ? {
  193. type: 'success',
  194. title: 'Sent to Aria2 successfully',
  195. } : {
  196. type: 'error',
  197. title: 'Failed to connect to Aria2',
  198. text: res.error,
  199. });
  200. return true;
  201. },
  202. urlOf: async function (model) {
  203. return model.downloadUrl || model.url || await model.getDownloadUrl();
  204. },
  205. configa2: async function (save, didBuild) {
  206. let ret = await new Box({
  207. title: 'Aria2 Config',
  208. html: `<form>
  209. <div class="box-input-group"><span class="box-label">Host</span><input class="box-input" name="host" value="${that.a2config.host}"></div>
  210. <div class="box-input-group"><span class="box-label">Port</span><input class="box-input" name="port" value="${that.a2config.port}"></div>
  211. <div class="box-input-group"><span class="box-label">Dir</span><input class="box-input" name="dir" value="${that.a2config.dir}"></div>
  212. <div class="box-input-group"><span class="box-label">Token</span><input class="box-input" name="token" value="${that.a2config.token || ''}"></div>
  213. <div class="box-options">
  214. <label class="box-option-item"><input class="box-input" name="https" type="checkbox"><span class="box-label"${that.a2config.https ? ' checked' : ''}>Https</span></label>
  215. <label class="box-option-item"><input class="box-input" name="remember" type="checkbox"><span class="box-label"${that.a2config.remember ? ' checked' : ''}>Remember</span></label>
  216. </div>
  217. </form>`,
  218. actions: {
  219. OK: modal => Object.fromEntries(new FormData(modal.querySelector('form'))),
  220. },
  221. didBuild: didBuild,
  222. });
  223. ret && (save || ret.remember) && GM_setValue('a2config', that.a2config = ret);
  224. return ret;
  225. },
  226. };
  227.  
  228. that.install();
  229. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址