✨ File download function optimization.
This commit is contained in:
356
src-tauri/src/inject/event.js
vendored
356
src-tauri/src/inject/event.js
vendored
@@ -1,207 +1,223 @@
|
|||||||
const shortcuts = {
|
const shortcuts = {
|
||||||
ArrowUp: () => scrollTo(0, 0),
|
ArrowUp: () => scrollTo(0, 0),
|
||||||
ArrowDown: () => scrollTo(0, document.body.scrollHeight),
|
ArrowDown: () => scrollTo(0, document.body.scrollHeight),
|
||||||
// Don't use command + ArrowLeft or command + ArrowRight
|
// Don't use command + ArrowLeft or command + ArrowRight
|
||||||
// When editing text in page, it causes unintended page navigation.
|
// When editing text in page, it causes unintended page navigation.
|
||||||
// ArrowLeft: () => window.history.back(),
|
// ArrowLeft: () => window.history.back(),
|
||||||
// ArrowRight: () => window.history.forward(),
|
// ArrowRight: () => window.history.forward(),
|
||||||
'[': () => window.history.back(),
|
'[': () => window.history.back(),
|
||||||
']': () => window.history.forward(),
|
']': () => window.history.forward(),
|
||||||
r: () => window.location.reload(),
|
r: () => window.location.reload(),
|
||||||
'-': () => zoomOut(),
|
'-': () => zoomOut(),
|
||||||
'=': () => zoomIn(),
|
'=': () => zoomIn(),
|
||||||
'+': () => zoomIn(),
|
'+': () => zoomIn(),
|
||||||
0: () => setZoom('100%'),
|
0: () => setZoom('100%'),
|
||||||
};
|
};
|
||||||
|
|
||||||
function setZoom(zoom) {
|
function setZoom(zoom) {
|
||||||
const html = document.getElementsByTagName('html')[0];
|
const html = document.getElementsByTagName('html')[0];
|
||||||
html.style.zoom = zoom;
|
html.style.zoom = zoom;
|
||||||
window.localStorage.setItem('htmlZoom', zoom);
|
window.localStorage.setItem('htmlZoom', zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
function zoomCommon(zoomChange) {
|
function zoomCommon(zoomChange) {
|
||||||
const currentZoom = window.localStorage.getItem('htmlZoom') || '100%';
|
const currentZoom = window.localStorage.getItem('htmlZoom') || '100%';
|
||||||
setZoom(zoomChange(currentZoom));
|
setZoom(zoomChange(currentZoom));
|
||||||
}
|
}
|
||||||
|
|
||||||
function zoomIn() {
|
function zoomIn() {
|
||||||
zoomCommon((currentZoom) => `${Math.min(parseInt(currentZoom) + 10, 200)}%`);
|
zoomCommon((currentZoom) => `${Math.min(parseInt(currentZoom) + 10, 200)}%`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function zoomOut() {
|
function zoomOut() {
|
||||||
zoomCommon((currentZoom) => `${Math.max(parseInt(currentZoom) - 10, 30)}%`);
|
zoomCommon((currentZoom) => `${Math.max(parseInt(currentZoom) - 10, 30)}%`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleShortcut(event) {
|
function handleShortcut(event) {
|
||||||
if (shortcuts[event.key]) {
|
if (shortcuts[event.key]) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
shortcuts[event.key]();
|
shortcuts[event.key]();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//这里参考 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) {
|
|
||||||
const identifier = uid();
|
function transformCallback(callback = () => {
|
||||||
const prop = `_${identifier}`;
|
}, once = false) {
|
||||||
Object.defineProperty(window, prop, {
|
const identifier = uid();
|
||||||
value: (result) => {
|
const prop = `_${identifier}`;
|
||||||
if (once) {
|
Object.defineProperty(window, prop, {
|
||||||
Reflect.deleteProperty(window, prop);
|
value: (result) => {
|
||||||
}
|
if (once) {
|
||||||
return callback(result);
|
Reflect.deleteProperty(window, prop);
|
||||||
},
|
}
|
||||||
writable: false,
|
return callback(result);
|
||||||
configurable: true,
|
},
|
||||||
});
|
writable: false,
|
||||||
return identifier;
|
configurable: true,
|
||||||
}
|
|
||||||
async function invoke(cmd, args) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (!window.__TAURI_POST_MESSAGE__)
|
|
||||||
reject('__TAURI_POST_MESSAGE__ does not exist~');
|
|
||||||
const callback = transformCallback((e) => {
|
|
||||||
resolve(e);
|
|
||||||
Reflect.deleteProperty(window, `_${error}`);
|
|
||||||
}, true);
|
|
||||||
const error = transformCallback((e) => {
|
|
||||||
reject(e);
|
|
||||||
Reflect.deleteProperty(window, `_${callback}`);
|
|
||||||
}, true);
|
|
||||||
window.__TAURI_POST_MESSAGE__({
|
|
||||||
cmd,
|
|
||||||
callback,
|
|
||||||
error,
|
|
||||||
...args,
|
|
||||||
});
|
});
|
||||||
});
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
async function invoke(cmd, args) {
|
||||||
const topDom = document.createElement('div');
|
return new Promise((resolve, reject) => {
|
||||||
topDom.id = 'pack-top-dom';
|
if (!window.__TAURI_POST_MESSAGE__)
|
||||||
document.body.appendChild(topDom);
|
reject('__TAURI_POST_MESSAGE__ does not exist~');
|
||||||
const domEl = document.getElementById('pack-top-dom');
|
const callback = transformCallback((e) => {
|
||||||
|
resolve(e);
|
||||||
domEl.addEventListener('mousedown', (e) => {
|
Reflect.deleteProperty(window, `_${error}`);
|
||||||
e.preventDefault();
|
}, true);
|
||||||
if (e.buttons === 1 && e.detail !== 2) {
|
const error = transformCallback((e) => {
|
||||||
invoke('drag_window');
|
reject(e);
|
||||||
}
|
Reflect.deleteProperty(window, `_${callback}`);
|
||||||
});
|
}, true);
|
||||||
|
window.__TAURI_POST_MESSAGE__({
|
||||||
domEl.addEventListener('touchstart', () => {
|
cmd,
|
||||||
invoke('drag_window');
|
callback,
|
||||||
});
|
error,
|
||||||
|
...args,
|
||||||
domEl.addEventListener('dblclick', () => {
|
|
||||||
invoke('fullscreen');
|
|
||||||
});
|
|
||||||
|
|
||||||
document.addEventListener('keyup', (event) => {
|
|
||||||
if (/windows|linux/i.test(navigator.userAgent) && event.ctrlKey) {
|
|
||||||
handleShortcut(event);
|
|
||||||
}
|
|
||||||
if (/macintosh|mac os x/i.test(navigator.userAgent) && event.metaKey) {
|
|
||||||
handleShortcut(event);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
document.addEventListener('click', (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;
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
&& !externalDownLoadLink()
|
|
||||||
) {
|
|
||||||
e.preventDefault();
|
|
||||||
invoke('download_file', {
|
|
||||||
params: {
|
|
||||||
url: absoluteUrl,
|
|
||||||
filename,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Rewrite the window.open function.
|
|
||||||
const originalWindowOpen = window.open;
|
|
||||||
window.open = function (url, name, specs) {
|
|
||||||
// Apple login and google login
|
|
||||||
if (name === 'AppleAuthentication') {
|
|
||||||
//do nothing
|
|
||||||
} else if (specs.includes('height=') || specs.includes('width=')) {
|
|
||||||
location.href = url;
|
|
||||||
} else {
|
|
||||||
const baseUrl = window.location.origin + window.location.pathname;
|
|
||||||
const hrefUrl = new URL(url, baseUrl);
|
|
||||||
invoke('open_browser', { url: hrefUrl.href });
|
|
||||||
}
|
|
||||||
// Call the original window.open function to maintain its normal functionality.
|
|
||||||
return originalWindowOpen.call(window, url, name, specs);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Set the default zoom, There are problems with Loop without using try-catch.
|
|
||||||
try {
|
|
||||||
setDefaultZoom();
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function setDefaultZoom() {
|
|
||||||
const htmlZoom = window.localStorage.getItem('htmlZoom');
|
|
||||||
if (htmlZoom) {
|
|
||||||
setZoom(htmlZoom);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilenameFromUrl(url) {
|
// Judgment of file download.
|
||||||
const urlPath = new URL(url).pathname;
|
function isDownloadLink(url) {
|
||||||
const filename = urlPath.substring(urlPath.lastIndexOf('/') + 1);
|
const fileExtensions = [
|
||||||
return filename;
|
'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',
|
||||||
function removeUrlParameters(url) {
|
'swf', 'tar', 'tif', 'tiff', 'ts', 'txt', 'wav', 'webm', 'webp', 'wma', 'wmv', 'xls', 'xlsx', 'xml', 'zip'
|
||||||
const parsedUrl = new URL(url);
|
];
|
||||||
parsedUrl.search = '';
|
const downloadLinkPattern = new RegExp(`\\.(${fileExtensions.join('|')})$`, 'i');
|
||||||
return parsedUrl.toString();
|
return downloadLinkPattern.test(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// No need to go to the download link.
|
// No need to go to the download link.
|
||||||
function externalDownLoadLink() {
|
function externalDownLoadLink() {
|
||||||
return ['quickref.me'].indexOf(location.hostname) > -1;
|
return ['quickref.me'].indexOf(location.hostname) > -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
const topDom = document.createElement('div');
|
||||||
|
topDom.id = 'pack-top-dom';
|
||||||
|
document.body.appendChild(topDom);
|
||||||
|
const domEl = document.getElementById('pack-top-dom');
|
||||||
|
|
||||||
|
domEl.addEventListener('mousedown', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (e.buttons === 1 && e.detail !== 2) {
|
||||||
|
invoke('drag_window');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
domEl.addEventListener('touchstart', () => {
|
||||||
|
invoke('drag_window');
|
||||||
|
});
|
||||||
|
|
||||||
|
domEl.addEventListener('dblclick', () => {
|
||||||
|
invoke('fullscreen');
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('keyup', (event) => {
|
||||||
|
if (/windows|linux/i.test(navigator.userAgent) && event.ctrlKey) {
|
||||||
|
handleShortcut(event);
|
||||||
|
}
|
||||||
|
if (/macintosh|mac os x/i.test(navigator.userAgent) && event.metaKey) {
|
||||||
|
handleShortcut(event);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('click', (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;
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Rewrite the window.open function.
|
||||||
|
const originalWindowOpen = window.open;
|
||||||
|
window.open = function (url, name, specs) {
|
||||||
|
// Apple login and google login
|
||||||
|
if (name === 'AppleAuthentication') {
|
||||||
|
//do nothing
|
||||||
|
} else if (specs.includes('height=') || specs.includes('width=')) {
|
||||||
|
location.href = url;
|
||||||
|
} else {
|
||||||
|
const baseUrl = window.location.origin + window.location.pathname;
|
||||||
|
const hrefUrl = new URL(url, baseUrl);
|
||||||
|
invoke('open_browser', {url: hrefUrl.href});
|
||||||
|
}
|
||||||
|
// Call the original window.open function to maintain its normal functionality.
|
||||||
|
return originalWindowOpen.call(window, url, name, specs);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Set the default zoom, There are problems with Loop without using try-catch.
|
||||||
|
try {
|
||||||
|
setDefaultZoom();
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function setDefaultZoom() {
|
||||||
|
const htmlZoom = window.localStorage.getItem('htmlZoom');
|
||||||
|
if (htmlZoom) {
|
||||||
|
setZoom(htmlZoom);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFilenameFromUrl(url) {
|
||||||
|
const urlPath = new URL(url).pathname;
|
||||||
|
const filename = urlPath.substring(urlPath.lastIndexOf('/') + 1);
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeUrlParameters(url) {
|
||||||
|
const parsedUrl = new URL(url);
|
||||||
|
parsedUrl.search = '';
|
||||||
|
return parsedUrl.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Toggle video playback when the window is hidden.
|
// Toggle video playback when the window is hidden.
|
||||||
function toggleVideoPlayback(pause) {
|
function toggleVideoPlayback(pause) {
|
||||||
const videos = document.getElementsByTagName('video');
|
const videos = document.getElementsByTagName('video');
|
||||||
for (const video of videos) {
|
for (const video of videos) {
|
||||||
if (pause) {
|
if (pause) {
|
||||||
video.pause();
|
video.pause();
|
||||||
} else {
|
} else {
|
||||||
video.play();
|
video.play();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user