fix: issue-447 use addEventListener to listen anchorElement click
This commit is contained in:
34
src-tauri/src/inject/event.js
vendored
34
src-tauri/src/inject/event.js
vendored
@@ -156,6 +156,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let filename = anchorElement.download || getFilenameFromUrl(absoluteUrl);
|
let filename = 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 (
|
||||||
@@ -261,29 +262,32 @@ function convertBlobUrlToBinary(blobUrl) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function downloadFromBlobUrl(blobUrl, filename) {
|
||||||
|
const tauri = window.__TAURI__;
|
||||||
|
convertBlobUrlToBinary(blobUrl).then((binary) => {
|
||||||
|
console.log('binary', binary);
|
||||||
|
tauri.fs.writeBinaryFile(filename, binary, {
|
||||||
|
dir: tauri.fs.BaseDirectory.Download,
|
||||||
|
}).then(() => {
|
||||||
|
window.pakeToast('Download successful, saved to download directory~');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// detect blob download by createElement("a")
|
// detect blob download by createElement("a")
|
||||||
function detectDownloadByCreateAnchor() {
|
function detectDownloadByCreateAnchor() {
|
||||||
const createEle = document.createElement;
|
const createEle = document.createElement;
|
||||||
document.createElement = (el) => {
|
document.createElement = (el) => {
|
||||||
if (el !== "a") return createEle.call(document, el);
|
if (el !== "a") return createEle.call(document, el);
|
||||||
const anchorEle = createEle.call(document, el);
|
const anchorEle = createEle.call(document, el);
|
||||||
const anchorClick = anchorEle.click;
|
|
||||||
|
|
||||||
Object.defineProperties(anchorEle, {
|
// use addEventListener to avoid overriding the original click event.
|
||||||
click: {
|
anchorEle.addEventListener('click', () => {
|
||||||
get: () => {
|
const url = anchorEle.href;
|
||||||
if (anchorEle.href && anchorEle.href.includes('blob:')) {
|
if (window.blobToUrlCaches.has(url)) {
|
||||||
const url = anchorEle.href;
|
downloadFromBlobUrl(url, anchorEle.download || getFilenameFromUrl(url));
|
||||||
convertBlobUrlToBinary(url).then((binary) => {
|
|
||||||
tauri.fs.writeBinaryFile(anchorEle.download || getFilenameFromUrl(url), binary, {
|
|
||||||
dir: tauri.fs.BaseDirectory.Download,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return anchorClick.bind(anchorEle);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
return anchorEle;
|
return anchorEle;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user