-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8inputpassword.js
More file actions
344 lines (334 loc) · 13.8 KB
/
Copy pathutf8inputpassword.js
File metadata and controls
344 lines (334 loc) · 13.8 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
By: Hsin Yuan Yeh
Using a text-input HTML element to mimic password-input.
Please see demo.html for usage.
Version: v2
Date: 2024/11/16
Project URL: https://github.com/iapyeh/utf8passwordinput
*/
class Utf8PasswordInput{
static counter = 0
/**
* constructor for creating a password input element with utf8 support.
* @param {Element} syncedEle the password element to be replaced
* @param {Boolean} syncInitialValue whether to get initial value from syncedEle element
*
* when syncInitialValue is true, trying to get initial value from syncedEle element.
* (usually true for login, fale for changing password)
*/
constructor(syncedEle,syncInitialValue){
Utf8PasswordInput.counter += 1
this.syncedEle = syncedEle
//const rect = this.syncedEle.getBoundingClientRect()
const style = this.syncedEle.currentStyle || window.getComputedStyle(this.syncedEle);
// when syncInitialValue is true, trying to get initial value from syncedEle element.
// (usually true for login, fale for changing password)
this.syncInitialValue = typeof(syncInitialValue)=='undefined' ? true : syncInitialValue
this.pwd = []
this.see = false
this.star = '*'
// create a replacement element for syncedEle for user to input password
this.inputEle = this.syncedEle.cloneNode(true)
this.inputEle.classList.add('utf8passwordinput')
this.inputEle.setAttribute('type','text')
this.inputEle.setAttribute('id',`_utf8pwd_${Utf8PasswordInput.counter}`)
this.inputEle.removeAttribute('name')
// insert the replacement element after the synced element
this.syncedEle.parentNode.insertBefore(this.inputEle,this.syncedEle.nextSibling)
// create the "see" icon
this.seeEle = document.createElement('span')
this.seeEle.classList.add('utf8passwordinput')
this.seeEle.classList.add('see')
this.seeEle.innerText = '👁'
// insert the see element at the end
this.syncedEle.parentNode.insertBefore(this.seeEle,this.inputEle.nextSibling)
// adjuset see element to be aligned at the right side
const seeRect = this.seeEle.getBoundingClientRect()
const marginLeft = '-'+(1+seeRect.width + parseInt(style.marginRight) + parseInt(style.paddingRight))+'px'
this.seeEle.setAttribute('style',`cursor:pointer;margin-left:${marginLeft}`)
// minimize the synced element
this.syncedEle.style.height = '0px'
this.syncedEle.style.width = '0px'
this.syncedEle.style.position = 'absolute';
this.syncedEle.style.top = -1000;
const self = this
// flag to stop getting initial value from syncedEle
this.userInputingStarted = false
// let syncedEle's value is always updated
this.inputEle.addEventListener('update', (evt)=>{
if ((!self.userInputingStarted) && evt.detail.length) {
self.userInputingStarted = true
}
self.syncedEle.value = evt.detail
})
this.init()
}
/**
* Initializes the Utf8PasswordInput instance by setting up event listeners,
* syncing initial values if necessary, and configuring the password input
* behavior. It attempts to restore the initial password from the synced
* element if `syncInitialValue` is true. Listeners are attached to handle
* input events, manage character insertion and deletion, and control password
* visibility toggling.
*/
init(){
const self = this
// find the form to hook up "reset" event
let p = this.syncedEle.parentNode
while (p != document.body){
if (p.tagName == 'FORM'){
p.addEventListener('reset',()=>{self.value=''})
break
}
p = p.parentNode
}
const setInitialValueFromSyncedEle=(timeout)=>{
// ele is the password element which browser's password manager would restore password to
if (!timeout) timeout = 30 * 1000 // try at most 30 seconds
const start = new Date().getTime()
const checkpwd = ()=>{
if (self.userInputingStarted){
// stop getting value when user is starting to input
return
}else if ((new Date().getTime() - start) > timeout){
console.warn(`failed to restore password from element ${self.syncedEle.outerHTML}`)
return;
}
try{
if (self.value == ''){
if (self.syncedEle.value == ''){
setTimeout(checkpwd,100)
}else{
self.value = self.syncedEle.value
self.syncedEle.style.display = 'none'
}
}
}catch(e){
console.error(e)
setTimeout(checkpwd,100)
}
}
checkpwd()
}
if (this.syncInitialValue) setInitialValueFromSyncedEle()
// hooking up listeners
const inputEle = this.inputEle
const seeEle = this.seeEle
const fireUpdateEvent = ()=>{
inputEle.dispatchEvent(new CustomEvent('update',{detail:self.value}))
}
//hooking event listeners for "input" element
inputEle.addEventListener('beforeinput',(evt)=>{
// selectionEnd 此時才準,到input event時已經不是真的值
let s = evt.target.selectionStart;
let e;
if (evt.inputType == 'insertCompositionText'){
// skip, handle ckj input in "compositionend" event
return
}else if (evt.inputType=='insertText'){
self._pos = evt.target.selectionEnd
e = evt.target.selectionEnd
}else if (evt.inputType=='deleteContentBackward'){
// <- del
self._pos = evt.target.selectionEnd
e = evt.target.selectionEnd
if (s==e) s = Math.max(0,s-self._starLen)
}else if (evt.inputType == 'deleteContentForward'){
// del ->
self._pos = evt.target.selectionEnd
e = evt.target.selectionEnd
if (s==e) e += self._starLen
}else if (evt.inputType == 'insertFromPaste'){
self._pos = evt.target.selectionEnd
e = evt.target.selectionEnd
}else if (evt.inputType == 'compositionend'){
//this case might not happen
self._pos = evt.target.selectionEnd
e = evt.target.selectionEnd
}else{
console.log('???'+evt.inputType)
e = s
}
if (s != e){
const idx0 = s/self._starLen
const idx1 = e/self._starLen
const delLength = idx1 - idx0
self.pwd.splice(idx0,delLength)
self._pos = s
}
})
inputEle.addEventListener('input', (evt)=>{
if (self.see) return
let pos,inserted,idx
if (evt.inputType=='insertText'){
pos = evt.target.selectionStart - evt.data.length
idx = pos/self._starLen
for(const char of evt.data){
self.pwd.splice(idx,0,char)
idx += 1
}
const extraOffset = (evt.data.length * self._starLen) - evt.data.length
self.resetInputEleValue(extraOffset)
fireUpdateEvent()
}else if (evt.inputType=='deleteContentBackward'){
self.resetInputEleValue()
fireUpdateEvent()
}else if (evt.inputType=='deleteContentForward'){
self.resetInputEleValue()
fireUpdateEvent()
}else if (evt.inputType == 'insertFromPaste'){
//unicodes inserted by paste, such as "😀"
idx = self._pos/self._starLen
inserted = evt.data //inputEle.value.substring(self._pos,evt.target.selectionStart)
let i = 0
for(const char of inserted){
self.pwd.splice(idx+i,0,char)
i += 1
}
// help the cursor to posite correclty at the end of inserted text when self._starLen > 1 (eg.😀)
const extraOffset = (i * self._starLen) - inserted.length
self.resetInputEleValue(extraOffset)
fireUpdateEvent()
}else{
// eg. historyUndo, insertCompositionText
}
})
inputEle.addEventListener('compositionstart', (evt)=>{
if (self.see) return
// delete selected text
const s = evt.target.selectionStart
const e = evt.target.selectionEnd
if (s != e){
const idx0 = s/self._starLen
const idx1 = e/self._starLen
const delLength = idx1 - idx0
self.pwd.splice(idx0,delLength)
}
})
inputEle.addEventListener('compositionend', (evt)=>{
//CKY chareactors are inputed
if (self.see) return
let idx = (evt.target.selectionStart - evt.data.length)/self._starLen
let i = 0
for(const char of evt.data){
self.pwd.splice(idx + i,0,char)
i += 1
}
const extraOffset = (i * self._starLen) - evt.data.length
self.resetInputEleValue(extraOffset)
fireUpdateEvent()
})
// hooking event listeners for "see" element
if (seeEle){
const showPassword = (evt)=>{
if (self.see) return //avoid double invoking by events
self.see = true
self._pos = inputEle.selectionStart
inputEle.value = self.getpwd(self.see)
}
const hidePassword = (evt)=>{
if (!self.see) return //avoid double invoking by events
self.value = inputEle.value
self.see = false
inputEle.value = self.getpwd(self.see)
inputEle.focus()
inputEle.selectionStart = self._pos
inputEle.selectionEnd = self._pos
}
seeEle.addEventListener('pointerdown', showPassword)
seeEle.addEventListener('pointerup',hidePassword)
}
}
/**
* property to get the value of the password input.
* @type {String}
*/
get value(){
return this.getpwd(true)
}
/**
* Sets the password value for the input element.
* This function updates the internal password array with the provided value,
* resetting any existing password, and then fires an update event to notify
* listeners of the change.
*
* @param {string} v - The new password value to be set.
*/
set value(v){
this.pwd = []
for (let i=0;i<v.length;i++){
this.pwd.push(v.substr(i,1))
}
//fire update event
this.resetInputEleValue(v.length)
this.inputEle.dispatchEvent(new CustomEvent('update',{detail:this.value}))
}
/**
* Returns the length of the string in bytes.
* @param {string} [v] - The string to calculate the length of. If not provided, use the current value of the password input.
* @returns {number} - The length of the string in bytes.
*/
getBytesLength(v){
// REF: https://stackoverflow.com/questions/5515869/string-length-in-bytes-in-javascript
if (typeof(v)=='undefined') v = this.value
else if (!typeof(v)=='string') {
console.warn('getBytesLength() called with non-string '+v)
return null
}
let m = encodeURIComponent(v).match(/%[89ABab]/g);
return v.length + (m ? m.length : 0);
}
/**
* The length of the password string in bytes.
* This property is a shortcut of calling `getBytesLength()` without arguments.
* @type {number}
*/
get length(){
return this.getBytesLength()
}
/**
* The character to be used to replace the password string when the user chooses to not see the password.
* @type {string}
*/
get star(){
return this._star
}
/**
* Sets the character to be used to replace the password string when the user chooses to not see the password.
* @param {string} c - The character to be used to replace the password string.
* @type {string}
*/
set star(c){
this._star = c
this._starLen = c.length
}
/**
* Attach an event listener to the input element of the password input.
* This is just a shortcut of calling `addEventListener()` on the input element.
* @param {string} name - The name of the event to listen for.
* @param {function} callback - The function to call when the event is triggered.
* @returns {EventTarget} - The input element, so you can chain the calls.
*/
on(name,callback){
return this.inputEle.addEventListener(name,callback)
}
resetInputEleValue (extraOffset) {
const pos = this.inputEle.selectionStart + (extraOffset || 0)
this.inputEle.value = this.getpwd(this.see)
// reposition the cursor
this.inputEle.selectionStart = pos
this.inputEle.selectionEnd = pos
}
getpwd(plain){
let chars = []
const self = this
this.pwd.map((c)=>{
if (plain){
chars.push(c)
}else{
chars.push(self.star)
}
})
return chars.join('')
}
}