From efd5626d5aa54676a442be181d9c7fbcd5af43df Mon Sep 17 00:00:00 2001 From: zjwmiao <1723168479@qq.com> Date: Sun, 28 Sep 2025 18:38:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/.vitepress/src/shared/analytics/index.ts | 28 ++++--- app/.vitepress/theme/index.ts | 88 ++++++++------------ 2 files changed, 52 insertions(+), 64 deletions(-) diff --git a/app/.vitepress/src/shared/analytics/index.ts b/app/.vitepress/src/shared/analytics/index.ts index bb4162435..68108609b 100644 --- a/app/.vitepress/src/shared/analytics/index.ts +++ b/app/.vitepress/src/shared/analytics/index.ts @@ -51,23 +51,25 @@ export const installer = ( } }; } - if (isCookieAgreed()) { - enableOA(); - } - startListenCookieSet(() => { + if (typeof window !== 'undefined' && typeof document !== 'undefined') { if (isCookieAgreed()) { - if (!oa?.enabled) enableOA(); + enableOA(); + } + startListenCookieSet(() => { + if (isCookieAgreed()) { + if (!oa?.enabled) enableOA(); + } else { + if (oa?.enabled) disableOA(); + } + }); + startListenHistoryChange(); + if (options.onPageView) { + reportPV(options.onPageView('/', location.pathname)); } else { - if (oa?.enabled) disableOA(); + reportPV(); } - }); - startListenHistoryChange(); - if (options.onPageView) { - reportPV(options.onPageView('/', location.pathname)); - } else { - reportPV(); + reportPerformance(); } - reportPerformance(); app.directive('analytics', vAnalytics); }; diff --git a/app/.vitepress/theme/index.ts b/app/.vitepress/theme/index.ts index fafd092e0..da7f7666c 100644 --- a/app/.vitepress/theme/index.ts +++ b/app/.vitepress/theme/index.ts @@ -34,59 +34,45 @@ export default { global.window = {}; global.__VUE_PROD_DEVTOOLS__ = false; } - if (!import.meta.env.SSR) { - const REGEXP = /^\/(?:zh|en)\/(security\/cve|download|sig)\/?/; - const pathServiceMap = { - 'security/cve': 'cvemanager', - download: 'download', - sig: 'sig', - } as Record; - - app.use(installer, { - appKey: 'openEuler', - request(data) { - reporAnalytics(data) - }, - onPageView(_, to) { - const path = REGEXP.exec(to)?.[1]; - const $service = path && pathServiceMap[path]; - if ($service) return { $service }; - }, - onEnable() { - const hm = document.createElement('script'); - hm.src = 'https://hm.baidu.com/hm.js?ab8d86daab9a8e98cf8faa239aefcd3c'; - hm.classList.add('analytics-script'); - const s = document.getElementsByTagName('HEAD')[0]; - s.appendChild(hm); - }, - onDisable() { - const scripts = document.querySelectorAll('script.analytics-script'); - scripts.forEach((script) => { - script.remove(); + app.use(installer, { + appKey: 'openEuler', + onEnable() { + const hm = document.createElement('script'); + hm.src = 'https://hm.baidu.com/hm.js?ab8d86daab9a8e98cf8faa239aefcd3c'; + hm.classList.add('analytics-script'); + const s = document.getElementsByTagName('HEAD')[0]; + s.appendChild(hm); + }, + onDisable() { + const scripts = document.querySelectorAll('script.analytics-script'); + scripts.forEach((script) => { + script.remove(); + }); + const hm = /^hm/i; + document.cookie + .split(';') + .map((c) => c.trim()) + .forEach((c) => { + const key = decodeURIComponent(c.split('=')[0]); + if (hm.test(key)) { + removeCustomCookie(key, { domain: location.hostname }); + } }); - const hm = /^hm/i; - document.cookie - .split(';') - .map((c) => c.trim()) - .forEach((c) => { - const key = decodeURIComponent(c.split('=')[0]); - if (hm.test(key)) { - removeCustomCookie(key, { domain: location.hostname }); - } - }); - [sessionStorage, localStorage].forEach((storage) => { - const keys = []; - for (let i = 0; i < storage.length; i++) { - const key = storage.key(i)!; - if (hm.test(key)) { - keys.push(key); - } + [sessionStorage, localStorage].forEach((storage) => { + const keys = []; + for (let i = 0; i < storage.length; i++) { + const key = storage.key(i)!; + if (hm.test(key)) { + keys.push(key); } - keys.forEach(key => storage.removeItem(key)); - }) - }, - }); - } + } + keys.forEach(key => storage.removeItem(key)); + }) + }, + request(data) { + reporAnalytics(data) + }, + }); app.directive('clamp-text', clampTextDirective); app.directive('scroll-bottom', scrollBottomDirective); app.use(VueDOMPurifyHTML); -- Gitee