吴晓阳
发布于 2025-03-18 / 13 阅读
0

vitepress加js脚本

vitepress加js脚本

在下面文件中写js脚本

js脚本

import DefaultTheme from 'vitepress/theme'

// 定义域名标题映射
const domainTitles = {
  'localhost': {
    name: 'Localhost域名的标题',
    text: '开开心心生活每一天!!!',
    beian: '京ICP备1号-1',
    ga: '浙公网安备33030202001973号'
  },
  'www.wuxiaoyang.cn': {
    name: '有时间就干点有意思的事',
    text: '开开心心生活每一天!!!',
    beian: '浙ICP备12047409号-1',
    ga: '浙公网安备33030202001973号'
  },
  'www.swl.ink': {
    name: '高山仰止景行止的记录',
    text: '开开心心生活每一天!!!',
    beian: '浙ICP备12047409号-5',
    ga: '浙公网安备33030202001973号'
  },
  'www.wxy.work': {
    name: '开开心心每一天',
    text: '开开心心生活每一天!!!',
    beian: '浙ICP备12047409号-6',
    ga: '浙公网安备33030202001973号'
  },
  'www.wxy.kim': {
    name: '阿拉法1',
    text: '开开心心生活每一天!!!',
    beian: '浙ICP备12047409号-7',
    ga: '浙公网安备33030202001973号'
  },
  'www.wxy.vip': {
    name: '信息奥赛学习网',
    text: '开开心心生活每一天!!!',
    beian: '浙ICP备12047409号-9',
    ga: '浙公网安备33030202001973号'
  }
};

export default {
  ...DefaultTheme,
  enhanceApp({ app, router, siteData }) {
    // 在客户端加载时执行
    if (typeof window !== 'undefined') {
        
      router.onAfterRouteChanged = () => {
        const hostname = window.location.hostname;
        console.log('路由变化后执行脚本,当前域名:', hostname);
        setTimeout(() => {
          const doctitleElement = document.querySelector('title');
          const titleElement = document.querySelector('.title span');
          const heroNameElement = document.querySelector('.VPHero .name');
          const heroTextElement = document.querySelector('.VPHero .text');
          const footerElement = document.querySelector('.VPFooter .icp span');
          const gaElement = document.querySelector('.VPFooter .mps span');
          // 查找匹配的域名配置
          const domainKey = Object.keys(domainTitles).find(key => hostname.includes(key));
          if (domainKey) {
            const titles = domainTitles[domainKey];
            if (heroNameElement) heroNameElement.textContent = titles.name;
            if (heroTextElement) heroTextElement.textContent = titles.text;
            if (titleElement) titleElement.textContent = titles.name;
            if (doctitleElement) doctitleElement.textContent = titles.name;
            if (footerElement) footerElement.innerHTML = titles.beian;
            if (gaElement) gaElement.innerHTML = titles.ga;
          }
          console.log('路由变化后执行脚本,当前域名:', hostname);
        }, 100);
      }
    }
  }
} 

vitepress安装

pnpm init
pnpm add -D vitepress

pnpm vitepress init

pnpm run docs:dev
pnpm run docs:build

Getting Started | VitePress