-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvscript.js
131 lines (128 loc) · 5.28 KB
/
vscript.js
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
let State = {}
/**
* @param {HTMLElement} root
* @param {boolean} init
*/
async function walk(root, init) {
for (const element of root.children) {
if (element instanceof HTMLElement) {
for (const attribute of element.attributes) {
if (init) {
if (attribute.name == 'v-data') {
Object.assign(State, eval(`(${attribute.value})`))
}
if (attribute.name.startsWith('@')) {
const attrs = attribute.name.substring(1).split('.')
const isMouseEvent = ['click','dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseout', 'mouseover', 'mouseup'].includes(attrs[0])
if (attrs.length == 1) {
if (attribute.name == '@boost') {
element.addEventListener('click', (event) => {
if (event.isTrusted && event.target == event.currentTarget) {
// Allow the link to be opened in a new tab or window
if (event.ctrlKey || event.shiftKey) {
return
}
event.preventDefault()
const href = event.target.getAttribute('href')
const path = new URL(href, window.location.origin).pathname
fetch(path, {
method: "GET",
headers: { "v-request": "true" },
})
.then(async(res) => {
if (res.status < 300) {
const title = res.headers.get("title")
if (typeof title == 'string') {
document.title = title
}
window.history.pushState({}, "", path)
const body = await res.text()
document.body.innerHTML = body
walk(document.body, true)
}
})
}
})
}
if (isMouseEvent) {
element.addEventListener(attrs[0], (event) => {
if (event.isTrusted) {
eval(`with (State) { (${attribute.value}) }`)
}
})
}
} else if (isMouseEvent) {
switch (attrs[attrs.length-1]) {
case 'inject':{
element.addEventListener(attrs[0], function(event) {
if (event.isTrusted && event.target == event.currentTarget) {
const path = attribute.value || window.location.href
let target = event.target
if (event.target.hasAttribute('v-target')) {
const targetid = event.target.getAttribute('v-target')
target = document.getElementById(targetid)
}
fetch(new URL(path, window.location.origin), {
method: "GET",
headers: { "v-request": "true" },
})
.then(async (res) => {
if (res.status < 300 && res.headers.get("content-type")?.startsWith('text/html')) {
const html = await res.text()
if (target instanceof HTMLElement) {
target.insertAdjacentHTML('beforebegin', html)
walk(target.previousSibling, true)
target.parentElement.removeChild(target)
const callback = event.target.getAttribute('v-callback')
if (typeof callback == 'string') {
eval(`with (State) { (${callback}) }`)
}
}
}
})
}
})
break;
}
case 'eject': {
element.addEventListener(attrs[0], function(event) {
if (event.isTrusted && event.target == event.currentTarget) {
let target = event.target
if (event.target.hasAttribute('v-target')) {
const targetid = event.target.getAttribute('v-target')
target = document.getElementById(targetid)
}
const replacer = document.createElement(target.tagName)
replacer.id = target.id
target.replaceWith(replacer)
}
})
break;
}
}
}
}
}
if (attribute.name == 'v-text') {
element.innerText = eval(`with (State) { (${attribute.value}) }`)
}
if (attribute.name == 'v-show') {
element.style.display = eval(`with (State) { (${attribute.value}) }`) ? 'revert' : 'none'
}
}
if (element.children.length) walk(element, init)
}
}
}
walk(document.body, true)
State = new Proxy(State, {
set(target, key, value) {
target[key] = value
walk(document.body, false)
},
})
window.addEventListener('popstate', (event) => {
if (event.isTrusted) {
location.pathname = window.location.pathname
}
})