(
function
() {
'use strict'
;
console.log(
'%c ☘️: 脚本加载... '
,
'font-size:16px;background-color:#4b5246;color:white;'
);
const CONFIG = {
FILE_NAME:
''
,
POLLING_COUNT: 300
};
const clearStore = () => {
console.log(
"%c 🏭: 清空储存 "
,
"font-size:16px;background-color:#700a6f;color:white;"
)
localStorage.removeItem(
'QZT_TEST'
);
Cookies.remove(
'Countdown'
);
};
const parse = (target) => {
let
o =
null
;
try
{
if
(
typeof
target ===
'string'
) {
o = JSON.parse(target);
}
}
catch
(error) {}
return
o;
};
const doDownload = (blob) => {
const fileName = CONFIG.FILE_NAME;
const link = document.createElement(
'a'
);
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
window.URL.revokeObjectURL(link.href);
};
const request = (url) =>
new
Promise((resolve) => {
GM_xmlhttpRequest({
method:
'GET'
,
url,
responseType:
'blob'
,
onload: async (res) => {
console.log(
'%c 📯: res '
,
'font-size:16px;background-color:#9630d2;color:white;'
, res);
const text = await res.response.text();
const blob = parse(text);
if
(blob?.error_code != 101 && res.status === 200) {
doDownload(res.response);
return
resolve(
'break'
);
}
resolve(res);
},
onerror: async (error) => {
console.log(
'%c 📟: parse -> error '
,
'font-size:16px;background-color:#818a1a;color:white;'
,
error
);
resolve(
null
);
}
});
});
const renderDOM = () => {
$(
'#ptDownload'
)
.clone()
.attr(
'id'
,
'ptDownload-clone'
)
.css({ padding:
'0 15px'
, width:
'auto'
})
.find(
'.s'
)
.text(
'轮询下载'
)
.parent()
.css({
'background-color'
:
'#eadd45'
, color:
'#fff'
})
.appendTo($(
'.download-handle'
));
};
let
downloadCount = 0;
const recursionDownloadRequest = async (url) => {
console.log(
'%c 👩‍💼: recursionDownloadRequest -> downloadCount '
,
'font-size:16px;background-color:#18df40;color:black;'
,
downloadCount
);
$(
'#ptDownload-clone'
).find(
'.s'
).text(`轮询中 (${downloadCount}/300)`);
if
(downloadCount >= CONFIG.POLLING_COUNT) {
$(
'#ptDownload-clone'
).find(
'.s'
).text(`轮询下载`);
downloadCount = 0;
return
;
}
try
{
const result = await request(url);
if
(result ===
'break'
) {
$(
'#ptDownload-clone'
).find(
'.s'
).text(`轮询下载`);
downloadCount = 0;
return
;
}
setTimeout(() => {
recursionDownloadRequest(url);
}, 10);
}
catch
(error) {
console.log(
'%c 🗻: recursionDownloadRequest -> error '
,
'font-size:16px;background-color:#1cd9a7;color:black;'
,
error
);
}
downloadCount++;
};
setTimeout(() => {
console.log(
'%c 🌽: 渲染 '
,
'font-size:16px;background-color:#04b8c2;color:white;'
);
renderDOM();
$(
'#ptDownload-clone'
).click(
function
() {
clearStore();
const url = $(
this
).data(
'url'
);
if
(url) {
const downloadUrl = HOST.DOWNLOAD + API.qztDownload +
'?url='
+ encodeURIComponent(url);
CONFIG.FILE_NAME = url.split(
'/'
).pop();
recursionDownloadRequest(downloadUrl);
}
});
});
})();