Skip to content

Commit

Permalink
remove search module and add baidu analyze
Browse files Browse the repository at this point in the history
  • Loading branch information
TrumanDu committed Dec 17, 2024
1 parent ab3ce4b commit 90a9fb5
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 154 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
},
"dependencies": {
"@electron/notarize": "^2.3.2",
"@nostar/baidu-analytics-electron": "^1.0.7",
"antd": "^5.10.1",
"axios": "^1.6.2",
"electron-debug": "^3.2.0",
Expand Down
22 changes: 6 additions & 16 deletions src/main/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,18 @@ class API {
this.pluginManager = new PluginManager(initCheck, this.setting);
}

public listen(mainWindow: BrowserWindow) {
public listen() {
ipcMain.on('trigger', async (event, arg) => {
console.log(arg);
try {
const window = arg.winId ? BrowserWindow.fromId(arg.winId) : mainWindow;
const data = await this[arg.type](arg, window, event);
const data = await this[arg.type](arg, event);
event.returnValue = data;
} catch (error) {
log.error(error);
}
});
}

public hideMainWindow(_arg: any, window: BrowserWindow) {
window?.hide();
}

public showMainWindow(_arg: any, window: BrowserWindow) {
window.show();
}

public listPlugins() {
return this.pluginManager.listPlugin();
}
Expand All @@ -52,8 +43,7 @@ class API {
return this.pluginManager.reloadPlugins();
}

public openPlugin(arg: any, window: BrowserWindow) {
this.hideMainWindow(arg, window);
public openPlugin(arg: any) {
const pluginObj = this.pluginManager.getPlugin(arg.data);
if (pluginObj.mode && pluginObj.mode === 'single') {
const name = arg.data;
Expand All @@ -72,7 +62,7 @@ class API {
}
}

public removePlugin(_arg: any, window: BrowserWindow) {
public removePlugin(_arg: any) {
this.pluginManager.removePlugin(_arg.data);
return this.listPlugins();
}
Expand All @@ -89,7 +79,7 @@ class API {
return this.pluginManager.getStoreAppList();
}

public async installPlugin(arg: any, _window: BrowserWindow, event) {
public async installPlugin(arg: any, event) {
const data = await this.pluginManager.installPlugin(arg.data);
const response = {
operator: 'installPlugin',
Expand All @@ -105,7 +95,7 @@ class API {
return this.setting.getSetting();
}

public saveSettingByKey(arg: any, window: BrowserWindow) {
public saveSettingByKey(arg: any) {
const { data } = arg;
this.setting.updateByKey(data.key, data.value);
}
Expand Down
54 changes: 54 additions & 0 deletions src/main/dashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import path from 'path';
import { app, BrowserWindow, shell } from 'electron';
import { resolveHtmlPath, getAssetPath } from './util';

const createDashboardWindow = async () => {
const newDashboardWindow = new BrowserWindow({
show: false,
center: true,
autoHideMenuBar: true,
width: 1560,
height: 900,
icon: getAssetPath('icon.png'),
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
webSecurity: false,
navigateOnDragDrop: true,
preload: app.isPackaged
? path.join(__dirname, 'preload.js')
: path.join(__dirname, '../../.erb/dll/preload.js'),
},
});

if (process.platform === 'darwin') {
app.dock.setIcon(getAssetPath('icon.png'));
app.dock.bounce();
}

newDashboardWindow.loadURL(resolveHtmlPath('dashboard.html'));
newDashboardWindow.on('close', (event) => {
newDashboardWindow.hide();
event.preventDefault();
});
// 当窗口准备好时,最大化窗口
newDashboardWindow.webContents.on('did-finish-load', () => {
newDashboardWindow.show();
});
newDashboardWindow.webContents.setWindowOpenHandler(
(data: { url: string }) => {
shell.openExternal(data.url);
return { action: 'deny' };
},
);
newDashboardWindow.webContents.on('will-navigate', (event, url) => {
// 判断链接是否为本地文件
if (!url.startsWith('file://')) {
event.preventDefault();
shell.openExternal(url); // 打开默认浏览器并跳转到该链接
}
});
return newDashboardWindow;
};

export default createDashboardWindow;
145 changes: 12 additions & 133 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,24 @@
* When running `npm run build` or `npm run build:main`, this file is compiled to
* `./src/main.js` using webpack. This gives us some performance wins.
*/
import path from 'path';
import fixPath from 'fix-path';
import {
app,
BrowserWindow,
globalShortcut,
shell,
ipcMain,
Menu,
} from 'electron';
import { app, BrowserWindow, globalShortcut, ipcMain } from 'electron';

import log from 'electron-log';
import { resolveHtmlPath, getAssetPath, getAppDir } from './util';
import { getAppDir } from './util';
import createTray from './tray';
import API from './api';
import AppUpdater from './app_updater';
import InitCheck from './init_check';
import createDashboardWindow from './dashboard';

const { baiduAnalyticsMain } = require('@nostar/baidu-analytics-electron');
// IMPORTANT: to fix file save problem in excalidraw: The request is not allowed by the user agent or the platform in the current context
app.commandLine.appendSwitch('enable-experimental-web-platform-features');
app.setAppUserModelId('top.trumandu.Toolkit');

fixPath();

let mainWindow: BrowserWindow | null = null;
let dashboardWindow: BrowserWindow | null = null;

if (process.env.NODE_ENV === 'production') {
Expand All @@ -47,6 +41,8 @@ if (isDebug) {
}
const initCheck = new InitCheck();

baiduAnalyticsMain(ipcMain);

const installExtensions = async () => {
const installer = require('electron-devtools-installer');
const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
Expand All @@ -59,120 +55,19 @@ const installExtensions = async () => {
)
.catch(console.log);
};
const createDashboardWindow = async () => {
const newDashboardWindow = new BrowserWindow({
show: false,
center: true,
autoHideMenuBar: true,
width: 1560,
height: 900,
icon: getAssetPath('icon.png'),
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
webSecurity: false,
navigateOnDragDrop: true,
preload: app.isPackaged
? path.join(__dirname, 'preload.js')
: path.join(__dirname, '../../.erb/dll/preload.js'),
},
});

if (process.platform === 'darwin') {
app.dock.setIcon(getAssetPath('icon.png'));
app.dock.bounce();
}

newDashboardWindow.loadURL(resolveHtmlPath('dashboard.html'));
newDashboardWindow.on('close', (event) => {
newDashboardWindow.hide();
event.preventDefault();
});
// 当窗口准备好时,最大化窗口
newDashboardWindow.webContents.on('did-finish-load', () => {
newDashboardWindow.show();
});
newDashboardWindow.webContents.setWindowOpenHandler(
(data: { url: string }) => {
shell.openExternal(data.url);
return { action: 'deny' };
},
);
newDashboardWindow.webContents.on('will-navigate', (event, url) => {
// 判断链接是否为本地文件
if (!url.startsWith('file://')) {
event.preventDefault();
shell.openExternal(url); // 打开默认浏览器并跳转到该链接
}
});
return newDashboardWindow;
};

const createWindow = async () => {
if (isDebug) {
// await installExtensions();
await installExtensions();
}

mainWindow = new BrowserWindow({
show: false,
frame: false,
titleBarStyle: 'hidden',
center: true,
transparent: true,
width: 678,
height: 680,
icon: getAssetPath('icon.png'),
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
webSecurity: false,
preload: app.isPackaged
? path.join(__dirname, 'preload.js')
: path.join(__dirname, '../../.erb/dll/preload.js'),
},
});
mainWindow.setPosition(
mainWindow.getPosition()[0],
mainWindow.getPosition()[1] / 2 + 200,
);

mainWindow.loadURL(resolveHtmlPath('main.html'));

mainWindow.on('ready-to-show', () => {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined');
}
});
// 当窗口完成加载后,自动获得焦点
mainWindow.webContents.on('did-finish-load', () => {
mainWindow?.focus();
});
if (!isDebug) {
mainWindow.on('blur', () => {
mainWindow?.hide();
});
}
mainWindow.on('closed', () => {
mainWindow = null;
});
mainWindow.setMenu(null);

// Open urls in the user's browser
mainWindow.webContents.setWindowOpenHandler((edata) => {
shell.openExternal(edata.url);
return { action: 'deny' };
});

// Remove this if your app does not use auto updates
// eslint-disable-next-line
// eslint-disable-next-line no-new
new AppUpdater();
dashboardWindow = await createDashboardWindow();
const api = new API(dashboardWindow, initCheck);
api.listen(mainWindow);
api.listen();
// 创建系统托盘图标
createTray(mainWindow, dashboardWindow, api);
mainWindow.setSkipTaskbar(true);
// mainWindow.setIgnoreMouseEvents(true);
createTray(dashboardWindow, api);
};

/**
Expand All @@ -192,18 +87,6 @@ app
.then(() => {
createWindow();
// 注册全局快捷键
if (
!globalShortcut.register('CmdOrCtrl+Alt+A', () => {
// 在此处执行快捷键触发的操作
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
}
})
) {
console.log('main shortcut register failed.');
}

if (
!globalShortcut.register('CmdOrCtrl+Alt+O', () => {
if (dashboardWindow) {
Expand All @@ -215,18 +98,14 @@ app
}

// 检测快捷键注册状态
log.info(
'main shortcut register:',
globalShortcut.isRegistered('CmdOrCtrl+Alt+A'),
);
log.info(
'dashboard shortcut register:',
globalShortcut.isRegistered('CmdOrCtrl+Alt+O'),
);
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
if (dashboardWindow === null) {
createWindow();
} else if (dashboardWindow?.isVisible()) {
dashboardWindow.hide();
Expand Down
6 changes: 1 addition & 5 deletions src/main/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import pkg from '../../package.json';
import { getAppDir, getAssetPath } from './util';
import API from './api';

function createTray(
window: BrowserWindow,
dashboard: BrowserWindow,
api: API,
): Promise<Tray> {
function createTray(dashboard: BrowserWindow, api: API): Promise<Tray> {
return new Promise((resolve) => {
const iconPath = getAssetPath('icons/16x16.png');

Expand Down
33 changes: 33 additions & 0 deletions src/renderer/Dashboard/baiduAnalutics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable no-underscore-dangle */
const baiduAnalyticsRenderer = (siteId: string, initCallback: any) => {
if (!(siteId && typeof siteId === 'string')) {
throw new TypeError(`require siteId`);
}

// 添加默认行为避免报错
window._hmt = window._hmt || [];

window.electron.ipcRenderer.on(
'baidu-analytics-electron-reply',
(_, { text }) => {
window._hmt = window._hmt || [];

if (initCallback && typeof initCallback === 'function') {
initCallback(window._hmt);
}

const hm = document.createElement('script');
hm.text = text;

const head = document.getElementsByTagName('head')[0];
head.appendChild(hm);
},
);

window.electron.ipcRenderer.sendMessage(
'baidu-analytics-electron-message',
siteId,
);
};

export default baiduAnalyticsRenderer;
Loading

0 comments on commit 90a9fb5

Please sign in to comment.