PageSourceSearch

https://wcowin.work/javascripts/translation-switcher.js

js wcowin.work collected 2026-09-28 05:46:11 UTC 3,331 bytes, 122 lines download raw bytes

1/**
2 * 翻译服务切换器
3 * 允许用户在 GLM 翻译和 Microsoft 翻译之间切换
4 * 注:Microsoft 翻译服务虽名为 Edge 翻译,但实际上支持所有现代浏览器
5 */
6
7class TranslationSwitcher {
8  constructor() {
9    this.currentService = 'edge'; // 默认使用 Microsoft 翻译
10    this.init();
11  }
12
13  init() {
14    // 从 localStorage 加载保存的设置
15    this.loadSavedSetting();
16    
17    // 检查 URL 参数,允许通过 URL 切换翻译服务
18    this.checkUrlParams();
19    
20    // 创建切换按钮(已隐藏)
21    this.createSwitcherUI();
22    
23    // 绑定事件(已禁用)
24    this.bindEvents();
25    
26    // 初始化当前服务
27    this.switchToService(this.currentService);
28  }
29
30  checkUrlParams() {
31    // 检查 URL 参数,允许通过 ?translation=glm 或 ?translation=edge 切换翻译服务
32    // 注:edge 代表 Microsoft 翻译服务
33    const urlParams = new URLSearchParams(window.location.search);
34    const translationParam = urlParams.get('translation');
35    if (translationParam && (translationParam === 'glm' || translationParam === 'edge')) {
36      this.currentService = translationParam;
37      this.saveSetting(translationParam);
38    }
39  }
40
41  loadSavedSetting() {
42    try {
43      const saved = localStorage.getItem('translation_service');
44      if (saved && (saved === 'glm' || saved === 'edge')) {
45        this.currentService = saved;
46      }
47    } catch (error) {
48      console.warn('加载翻译服务设置失败:', error);
49    }
50  }
51
52  saveSetting(service) {
53    try {
54      localStorage.setItem('translation_service', service);
55    } catch (error) {
56      console.warn('保存翻译服务设置失败:', error);
57    }
58  }
59
60  createSwitcherUI() {
61    // 不创建切换器 UI,根据用户要求隐藏
62    console.log('Translation switcher UI is hidden as requested');
63  }
64
65  addStyles() {
66    // 不添加样式,因为切换器 UI 被隐藏
67  }
68
69  bindEvents() {
70    // 不绑定事件,因为切换器 UI 被隐藏
71  }
72
73  switchToService(service) {
74    this.currentService = service;
75    this.saveSetting(service);
76    
77    // 切换翻译服务
78    if (service === 'glm') {
79      this.activateGLMTranslation();
80    } else {
81      this.activateEdgeTranslation();
82    }
83  }
84
85  activateGLMTranslation() {
86    console.log('切换到 GLM 翻译服务');
87    
88    // 确保 GLM 翻译已初始化
89    if (window.translateTo) {
90      console.log('GLM 翻译服务已就绪');
91    } else {
92      console.log('GLM 翻译服务未初始化');
93    }
94  }
95
96  activateEdgeTranslation() {
97    console.log('切换到 Microsoft 翻译服务');
98    
99    // 确保 Microsoft 翻译已初始化
100    if (window.translateToEdge) {
101      console.log('Microsoft 翻译服务已就绪');
102    } else {
103      // 尝试初始化 Microsoft 翻译
104      if (typeof translate !== 'undefined') {
105        if (typeof initEdgeTranslation === 'function') {
106          initEdgeTranslation();
107        }
108      } else {
109        console.error('Microsoft 翻译服务依赖的 translate.js 未加载');
110      }
111    }
112  }
113}
114
115// 初始化翻译服务切换器
116if (document.readyState === 'loading') {
117  document.addEventListener('DOMContentLoaded', () => {
118    window.translationSwitcher = new TranslationSwitcher();
119  });
120} else {
121  window.translationSwitcher = new TranslationSwitcher();
122}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.