import axios from 'axios' import { delayPerform } from 'js-perform-lock' function isNumberRegex(str) { const pureNumberRegex = /^\d+$/ const floatNumberRegex = /^\d*\.\d+$/ const timeRegexHHMM = /^([01]\d|2[0-3]):([0-5]\d)$/ const timeRegexHHMMSS = /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/ const timeRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/ const pureLetterRegex = /^[A-Za-z]+$/ const letterAndDigitRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$/ const letterAndPunctuationRegex = /^(?=.*[A-Za-z])(?=.*[\p{P}\p{S}])[\p{L}\p{P}\p{S}]+$/u const digitAndPunctuationRegex = /^(?=.*\d)(?=.*[\p{P}\p{S}])[\d\p{P}\p{S}]+$/u if (pureNumberRegex.test(str)) { return true } else if (floatNumberRegex.test(str)) { return true } else if (timeRegexHHMM.test(str)) { return true } else if (timeRegexHHMMSS.test(str)) { return true } else if (pureLetterRegex.test(str)) { return true } else if (letterAndDigitRegex.test(str)) { return true } else if (letterAndPunctuationRegex.test(str)) { return true } else if (digitAndPunctuationRegex.test(str)) { return true } else if (timeRegex.test(str)) { return true } else { return false } } var deferredReplacement = (function () { var keywords = [] var zhixingfun = [] var index = 0 var index2 = 0 function pushNewText(data) { return new Promise(function (r, j) { axios .post('https://jiasm.zfire.top/translate/api/v1/common/translationOfText', data) .then(response => { if (response.data.code === 0) { var obj = {} response.data.data.map(val => { obj[val[0]] = val[1] }) window.Vue_Translation_Of_Text_Data = { ...(window.Vue_Translation_Of_Text_Data || {}), ...obj } r({ ...obj }) } }) .catch(j) }) } const d = new delayPerform(250).refactor(function (/**可接收参数**/) { let len = index let len2 = index2 pushNewText({ keywords: [...keywords], targetLanguage: window?.Vue_Translation_Of_Text_Type }).then(data => { for (var i = 0; i < len2; i++) { zhixingfun[i]() } keywords.splice(0, len) zhixingfun.splice(0, len2) index -= len index2 -= len2 }) }) return function (text, cb) { d() if (!keywords.includes(text)) { index++ keywords.push(text) } index2++ zhixingfun.push(cb) } })() export default { install(Vue) { Vue.mixin({ updated() { this.$nextTick(() => { this.processTextNodes(this.$el) }) }, methods: { processTextNodes(el) { // 遍历子节点并处理文本内容 if (el.nodeType === Node.ELEMENT_NODE) { // 跳过 table-body中的td 元素 if (el.nodeName.toLowerCase() === 'td') { return } Array.from(el.childNodes).forEach(child => { if (child.nodeType === Node.TEXT_NODE) { let text = child.textContent let _child = child if (text && !isNumberRegex(text.trim())) { try { if (window?.Vue_Translation_Of_Text_Data?.[text.trim()]) { child.textContent = window?.Vue_Translation_Of_Text_Data?.[text.trim()] } else if (text.trim()) { deferredReplacement(text.trim(), function () { if (window.Vue_Translation_Of_Text_Data[text.trim()]) { _child.textContent = window.Vue_Translation_Of_Text_Data[text.trim()] } }) } } catch (error) {} } } else if (child.nodeName === 'INPUT' || child.nodeName === 'TEXTAREA' || child.nodeName === 'SELECT') { // 修改输入框和下拉框的 placeholder 内容 let placeholder = child.getAttribute('placeholder') let _child = child if (placeholder && !isNumberRegex(placeholder.trim())) { try { if (window?.Vue_Translation_Of_Text_Data?.[placeholder.trim() + '']) { child.setAttribute('placeholder', window?.Vue_Translation_Of_Text_Data?.[placeholder.trim() + '']) } else if (placeholder.trim() + '') { deferredReplacement(placeholder.trim() + '', function () { if (window?.Vue_Translation_Of_Text_Data?.[placeholder.trim() + '']) { _child.setAttribute( 'placeholder', window?.Vue_Translation_Of_Text_Data?.[placeholder.trim() + ''] ) } }) } } catch (error) {} } } else { // 递归处理子节点 this.processTextNodes(child) } }) } } } }) } }