`
function isCjkContext(str) {
// since js regex does not support 5-digit hex unicode
// we will have to check for the range ourselves
// CJK Unified Ideographs
const cjkRange1 = [0x4E00, 0x9FFF]
// CJK Unified Ideographs Extension A
const cjkRange2 = [0x3400, 0x4DBF]
// CJK Unified Ideographs Extension B
const cjkRange3 = [0x20000, 0x2A6DF]
// CJK Unified Ideographs Extension C
const cjkRange4 = [0x2A700, 0x2B73F]
// CJK Unified Ideographs Extension D
const cjkRange5 = [0x2B740, 0x2B81F]
// CJK Unified Ideographs Extension E
const cjkRange6 = [0x2B820, 0x2CEAF]
// CJK Unified Ideographs Extension F
const cjkRange7 = [0x2CEB0, 0x2EBEF]
// CJK Unified Ideographs Extension G
const cjkRange8 = [0x30000, 0x3134F]
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
if ((char >= cjkRange1[0] && char <= cjkRange1[1]) ||
(char >= cjkRange2[0] && char <= cjkRange2[1]) ||
(char >= cjkRange3[0] && char <= cjkRange3[1]) ||
(char >= cjkRange4[0] && char <= cjkRange4[1]) ||
(char >= cjkRange5[0] && char <= cjkRange5[1]) ||
(char >= cjkRange6[0] && char <= cjkRange6[1]) ||
(char >= cjkRange7[0] && char <= cjkRange7[1]) ||
(char >= cjkRange8[0] && char <= cjkRange8[1])) {
return true
}
}
return false
}
function setupControls() {
const articleBody = document.querySelector('article')
if (articleBody == null) {
return;
}
if (document.documentElement.lang.startsWith('zh')) {
articleBody.insertAdjacentHTML('beforebegin', controlHtml)
} else {
articleBody.insertAdjacentHTML('beforebegin', controlHtmlEn)
}
const translationModeBox = document.getElementById('d-l10n-translation-mode')
const annotationModeBox = document.getElementById('d-l10n-annotation-mode')
if (translationModeBox == null) {
console.error('d-l10n-translation-mode not found')
return
}
if (annotationModeBox == null) {
console.error('d-l10n-annotation-mode not found')
return
}
const currentPreferredTranslation = getPreferredTranslation()
translationModeBox.value = currentPreferredTranslation
const currentPreferredAnnotation = getPreferredAnnotation()
annotationModeBox.value = currentPreferredAnnotation
translationModeBox.addEventListener('change', (event) => {
changePreferredTranslation(event.target.value)
processTranslationBoxes()
})
annotationModeBox.addEventListener('change', (event) => {
changePreferredAnnotation(event.target.value)
processTranslationBoxes()
})
}
function processColorBoxes() {
const allColorBoxes = document.querySelectorAll(".d-l10n-color")
const knownColors = {}
// first pass: collect all colors
allColorBoxes.forEach((colorBox) => {
const key = colorBox.innerHTML
const value = colorBox.getAttribute('data-color')
if (value == null || value.length < 1) {
return
}
knownColors[key] = value
})
// second pass: add a little box with the color
allColorBoxes.forEach((colorBox) => {
const key = colorBox.innerHTML
const value = knownColors[key]
if (value == null || value.length < 1) {
return
}
colorBox.innerHTML = `