diff --git a/.cspell/custom-words.txt b/.cspell/custom-words.txt index fe63b9ac..eda3a43a 100644 --- a/.cspell/custom-words.txt +++ b/.cspell/custom-words.txt @@ -2,6 +2,7 @@ acodec adblocker ahooks antd +aplus behaviour bili bilibili @@ -15,6 +16,7 @@ conventionalcommits datetime DDTHH execa +idtype immer inversify KHTML diff --git a/.env.development b/.env.development index 229bd70e..4f66209f 100644 --- a/.env.development +++ b/.env.development @@ -1,5 +1,4 @@ APP_TD_APPID= -APP_CLARITY_APPID= GH_TOKEN= LOAD_DEVTOOLS= diff --git a/.env.production b/.env.production index 4a1edef8..99737b75 100644 --- a/.env.production +++ b/.env.production @@ -1,5 +1,4 @@ APP_TD_APPID= -APP_CLARITY_APPID= GH_TOKEN= DEBUG_PLUGINS=false diff --git a/packages/main/package.json b/packages/main/package.json index 9fdcc7d8..6373f308 100644 --- a/packages/main/package.json +++ b/packages/main/package.json @@ -66,7 +66,9 @@ "lint-staged": "^15.2.5", "lodash": "^4.17.21", "mime-types": "^2.1.35", + "nanoid": "^5.0.7", "node-fetch": "^3.3.2", + "node-machine-id": "^1.1.12", "node-pty": "^1.0.0", "reflect-metadata": "^0.2.2", "strip-ansi": "^7.1.0", diff --git a/packages/main/src/controller/HomeController.ts b/packages/main/src/controller/HomeController.ts index 734efc9f..ff06767c 100644 --- a/packages/main/src/controller/HomeController.ts +++ b/packages/main/src/controller/HomeController.ts @@ -24,6 +24,8 @@ import WebviewService from "../services/WebviewService.ts"; import FavoriteRepository from "../repository/FavoriteRepository.ts"; import VideoRepository from "../repository/VideoRepository.ts"; import ConversionRepository from "../repository/ConversionRepository.ts"; +import { machineId } from "node-machine-id"; +import { nanoid } from "nanoid"; @injectable() export default class HomeController implements Controller { @@ -271,4 +273,21 @@ export default class HomeController implements Controller { } return ""; } + + @handle("get-machine-id") + async getMachineId() { + try { + const id = this.store.get("machineId"); + if (id) return id; + const newId = await machineId(); + this.store.set("machineId", newId); + return newId; + } catch (e) { + const id = this.store.get("machineId"); + if (id) return id; + const newId = nanoid(); + this.store.set("machineId", newId); + return newId; + } + } } diff --git a/packages/main/src/main.d.ts b/packages/main/src/main.d.ts index 848cce52..e20b34d3 100644 --- a/packages/main/src/main.d.ts +++ b/packages/main/src/main.d.ts @@ -52,6 +52,8 @@ declare interface AppStore { showTerminal: boolean; // 隐私模式 privacy: boolean; + // 机器id + machineId: string; } declare interface BrowserStore { diff --git a/packages/main/src/preload.ts b/packages/main/src/preload.ts index c59494ab..0f6bca7e 100644 --- a/packages/main/src/preload.ts +++ b/packages/main/src/preload.ts @@ -169,6 +169,9 @@ const electronApi = { deleteConversion(id: number): Promise { return ipcRenderer.invoke("delete-conversion", id); }, + getMachineId(): Promise { + return ipcRenderer.invoke("get-machine-id"); + }, }; contextBridge.exposeInMainWorld(apiKey, electronApi); diff --git a/packages/main/src/vendor/ElectronStore.ts b/packages/main/src/vendor/ElectronStore.ts index df016662..52eb7ba4 100644 --- a/packages/main/src/vendor/ElectronStore.ts +++ b/packages/main/src/vendor/ElectronStore.ts @@ -28,6 +28,7 @@ export default class StoreService extends Store implements Vendor { language: AppLanguage.System, showTerminal: false, privacy: false, + machineId: "", }, }); } diff --git a/packages/renderer/src/App.tsx b/packages/renderer/src/App.tsx index 5d3330a2..34223bfe 100644 --- a/packages/renderer/src/App.tsx +++ b/packages/renderer/src/App.tsx @@ -9,6 +9,8 @@ import "./App.scss"; import useElectron from "./hooks/electron"; import Loading from "./components/Loading"; import { DownloadFilter } from "./types"; +import { tdApp } from "./utils"; +import { useAsyncEffect } from "ahooks"; const AppLayout = lazy(() => import("./layout/App")); const HomePage = lazy(() => import("./pages/HomePage")); @@ -23,7 +25,7 @@ function getAlgorithm(appTheme: "dark" | "light") { const App: FC = () => { const dispatch = useDispatch(); const [appTheme, setAppTheme] = React.useState<"dark" | "light">("light"); - const { addIpcListener, removeIpcListener } = useElectron(); + const { addIpcListener, removeIpcListener, getMachineId } = useElectron(); const themeChange = (event: MediaQueryListEvent) => { if (event.matches) { @@ -52,6 +54,11 @@ const App: FC = () => { }; }, []); + useAsyncEffect(async () => { + const deviceId = await getMachineId(); + tdApp.onEvent("页面加载", { deviceId }); + }, []); + useEffect(() => { const isDarkTheme = matchMedia("(prefers-color-scheme: dark)"); isDarkTheme.addEventListener("change", themeChange); diff --git a/packages/renderer/src/main.tsx b/packages/renderer/src/main.tsx index a633bcc9..29ecc9b2 100644 --- a/packages/renderer/src/main.tsx +++ b/packages/renderer/src/main.tsx @@ -5,18 +5,17 @@ import { Provider } from "react-redux"; import store from "./store"; import dayjs from "dayjs"; import "dayjs/locale/zh-cn"; -import { tdApp, initClarity } from "./utils"; +import { tdApp } from "./utils"; import App from "./App"; import "./i18n"; dayjs.locale("zh-cn"); tdApp.init(); -initClarity(); createRoot(document.getElementById("root") as HTMLElement).render( - , + ); diff --git a/packages/renderer/src/utils/clarity.ts b/packages/renderer/src/utils/clarity.ts deleted file mode 100644 index 4da79c56..00000000 --- a/packages/renderer/src/utils/clarity.ts +++ /dev/null @@ -1,14 +0,0 @@ -/* eslint-disable */ -// @ts-nocheck -// prettier-ignore -export function initClarity() { - try { - (function(c,l,a,r,i,t,y){ - c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; - t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; - y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); - })(window, document, "clarity", "script", import.meta.env.APP_CLARITY_APPID); - } catch (e) { - // empty - } -} diff --git a/packages/renderer/src/utils/index.ts b/packages/renderer/src/utils/index.ts index df06e3d2..35dd7083 100644 --- a/packages/renderer/src/utils/index.ts +++ b/packages/renderer/src/utils/index.ts @@ -3,7 +3,6 @@ import { isUrl } from "./url"; export { http } from "./http"; export { tdApp } from "./tdapp"; -export { initClarity } from "./clarity"; export const requestImage = (url: string, timeout = 1000): Promise => { return new Promise((resolve, reject) => { diff --git a/packages/renderer/src/utils/tdapp.ts b/packages/renderer/src/utils/tdapp.ts index b7c1fe0e..5345b01d 100644 --- a/packages/renderer/src/utils/tdapp.ts +++ b/packages/renderer/src/utils/tdapp.ts @@ -17,7 +17,7 @@ class TDEvent { headElement.appendChild(script); } - private onEvent(eventId: string, mapKv: Record = {}) { + onEvent(eventId: string, mapKv: Record = {}) { try { window.TDAPP?.onEvent(eventId, "", mapKv); } catch (e) { diff --git a/packages/renderer/src/vite-env.d.ts b/packages/renderer/src/vite-env.d.ts index 11b4a64e..4b83ba8c 100644 --- a/packages/renderer/src/vite-env.d.ts +++ b/packages/renderer/src/vite-env.d.ts @@ -8,10 +8,9 @@ declare global { onEvent: ( eventId: string, label: "", - mapKv: Record, + mapKv: Record ) => void; }; - clarity?: any; } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fd07517e..803ebe78 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -121,9 +121,15 @@ importers: mime-types: specifier: ^2.1.35 version: 2.1.35 + nanoid: + specifier: ^5.0.7 + version: 5.0.7 node-fetch: specifier: ^3.3.2 version: 3.3.2 + node-machine-id: + specifier: ^1.1.12 + version: 1.1.12 node-pty: specifier: ^1.0.0 version: 1.0.0 @@ -4206,6 +4212,9 @@ packages: engines: {node: ^12.13 || ^14.13 || >=16} hasBin: true + node-machine-id@1.1.12: + resolution: {integrity: sha512-QNABxbrPa3qEIfrE6GOJ7BYIuignnJw7iQ2YPbc3Nla1HzRJjXzZOiikfF8m7eAMfichLt3M4VgLOetqgDmgGQ==} + node-pty@1.0.0: resolution: {integrity: sha512-wtBMWWS7dFZm/VgqElrTvtfMq4GzJ6+edFI0Y0zyzygUSZMgZdraDUMUhCIvkjhJjme15qWmbyJbtAx4ot4uZA==} @@ -7813,7 +7822,7 @@ snapshots: app-builder-bin@4.0.0: {} - app-builder-lib@24.13.3(dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)): + app-builder-lib@24.13.3(dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)): dependencies: '@develar/schema-utils': 2.6.5 '@electron/notarize': 2.2.1 @@ -7827,7 +7836,7 @@ snapshots: builder-util-runtime: 9.2.4 chromium-pickle-js: 0.2.0 debug: 4.3.4 - dmg-builder: 24.13.3(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)) + dmg-builder: 24.13.3(electron-builder-squirrel-windows@24.13.3) ejs: 3.1.10 electron-builder-squirrel-windows: 24.13.3(dmg-builder@24.13.3) electron-publish: 24.13.1 @@ -8619,9 +8628,9 @@ snapshots: dependencies: path-type: 4.0.0 - dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)): + dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3): dependencies: - app-builder-lib: 24.13.3(dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)) + app-builder-lib: 24.13.3(dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)) builder-util: 24.13.1 builder-util-runtime: 9.2.4 fs-extra: 10.1.0 @@ -8694,7 +8703,7 @@ snapshots: electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3): dependencies: - app-builder-lib: 24.13.3(dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)) + app-builder-lib: 24.13.3(dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)) archiver: 5.3.2 builder-util: 24.13.1 fs-extra: 10.1.0 @@ -8704,11 +8713,11 @@ snapshots: electron-builder@24.13.3(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)): dependencies: - app-builder-lib: 24.13.3(dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)))(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)) + app-builder-lib: 24.13.3(dmg-builder@24.13.3(electron-builder-squirrel-windows@24.13.3))(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)) builder-util: 24.13.1 builder-util-runtime: 9.2.4 chalk: 4.1.2 - dmg-builder: 24.13.3(electron-builder-squirrel-windows@24.13.3(dmg-builder@24.13.3)) + dmg-builder: 24.13.3(electron-builder-squirrel-windows@24.13.3) fs-extra: 10.1.0 is-ci: 3.0.1 lazy-val: 1.0.5 @@ -10137,6 +10146,8 @@ snapshots: - bluebird - supports-color + node-machine-id@1.1.12: {} + node-pty@1.0.0: dependencies: nan: 2.19.0