feat: 增加透过 blob 下载文件的支持
This commit is contained in:
748
src-tauri/Cargo.lock
generated
748
src-tauri/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -12,12 +12,12 @@ rust-version = "1.63.0"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "1.2.1", features = [] }
|
||||
tauri-build = { version = "1.3.0", features = [] }
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0.89"
|
||||
serde = { version = "1.0.150", features = ["derive"] }
|
||||
tauri = { version = "1.2.4", features = ["api-all", "system-tray"] }
|
||||
tauri = { version = "1.3.0", features = ["api-all", "system-tray"] }
|
||||
download_rs = { version = "0.2.0", features = ["sync_download"] }
|
||||
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
|
||||
|
||||
|
||||
108
src-tauri/src/inject/event.js
vendored
108
src-tauri/src/inject/event.js
vendored
@@ -128,42 +128,58 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
});
|
||||
|
||||
const detectAnchorElementClick = (e) => {
|
||||
const anchorElement = e.target.closest('a');
|
||||
|
||||
if (anchorElement && anchorElement.href) {
|
||||
const target = anchorElement.target;
|
||||
anchorElement.target = '_self';
|
||||
const hrefUrl = new URL(anchorElement.href);
|
||||
const absoluteUrl = hrefUrl.href;
|
||||
if (absoluteUrl.includes('blob:')) {
|
||||
// convert blob url to binary file
|
||||
converBlobUrlToBinary(absoluteUrl).then(binary => {
|
||||
const tarui = window.__TAURI__;
|
||||
tarui.fs.writeBinaryFile(anchorElement.download, binary, {
|
||||
dir: tarui.fs.BaseDirectory.Download,
|
||||
});
|
||||
|
||||
})
|
||||
return;
|
||||
}
|
||||
|
||||
// Handling external link redirection.
|
||||
if (
|
||||
window.location.host !== hrefUrl.host &&
|
||||
(target === '_blank' || target === '_new')
|
||||
) {
|
||||
e.preventDefault();
|
||||
invoke('open_browser', {url: absoluteUrl});
|
||||
return;
|
||||
}
|
||||
|
||||
let filename = anchorElement.download ? anchorElement.download : getFilenameFromUrl(absoluteUrl)
|
||||
// Process download links for Rust to handle.
|
||||
// If the download attribute is set, the download attribute is used as the file name.
|
||||
if ((anchorElement.download || e.metaKey || e.ctrlKey || isDownloadLink(absoluteUrl))
|
||||
&& !externalDownLoadLink()
|
||||
) {
|
||||
e.preventDefault();
|
||||
invoke('download_file', {
|
||||
params: {
|
||||
url: absoluteUrl,
|
||||
filename,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Prevent some special websites from executing in advance, before the click event is triggered.
|
||||
document.addEventListener('mousedown', (e) => {
|
||||
const anchorElement = e.target.closest('a');
|
||||
document.addEventListener('mousedown', detectAnchorElementClick);
|
||||
document.addEventListener('click', detectAnchorElementClick);
|
||||
|
||||
if (anchorElement && anchorElement.href) {
|
||||
const target = anchorElement.target;
|
||||
anchorElement.target = '_self';
|
||||
const hrefUrl = new URL(anchorElement.href);
|
||||
const absoluteUrl = hrefUrl.href;
|
||||
|
||||
// Handling external link redirection.
|
||||
if (
|
||||
window.location.host !== hrefUrl.host &&
|
||||
(target === '_blank' || target === '_new')
|
||||
) {
|
||||
e.preventDefault();
|
||||
invoke('open_browser', {url: absoluteUrl});
|
||||
return;
|
||||
}
|
||||
|
||||
let filename = anchorElement.download ? anchorElement.download : getFilenameFromUrl(absoluteUrl)
|
||||
// Process download links for Rust to handle.
|
||||
// If the download attribute is set, the download attribute is used as the file name.
|
||||
if ((anchorElement.download || e.metaKey || e.ctrlKey || isDownloadLink(absoluteUrl))
|
||||
&& !externalDownLoadLink()
|
||||
) {
|
||||
e.preventDefault();
|
||||
invoke('download_file', {
|
||||
params: {
|
||||
url: absoluteUrl,
|
||||
filename,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
collectUrlToBlobs();
|
||||
|
||||
// Rewrite the window.open function.
|
||||
const originalWindowOpen = window.open;
|
||||
@@ -222,3 +238,27 @@ function toggleVideoPlayback(pause) {
|
||||
}
|
||||
}
|
||||
|
||||
// Collect blob urls to blob by overriding window.URL.createObjectURL
|
||||
function collectUrlToBlobs() {
|
||||
const backupCreateObjectURL = window.URL.createObjectURL;
|
||||
window.blobToUrlCaches = new Map();
|
||||
window.URL.createObjectURL = (blob) => {
|
||||
const url = backupCreateObjectURL.call(window.URL, blob);
|
||||
window.blobToUrlCaches.set(url, blob);
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function converBlobUrlToBinary(blobUrl) {
|
||||
return new Promise((resolve) => {
|
||||
const blob = window.blobToUrlCaches.get(blobUrl);
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.readAsArrayBuffer(blob);
|
||||
reader.onload = () => {
|
||||
resolve(reader.result);
|
||||
};
|
||||
})
|
||||
|
||||
}
|
||||
@@ -1,28 +1,43 @@
|
||||
{
|
||||
"package": {
|
||||
"productName": "WeRead",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"tauri": {
|
||||
"security": {
|
||||
"csp": null
|
||||
"package": {
|
||||
"productName": "WeRead",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"updater": {
|
||||
"active": false
|
||||
"tauri": {
|
||||
"security": {
|
||||
"csp": null,
|
||||
"dangerousRemoteDomainIpcAccess": [
|
||||
{
|
||||
"domain": "weread.qq.com",
|
||||
"windows": [
|
||||
"pake"
|
||||
],
|
||||
"enableTauriAPI": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"updater": {
|
||||
"active": false
|
||||
},
|
||||
"systemTray": {
|
||||
"iconPath": "png/icon_512.png",
|
||||
"iconAsTemplate": true
|
||||
},
|
||||
"allowlist": {
|
||||
"all": true,
|
||||
"fs": {
|
||||
"all": true,
|
||||
"scope": [
|
||||
"$DOWNLOAD/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"systemTray": {
|
||||
"iconPath": "png/weread_512.png",
|
||||
"iconAsTemplate": true
|
||||
},
|
||||
"allowlist": {
|
||||
"all": true
|
||||
"build": {
|
||||
"withGlobalTauri": true,
|
||||
"devPath": "../dist",
|
||||
"distDir": "../dist",
|
||||
"beforeBuildCommand": "",
|
||||
"beforeDevCommand": ""
|
||||
}
|
||||
},
|
||||
"build": {
|
||||
"withGlobalTauri": true,
|
||||
"devPath": "../dist",
|
||||
"distDir": "../dist",
|
||||
"beforeBuildCommand": "",
|
||||
"beforeDevCommand": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user