-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-id.ts
More file actions
53 lines (41 loc) · 1.46 KB
/
app-id.ts
File metadata and controls
53 lines (41 loc) · 1.46 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
const ONE_DAY = 24 * 60 * 60 * 1000
function getAppId() {
const urlParams = new URLSearchParams(window.location.search)
const urlAppId = urlParams.get('appId')
console.log('appId', urlAppId)
if (urlAppId && urlAppId.startsWith('did:key:')) {
localStorage.setItem('appId', urlAppId)
localStorage.setItem('lastAccess', Date.now().toString())
}
const lastAccess = Number.parseInt(localStorage.getItem('lastAccess') || '0')
// Check if lastAccess was more than 1 day ago, we don't want to store appId longer than that
if (lastAccess + ONE_DAY < Date.now()) {
localStorage.removeItem('appId')
localStorage.removeItem('lastAccess')
}
return localStorage.getItem('appId') || null
}
export default defineNuxtPlugin(() => {
const router = useRouter()
router.afterEach(async () => {
await nextTick()
function updatePage() {
const appId = getAppId()
if (appId) {
document.body.querySelectorAll('span').forEach((el) => {
// Process only direct text nodes, not text in child elements
for (const node of el.childNodes) {
if (node.nodeType === Node.TEXT_NODE && node.textContent && node.textContent.includes('<your-app-id>')) {
node.textContent = node.textContent.replace(/<your-app-id>/g, appId);
}
}
})
}
}
updatePage()
const nuxtApp = useNuxtApp()
nuxtApp.hook('page:finish', () => {
updatePage()
})
})
})