Skip to content

Commit

Permalink
feat: update download
Browse files Browse the repository at this point in the history
  • Loading branch information
kuoruan committed Jan 23, 2024
1 parent 3180516 commit 131b3f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
47 changes: 32 additions & 15 deletions src/components/TorrentDownloadItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export default {
url: pageLink,
timeout: 5000,
context: { pageTitle },
ontimeout: function () {
ontimeout: () => {
reject(new Error("下载超时,请重试!"));
},
onerror: function () {
onerror: () => {
reject(new Error("下载失败,请重试!"));
},
onload: function ({ context = {}, responseText = "" }) {
onload: ({ context = {}, responseText = "" }) => {
let matches;
if (
responseText &&
Expand All @@ -78,24 +78,41 @@ export default {
});
});
},
downloadTorrent(url, name) {
return new Promise((resolve, reject) => {
GM_download({
url,
name,
saveAs: true,
conflictAction: "prompt",
onload: function () {
resolve();
},
ontimeout: function () {
async downloadTorrent(torrentUrl, torrentName) {
const blob = await new Promise((resolve, reject) => {
// direct download of .torrent file is not allowed by default
// so we can not use GM_download now
GM_xmlhttpRequest({
method: "GET",
url: torrentUrl,
responseType: "blob",
timeout: 5000,
ontimeout: () => {
reject(new Error("下载超时,请重试!"));
},
onerror: function () {
onerror: () => {
reject(new Error("下载失败,请重试!"));
},
onload: ({ response }) => {
resolve(response);
},
});
});
const herf = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = herf;
anchor.style.display = "none";
anchor.download = torrentName;
this.$el.appendChild(anchor);
anchor.click();
setTimeout(() => {
this.$el.removeChild(anchor);
URL.revokeObjectURL(herf);
}, 0);
},
async getAndDownloadTorrent() {
if (!this.detailLink) {
Expand Down
2 changes: 1 addition & 1 deletion userscript.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
"https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js",
],
connect: ["dmhy.org", "b168.net"],
grant: ["GM_addStyle", "GM_setClipboard", "GM_xmlhttpRequest", "GM_download"],
grant: ["GM_addStyle", "GM_setClipboard", "GM_xmlhttpRequest"],
source: "https://github.com/kuoruan/dmhy-download-helper.git",
license: packageConfig.license,
"run-at": "document-end",
Expand Down
9 changes: 5 additions & 4 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ if (fs.existsSync(buildNumberFile)) {
export default defineConfig(({ mode }) => {
const isDev = mode === "dev";

if (isDev) {
writeNewBuildNumber(++buildNumber);
}

return {
resolve: {
alias: {
Expand All @@ -44,6 +40,7 @@ export default defineConfig(({ mode }) => {
minify: false,
cssMinify: true,
cssCodeSplit: false,
sourcemap: false,
assetsInlineLimit: Number.POSITIVE_INFINITY, // always inline assets
rollupOptions: {
input: {
Expand All @@ -66,6 +63,10 @@ export default defineConfig(({ mode }) => {
vue2(),
banner({
content: (fileName) => {
if (isDev) {
writeNewBuildNumber(++buildNumber);
}

return fileName.endsWith(".js")
? createBanner(isDev, buildNumber)
: "";
Expand Down

0 comments on commit 131b3f3

Please sign in to comment.