feat: 增加透过 blob 下载文件的支持
This commit is contained in:
8
bin/builders/common.ts
vendored
8
bin/builders/common.ts
vendored
@@ -5,6 +5,7 @@ import fs from 'fs/promises';
|
||||
import fs2 from 'fs-extra';
|
||||
import { npmDirectory } from '@/utils/dir.js';
|
||||
import logger from '@/options/logger.js';
|
||||
import URL from 'node:url';
|
||||
|
||||
|
||||
export async function promptText(message: string, initial?: string) {
|
||||
@@ -17,6 +18,10 @@ export async function promptText(message: string, initial?: string) {
|
||||
return response.content;
|
||||
}
|
||||
|
||||
function setSecurityConfigWithUrl(tauriConfig: any, url: string) {
|
||||
const {hostname} = URL.parse(url);
|
||||
tauriConfig.tauri.security.dangerousRemoteDomainIpcAccess[0].domain = hostname;
|
||||
}
|
||||
|
||||
export async function mergeTauriConfig(
|
||||
url: string,
|
||||
@@ -267,6 +272,9 @@ export async function mergeTauriConfig(
|
||||
}
|
||||
}
|
||||
|
||||
// 设置安全调用 window.__TAURI__ 的安全域名为设置的应用域名
|
||||
setSecurityConfigWithUrl(tauriConf, url);
|
||||
|
||||
// 保存配置文件
|
||||
let configPath = "";
|
||||
switch (process.platform) {
|
||||
|
||||
28
dist/cli.js
vendored
28
dist/cli.js
vendored
@@ -9,6 +9,7 @@ import path from 'path';
|
||||
import fs$1 from 'fs/promises';
|
||||
import fs2 from 'fs-extra';
|
||||
import chalk from 'chalk';
|
||||
import URL from 'node:url';
|
||||
import crypto from 'crypto';
|
||||
import axios from 'axios';
|
||||
import { fileTypeFromBuffer } from 'file-type';
|
||||
@@ -1639,6 +1640,10 @@ function promptText(message, initial) {
|
||||
return response.content;
|
||||
});
|
||||
}
|
||||
function setSecurityConfigWithUrl(tauriConfig, url) {
|
||||
const { hostname } = URL.parse(url);
|
||||
tauriConfig.tauri.security.dangerousRemoteDomainIpcAccess[0].domain = hostname;
|
||||
}
|
||||
function mergeTauriConfig(url, options, tauriConf) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const { width, height, fullscreen, transparent, resizable, userAgent, showMenu, showSystemTray, systemTrayIcon, iterCopyFile, identifier, name, } = options;
|
||||
@@ -1862,6 +1867,8 @@ function mergeTauriConfig(url, options, tauriConf) {
|
||||
tauriConf.tauri.systemTray.iconPath = "png/icon_512.png";
|
||||
}
|
||||
}
|
||||
// 设置安全调用 window.__TAURI__ 的安全域名为设置的应用域名
|
||||
setSecurityConfigWithUrl(tauriConf, url);
|
||||
// 保存配置文件
|
||||
let configPath = "";
|
||||
switch (process.platform) {
|
||||
@@ -2102,17 +2109,32 @@ function checkRustInstalled() {
|
||||
|
||||
var tauri$3 = {
|
||||
security: {
|
||||
csp: null
|
||||
csp: null,
|
||||
dangerousRemoteDomainIpcAccess: [
|
||||
{
|
||||
domain: "weread.qq.com",
|
||||
windows: [
|
||||
"pake"
|
||||
],
|
||||
enableTauriAPI: true
|
||||
}
|
||||
]
|
||||
},
|
||||
updater: {
|
||||
active: false
|
||||
},
|
||||
systemTray: {
|
||||
iconPath: "png/weread_512.png",
|
||||
iconPath: "png/icon_512.png",
|
||||
iconAsTemplate: true
|
||||
},
|
||||
allowlist: {
|
||||
all: true
|
||||
all: true,
|
||||
fs: {
|
||||
all: true,
|
||||
scope: [
|
||||
"$DOWNLOAD/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
||||
var build = {
|
||||
|
||||
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