Skip to content

Commit e933116

Browse files
committed
refactor: 优化从 webpack 中获得 axios 对象的方法
1 parent 51f14e1 commit e933116

File tree

1 file changed

+90
-76
lines changed

1 file changed

+90
-76
lines changed

weiyun.user.js

+90-76
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name WeiyunHelper
33
// @namespace https://pigfly.im/
4-
// @version 0.0.1
4+
// @version 0.0.2
55
// @description 微云下载时文件支持导出到 aria2 下载
66
// @author Luke
77
// @match https://www.weiyun.com/*
@@ -149,82 +149,96 @@
149149
}
150150
`);
151151

152-
// 获取 axios 对象
153-
const axios = webpackJsonp([], [], ['tIFN']);
154-
155-
axios.interceptors.response.use(
156-
(response) => {
157-
let { data, config } = response;
158-
let isDownload = false;
159-
let isSuccess = data.data.rsp_header.retcode === 0;
160-
let isDiskFileBatchDownload = config.url.indexOf('/webapp/json/weiyunQdiskClient/DiskFileBatchDownload') > -1;
161-
let isDiskFilePackageDownload = config.url.indexOf('/webapp/json/weiyunQdisk/DiskFilePackageDownload') > -1;
162-
let downloadUrl = '';
163-
let cookieName = '';
164-
let cookieValue = '';
165-
let URI = {};
166-
let fileName = '';
167-
168-
// 单个文件下载
169-
if (
170-
isSuccess &&
171-
isDiskFileBatchDownload &&
172-
data.data &&
173-
data.data.rsp_body &&
174-
data.data.rsp_body.RspMsg_body &&
175-
data.data.rsp_body.RspMsg_body.file_list &&
176-
data.data.rsp_body.RspMsg_body.file_list.length > 0
177-
) {
178-
let fileList = data.data.rsp_body.RspMsg_body.file_list;
179-
isDownload = true;
180-
downloadUrl = fileList[0].https_download_url;
181-
cookieName = fileList[0].cookie_name;
182-
cookieValue = fileList[0].cookie_value;
183-
URI = new URL(downloadUrl);
184-
fileName = decodeURI(URI.pathname.substr(URI.pathname.lastIndexOf('/') + 1));
152+
const chunkId = Math.random().toString(36).substring(7);
153+
webpackJsonp([7890], {
154+
[chunkId]: function (m, e, r) {
155+
let modules = Object.values(r.c)
156+
.filter((x) => !!x.exports.Axios)
157+
.map((x) => x.exports);
158+
159+
// 如果没有检查到 axios 对象则退出
160+
if (modules.length === 0) {
161+
console.error('没有检测到 axios 模块,已退出 WeiyunHelper');
162+
return false;
185163
}
186164

187-
// 批量下载文件
188-
if (
189-
isSuccess &&
190-
isDiskFilePackageDownload &&
191-
data.data &&
192-
data.data.rsp_body &&
193-
data.data.rsp_body.RspMsg_body
194-
) {
195-
let file = data.data.rsp_body.RspMsg_body;
196-
isDownload = true;
197-
downloadUrl = file.https_download_url;
198-
cookieName = file.cookie_name;
199-
cookieValue = file.cookie_value;
200-
fileName = `微云合并下载文件_${new Date().Format('yyyy-MM-dd hh:mm:ss')}.zip`;
201-
}
202-
203-
if (isDownload) {
204-
let ariaNgUrl = `http://aria2.me/aria-ng/#!/new/task?url=${btoa(
205-
downloadUrl
206-
)}&header=Cookie:${cookieName}=${cookieValue}&out=${encodeURI(fileName)}`;
207-
208-
console.log('文件名称:', fileName);
209-
console.log('下载地址:', downloadUrl);
210-
console.log('请求参数:', `Cookie:${cookieName}=${cookieValue}`);
211-
console.log('AriaNg URL:', ariaNgUrl);
212-
213-
// 发送消息通知提醒使用 ariaNg 下载
214-
wyNotify({
215-
title: `开始下载: ${fileName}`,
216-
description: '点击此处使用 AriaNg 下载',
217-
onclick: function () {
218-
window.open(ariaNgUrl);
219-
},
220-
});
221-
}
222-
223-
return response;
165+
let axios = modules[0];
166+
axios.interceptors.response.use(
167+
(response) => {
168+
let { data, config } = response;
169+
let isDownload = false;
170+
let isSuccess = data.data.rsp_header.retcode === 0;
171+
let isDiskFileBatchDownload =
172+
config.url.indexOf('/webapp/json/weiyunQdiskClient/DiskFileBatchDownload') > -1;
173+
let isDiskFilePackageDownload = config.url.indexOf('/webapp/json/weiyunQdisk/DiskFilePackageDownload') > -1;
174+
let downloadUrl = '';
175+
let cookieName = '';
176+
let cookieValue = '';
177+
let URI = {};
178+
let fileName = '';
179+
180+
// 单个文件下载
181+
if (
182+
isSuccess &&
183+
isDiskFileBatchDownload &&
184+
data.data &&
185+
data.data.rsp_body &&
186+
data.data.rsp_body.RspMsg_body &&
187+
data.data.rsp_body.RspMsg_body.file_list &&
188+
data.data.rsp_body.RspMsg_body.file_list.length > 0
189+
) {
190+
let fileList = data.data.rsp_body.RspMsg_body.file_list;
191+
isDownload = true;
192+
downloadUrl = fileList[0].https_download_url;
193+
cookieName = fileList[0].cookie_name;
194+
cookieValue = fileList[0].cookie_value;
195+
URI = new URL(downloadUrl);
196+
fileName = decodeURI(URI.pathname.substr(URI.pathname.lastIndexOf('/') + 1));
197+
}
198+
199+
// 批量下载文件
200+
if (
201+
isSuccess &&
202+
isDiskFilePackageDownload &&
203+
data.data &&
204+
data.data.rsp_body &&
205+
data.data.rsp_body.RspMsg_body
206+
) {
207+
let file = data.data.rsp_body.RspMsg_body;
208+
isDownload = true;
209+
downloadUrl = file.https_download_url;
210+
cookieName = file.cookie_name;
211+
cookieValue = file.cookie_value;
212+
fileName = `微云合并下载文件_${new Date().Format('yyyy-MM-dd hh:mm:ss')}.zip`;
213+
}
214+
215+
if (isDownload) {
216+
let ariaNgUrl = `http://aria2.me/aria-ng/#!/new/task?url=${btoa(
217+
downloadUrl
218+
)}&header=Cookie:${cookieName}=${cookieValue}&out=${encodeURI(fileName)}`;
219+
220+
console.log('文件名称:', fileName);
221+
console.log('下载地址:', downloadUrl);
222+
console.log('请求参数:', `Cookie:${cookieName}=${cookieValue}`);
223+
console.log('AriaNg URL:', ariaNgUrl);
224+
225+
// 发送消息通知提醒使用 ariaNg 下载
226+
wyNotify({
227+
title: `开始下载: ${fileName}`,
228+
description: '点击此处使用 AriaNg 下载',
229+
onclick: function () {
230+
window.open(ariaNgUrl);
231+
},
232+
});
233+
}
234+
235+
return response;
236+
},
237+
(error) => {
238+
console.log('发生错误', error);
239+
return Promise.reject(error);
240+
}
241+
);
224242
},
225-
(error) => {
226-
console.log('发生错误', error);
227-
return Promise.reject(error);
228-
}
229-
);
243+
}, [chunkId]);
230244
})();

0 commit comments

Comments
 (0)