xyc
3 天以前 53052625c7c520744a4ca061e204ec9594ca67f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const SKIPPED_TAGS = {
  SCRIPT: true,
  STYLE: true
}
 
let replacements = []
let observer = null
 
function isConfigured(realName, codename) {
  return realName &&
    realName !== codename &&
    !(realName.startsWith('[') && realName.endsWith(']'))
}
 
function replaceAsciiCodename(value, codename, realName) {
  const pattern = new RegExp('(^|[^A-Za-z0-9])' + codename + '(?=$|[^A-Za-z0-9])', 'g')
  return value.replace(pattern, function (match, prefix) {
    return prefix + realName
  })
}
 
export function replaceSensitiveText(value) {
  if (typeof value !== 'string' || !value || !replacements.length) {
    return value
  }
 
  return replacements.reduce(function (result, item) {
    if (/^[A-Za-z0-9]+$/.test(item.codename)) {
      return replaceAsciiCodename(result, item.codename, item.real_name)
    }
    return result.split(item.codename).join(item.real_name)
  }, value)
}
 
function replaceTextNode(node) {
  const nextValue = replaceSensitiveText(node.nodeValue)
  if (nextValue !== node.nodeValue) {
    node.nodeValue = nextValue
  }
}
 
function replaceAttribute(element, attribute) {
  if (!element.hasAttribute(attribute)) {
    return
  }
  const value = element.getAttribute(attribute)
  const nextValue = replaceSensitiveText(value)
  if (nextValue !== value) {
    element.setAttribute(attribute, nextValue)
  }
}
 
function replaceAttributes(root) {
  const attributes = ['placeholder', 'title', 'aria-label']
  // MutationObserver may report comment nodes or other non element nodes.
  if (root.nodeType !== 1 && root.nodeType !== 9 && root.nodeType !== 11) {
    return
  }
  if (root.nodeType === 1) {
    attributes.forEach(function (attribute) {
      replaceAttribute(root, attribute)
    })
  }
  root.querySelectorAll('[placeholder], [title], [aria-label]').forEach(function (element) {
    attributes.forEach(function (attribute) {
      replaceAttribute(element, attribute)
    })
  })
}
 
function replaceDom(root) {
  if (!root || !replacements.length) {
    return
  }
 
  if (root.nodeType === 3) {
    replaceTextNode(root)
    return
  }
 
  replaceAttributes(root)
  const walker = document.createTreeWalker(
    root,
    NodeFilter.SHOW_TEXT,
    {
      acceptNode: function (node) {
        const parent = node.parentElement
        return parent && !SKIPPED_TAGS[parent.tagName]
          ? NodeFilter.FILTER_ACCEPT
          : NodeFilter.FILTER_REJECT
      }
    }
  )
 
  let node = walker.nextNode()
  while (node) {
    replaceTextNode(node)
    node = walker.nextNode()
  }
}
 
function watchDom() {
  if (observer || !document.body) {
    return
  }
 
  observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      if (mutation.type === 'characterData') {
        replaceTextNode(mutation.target)
        return
      }
      mutation.addedNodes.forEach(function (node) {
        replaceDom(node)
      })
    })
  })
 
  observer.observe(document.body, {
    childList: true,
    subtree: true,
    characterData: true
  })
}
 
export async function installSensitiveWords() {
  try {
    const baseUrl = process.env.BASE_URL || '/'
    const response = await fetch(baseUrl + 'sensitive-words.json?ts=' + Date.now(), {
      cache: 'no-store'
    })
    if (!response.ok) {
      return
    }
 
    const payload = await response.json()
    replacements = (payload.sensitive_words || [])
      .filter(function (item) {
        return item && item.codename && isConfigured(item.real_name, item.codename)
      })
      .sort(function (left, right) {
        return right.codename.length - left.codename.length
      })
 
    replaceDom(document.body)
    watchDom()
  } catch (error) {
    // The application still works with development codenames if the dictionary is unavailable.
  }
}