feat: 前端加密的 kv 存儲
This commit is contained in:
43
client/Data.ts
Normal file
43
client/Data.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import CryptoJS from "./types/CryptoJS.d.ts"
|
||||||
|
const dataIsEmpty = !localStorage.tws_data || localStorage.tws_data == ''
|
||||||
|
|
||||||
|
const aes = {
|
||||||
|
enc: (m: string, k: string) => CryptoJS.AES.encrypt(m, k).toString(),
|
||||||
|
dec: (m: string, k: string) => CryptoJS.AES.decrypt(m, k).toString(CryptoJS.enc.Utf8),
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = location.host + '_TWS_姐姐'
|
||||||
|
|
||||||
|
if (dataIsEmpty) localStorage.tws_data = aes.enc('{}', key)
|
||||||
|
|
||||||
|
let _dec = aes.dec(localStorage.tws_data, key)
|
||||||
|
if (_dec == '') _dec = '{}'
|
||||||
|
|
||||||
|
const _data_cached = JSON.parse(_dec)
|
||||||
|
|
||||||
|
// 類型定義
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
data: {
|
||||||
|
apply: () => void
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-window
|
||||||
|
(window.data == null) && (window.data = new Proxy({
|
||||||
|
apply() {
|
||||||
|
localStorage.tws_data = aes.enc(JSON.stringify(_data_cached), key)
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
get(_obj, k) {
|
||||||
|
return _data_cached[k]
|
||||||
|
},
|
||||||
|
set(_obj, k, v) {
|
||||||
|
_data_cached[k] = v
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
// deno-lint-ignore no-window
|
||||||
|
export default window.data
|
||||||
Reference in New Issue
Block a user