fix: Solve vitural anchorEle click event cannot detect by addEventListener to start download
This commit is contained in:
42
src-tauri/src/inject/event.js
vendored
42
src-tauri/src/inject/event.js
vendored
@@ -146,16 +146,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const hrefUrl = new URL(anchorElement.href);
|
const hrefUrl = new URL(anchorElement.href);
|
||||||
const absoluteUrl = hrefUrl.href;
|
const absoluteUrl = hrefUrl.href;
|
||||||
|
|
||||||
// Convert blob url to binary file
|
|
||||||
if (absoluteUrl.includes('blob:')) {
|
|
||||||
convertBlobUrlToBinary(absoluteUrl).then((binary) => {
|
|
||||||
tauri.fs.writeBinaryFile(anchorElement.download, binary, {
|
|
||||||
dir: tauri.fs.BaseDirectory.Download,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handling external link redirection.
|
// Handling external link redirection.
|
||||||
if (
|
if (
|
||||||
window.location.host !== hrefUrl.host &&
|
window.location.host !== hrefUrl.host &&
|
||||||
@@ -166,9 +156,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let filename = anchorElement.download
|
let filename = anchorElement.download || getFilenameFromUrl(absoluteUrl);
|
||||||
? anchorElement.download
|
|
||||||
: getFilenameFromUrl(absoluteUrl);
|
|
||||||
// Process download links for Rust to handle.
|
// Process download links for Rust to handle.
|
||||||
// If the download attribute is set, the download attribute is used as the file name.
|
// If the download attribute is set, the download attribute is used as the file name.
|
||||||
if (
|
if (
|
||||||
@@ -193,6 +181,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
document.addEventListener('click', detectAnchorElementClick, true);
|
document.addEventListener('click', detectAnchorElementClick, true);
|
||||||
|
|
||||||
collectUrlToBlobs();
|
collectUrlToBlobs();
|
||||||
|
detectDownloadByCreateAnchor();
|
||||||
|
|
||||||
// Rewrite the window.open function.
|
// Rewrite the window.open function.
|
||||||
const originalWindowOpen = window.open;
|
const originalWindowOpen = window.open;
|
||||||
@@ -272,3 +261,30 @@ function convertBlobUrlToBinary(blobUrl) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// detect blob download by createElement("a")
|
||||||
|
function detectDownloadByCreateAnchor() {
|
||||||
|
const createEle = document.createElement;
|
||||||
|
document.createElement = (el) => {
|
||||||
|
if (el !== "a") return createEle.call(document, el);
|
||||||
|
const anchorEle = createEle.call(document, el);
|
||||||
|
const anchorClick = anchorEle.click;
|
||||||
|
|
||||||
|
Object.defineProperties(anchorEle, {
|
||||||
|
click: {
|
||||||
|
get: () => {
|
||||||
|
if (anchorEle.href && anchorEle.href.includes('blob:')) {
|
||||||
|
const url = anchorEle.href;
|
||||||
|
convertBlobUrlToBinary(url).then((binary) => {
|
||||||
|
tauri.fs.writeBinaryFile(anchorEle.download || getFilenameFromUrl(url), binary, {
|
||||||
|
dir: tauri.fs.BaseDirectory.Download,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return anchorClick.bind(anchorEle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user