// ==UserScript== // @name Product Hunt 汉化 // @namespace https://github.com/anghunk/UserScript // @description 由于官方不支持中文,汉化大部分的翻译 // @match *://*.producthunt.com/* // @grant none // @license Apache-2.0 license // @icon https://app.netlify.com/favicon-48x48.png // @version 1.0 // @author anghunk // ==/UserScript== const zh_Hans = [ ['Launches', '发布'], ['Products', '产品'], ['News', '新闻'], ['Community', '社区'], ['Advertise', '广告'], ['Top Products Launching Today', '今日热门产品发布'], ['Featured', '精选'], ['All', '全部'], ['Top launches', '热门推荐'], ['Submit', '发布'], ["Today's winners", '今日榜单'], ["Yesterday's winners", '昨日榜单'], ["Last week's winners", '上周榜单'], ["Last month's winners", '上月榜单'], ['Coming soon', '即将推出'], ['Jobsolv', '职业求解'], ['TapRefer', '点击参考'], ['Latest Stories', '最新故事'], ['Discussions', '讨论'], ['Launching soon?', '即将推出?'], ['Learn everything you need to know about launching on Product Hunt 🚀', '了解有关在 Product Hunt 上发布所需的所有信息 🚀'], ['Read the guide', '阅读指南'], ]; class ReplaceText { constructor(i18n, mode = 'equal') { this.W = typeof unsafeWindow === 'undefined' ? window : unsafeWindow; this.done = new Set(); this.alert = this.W.alert.bind(this.W); this.confirm = this.W.confirm.bind(this.W); this.prompt = this.W.prompt.bind(this.W); const i18nMap = new Map(i18n); const i18nArr = i18n.map(value => value[0]); if (mode === 'regexp') { this.textReplace = (text) => { if (i18nMap.has(text)) text = i18nMap.get(text); else { const key = i18nArr.find(key => (key instanceof RegExp && text.match(key) !== null)); if (key !== undefined) text = text.replace(key, i18nMap.get(key)); } return text; }; } else if (mode === 'match') { this.textReplace = (text) => { const key = i18nArr.find(key => (text.match(key) !== null)); if (key !== undefined) text = text.replace(key, i18nMap.get(key)); return text; }; } else { this.textReplace = (text) => { if (i18nMap.has(text)) text = i18nMap.get(text); return text; }; } this.replaceAlert(); this.replaceObserver(); } replaceAlert() { this.W.alert = (message) => this.alert(this.textReplace(message)); this.W.confirm = (message) => this.confirm(this.textReplace(message)); this.W.prompt = (message, _default) => this.prompt(this.textReplace(message), _default); } replaceNode(node, self = false) { const list = this.getReplaceList(node, self); for (let index in list) { list[index].forEach(node => { if (this.done.has(node[index])) return; const newText = this.textReplace(node[index]); if (node[index] !== newText) { this.done.add(newText); node[index] = newText; } }); } } replaceObserver() { const bodyObserver = new MutationObserver(mutations => { mutations.forEach(mutation => { if (mutation.type === 'attributes' || mutation.type === 'characterData') this.replaceNode(mutation.target, true); else if (mutation.type === 'childList') { mutation.addedNodes.forEach(addedNode => this.replaceNode(addedNode)); } }); }); document.addEventListener('readystatechange', () => { bodyObserver.observe(document.body, { attributes: true, characterData: true, childList: true, subtree: true }); this.replaceNode(document.body); }, { capture: true, once: true }); } getReplaceList(node, self = false) { const list = { data: new Set(), placeholder: new Set(), title: new Set(), value: new Set(), }; const nodeList = self ? [node] : this.nodeForEach(node); nodeList.forEach(node => { if (node.parentElement instanceof HTMLScriptElement || node.parentElement instanceof HTMLStyleElement) return; if (node instanceof HTMLElement && node.title !== '') list.title.add(node); if (node instanceof HTMLInputElement && ['button', 'reset', 'submit'].includes(node.type) && node.value !== '') list.value.add(node); else if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement && node.placeholder !== '') list.placeholder.add(node); else if (node instanceof Text) list.data.add(node); }); return list; } nodeForEach(node) { const list = []; list.push(node); if (node.hasChildNodes()) node.childNodes.forEach(child => list.push(...this.nodeForEach(child))); return list; } } new ReplaceText(zh_Hans, 'regexp');