2023-12-07 14:26:08 +11:00

80 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os from 'os';
import path from 'path';
import { ipcMain, dialog } from 'electron';
import log from 'electron-log';
import { throttle } from 'lodash';
import { startServer } from './proxyServer';
import { installCert, checkCertInstalled } from './cert';
import { downloadFile, deleteFiles } from './utils';
let win;
export default function initIPC() {
ipcMain.handle('invoke_初始化信息', async (event, arg) => {
return checkCertInstalled();
});
ipcMain.handle('invoke_开始初始化', (event, arg) => {
return installCert(false);
});
ipcMain.handle('invoke_启动服务', async (event, arg) => {
let cachePath;
let pattern;
if (process.platform === 'darwin') {
cachePath = 'Library/Containers/com.tencent.xinWeChat/Data/.wxapplet/web/profiles/';
pattern = 'multitab**/Cache/Cache_Data';
} else {
cachePath = 'AppData/Roaming/Tencent/WeChat/radium/web/profiles/';
pattern = 'multitab**/Cache/Cache_Data/**';
}
cachePath = path.join(os.homedir(), cachePath);
console.log('cwd:', cachePath);
// delete cached polyfillxxxx.js files first (the proxy.intercept to enforce no-cache request header not working!)
// deleteFiles(cachePath, [/.*LOCK/], /zn.POLYFILL/);
// delete Cache_Data dirs, works well in macOS
// in Windows we need to run this tool before wechat
// otherwise the cache can't be cleared thoroughly due to file locks and the tool can't capture videos later!
process.platform === 'darwin' && await deleteFiles(cachePath, pattern);
return startServer({
win: win,
setProxyErrorCallback: err => {
console.log('开启代理失败', err);
},
});
});
ipcMain.handle('invoke_选择下载位置', async (event, arg) => {
const result = dialog.showOpenDialogSync({ title: '保存', properties: ['openDirectory'] });
if (!result?.[0]) {
throw '取消';
}
return result?.[0];
});
ipcMain.handle('invoke_下载视频', async (event, { url, decodeKey, savePath, description }) => {
let re = /(?:^|\s)(?!#\S+)\s*([^#\s]*)/;
let m = description.match(re);
let fileName = m && m.length > 1 ? m[1].replaceAll(/[,]/g, "_") : description;
console.log('description:', description);
console.log("fileName:", fileName);
console.log("url:", url)
console.log("decodeKey:", decodeKey);
return downloadFile(
url,
decodeKey,
// `${savePath}/${Date.now()}.mp4`,
`${savePath}/${fileName}.mp4`,
throttle(value => win?.webContents?.send?.('e_进度变化', value), 1000),
).catch(err => {
console;
});
});
}
export function setWin(w) {
win = w;
}