Files
browser-scripts/scripts/ttv-obfuscated-names/storage.ts
T
zomo 6218d6bad1 changed observer callback
update names based on the element from the mutation event rather than checking the chatbox for unchecked messages each mutation
2026-04-19 19:01:01 -05:00

141 lines
3.3 KiB
TypeScript

import { NameConfigInstance, nameList, colorList } from './options'
type StoredUsers = {
[username: string]: NameConfigInstance
}
type NameListUsed = {
[username: string]: number
}
interface StoredData {
storedUsers: StoredUsers
nameListUsed: NameListUsed
nameListCount: number
}
let { storedUsers, nameListUsed, nameListCount } = loadStoredUserData()
export function getStoredUser(name: string): NameConfigInstance | null {
if (name in storedUsers) {
return storedUsers[name]
}
return null
}
export function setStoredUser(name: string, details: NameConfigInstance) {
storedUsers[name] = details
updateStorageData()
}
export function increaseNameListUsed(name: string) {
nameListUsed[name] = (nameListUsed[name] ?? 0) + 1
updateStorageData()
}
export function increaseNameListCount() {
nameListCount++
updateStorageData()
}
function updateStorageData() {
localStorage.setItem(
'obf-data',
JSON.stringify({
storedUsers,
nameListUsed,
nameListCount,
})
)
}
function loadStoredUserData(): StoredData {
const dataStr = localStorage.getItem('obf-data')
if (dataStr === null) {
return {
storedUsers: {},
nameListUsed: {},
nameListCount: 0,
}
}
try {
const data = JSON.parse(dataStr)
return data as StoredData
} catch (e) {
return {
storedUsers: {},
nameListUsed: {},
nameListCount: 0,
}
}
}
// TODO will be useful for better options
export function clearStoredUsers() {
localStorage.removeItem('obf-data')
storedUsers = {}
nameListUsed = {}
nameListCount = 0
}
;(unsafeWindow as any).obfClear = () => {
clearStoredUsers()
location.reload()
}
export function getRandomName(): NameConfigInstance {
const startingIndex = Math.round(Math.random() * (nameList.length - 1))
let foundName = false
let currentIndex = startingIndex
for (let i = 0; i < nameList.length; i++) {
currentIndex = (startingIndex + i) % nameList.length
let isUsed = nameListUsed[nameList[currentIndex].username] ?? 0
if (isUsed === nameListCount) {
foundName = true
break
}
}
if (!foundName) {
increaseNameListCount()
currentIndex = startingIndex
}
increaseNameListUsed(nameList[currentIndex].username)
const newName = nameList[currentIndex]
const newNameInstance: NameConfigInstance = {
username: newName.username,
image: newName.image,
color: '',
nameCount: nameListCount,
}
// choose a random color from the list
if (Array.isArray(newName.color)) {
const color =
newName.color[
Math.round(Math.random() * (newName.color.length - 1))
]
newNameInstance.color = color
return newNameInstance
}
// choose a random color from the global list
if (newName.color === null) {
const color =
colorList[Math.round(Math.random() * (colorList.length - 1))]
newNameInstance.color = color
return newNameInstance
}
// copy individual carbing
newNameInstance.color = newName.color
return newNameInstance
}