File download function optimization.

This commit is contained in:
Tw93
2023-06-04 12:53:36 +08:00
parent e70b29468a
commit e369d8239a

View File

@@ -42,7 +42,9 @@ function handleShortcut(event) {
//这里参考 ChatGPT 的代码 //这里参考 ChatGPT 的代码
const uid = () => window.crypto.getRandomValues(new Uint32Array(1))[0]; const uid = () => window.crypto.getRandomValues(new Uint32Array(1))[0];
function transformCallback(callback = () => {}, once = false) {
function transformCallback(callback = () => {
}, once = false) {
const identifier = uid(); const identifier = uid();
const prop = `_${identifier}`; const prop = `_${identifier}`;
Object.defineProperty(window, prop, { Object.defineProperty(window, prop, {
@@ -57,6 +59,7 @@ function transformCallback(callback = () => {}, once = false) {
}); });
return identifier; return identifier;
} }
async function invoke(cmd, args) { async function invoke(cmd, args) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!window.__TAURI_POST_MESSAGE__) if (!window.__TAURI_POST_MESSAGE__)
@@ -78,6 +81,23 @@ async function invoke(cmd, args) {
}); });
} }
// Judgment of file download.
function isDownloadLink(url) {
const fileExtensions = [
'3gp', '7z', 'ai', 'apk', 'avi', 'bmp', 'csv', 'dmg', 'doc', 'docx', 'fla', 'flv', 'gif', 'gz', 'gzip',
'ico', 'iso', 'indd', 'jar', 'jpeg', 'jpg', 'm3u8', 'mov', 'mp3', 'mp4', 'mpa', 'mpg',
'mpeg', 'msi', 'odt', 'ogg', 'ogv', 'pdf', 'png', 'ppt', 'pptx', 'psd', 'rar', 'raw', 'rss', 'svg',
'swf', 'tar', 'tif', 'tiff', 'ts', 'txt', 'wav', 'webm', 'webp', 'wma', 'wmv', 'xls', 'xlsx', 'xml', 'zip'
];
const downloadLinkPattern = new RegExp(`\\.(${fileExtensions.join('|')})$`, 'i');
return downloadLinkPattern.test(url);
}
// No need to go to the download link.
function externalDownLoadLink() {
return ['quickref.me'].indexOf(location.hostname) > -1;
}
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const topDom = document.createElement('div'); const topDom = document.createElement('div');
topDom.id = 'pack-top-dom'; topDom.id = 'pack-top-dom';
@@ -130,7 +150,7 @@ document.addEventListener('DOMContentLoaded', () => {
let filename = anchorElement.download ? anchorElement.download : getFilenameFromUrl(absoluteUrl) let filename = anchorElement.download ? 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 ((anchorElement.download || e.metaKey || e.ctrlKey) if ((anchorElement.download || e.metaKey || e.ctrlKey || isDownloadLink(absoluteUrl))
&& !externalDownLoadLink() && !externalDownLoadLink()
) { ) {
e.preventDefault(); e.preventDefault();
@@ -188,10 +208,6 @@ function removeUrlParameters(url) {
return parsedUrl.toString(); return parsedUrl.toString();
} }
// No need to go to the download link.
function externalDownLoadLink() {
return ['quickref.me'].indexOf(location.hostname) > -1;
}
// Toggle video playback when the window is hidden. // Toggle video playback when the window is hidden.
function toggleVideoPlayback(pause) { function toggleVideoPlayback(pause) {