-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsi.html
185 lines (166 loc) · 6.06 KB
/
qsi.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Query String Inspector</title>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
</head>
<body class="bg-blue-50 text-gray-700">
<main id="app">
<h1 class="text-4xl m-4">Query String Inspector</h1>
<section class="m-4 flex">
<textarea
class="p-2 font-mono w-full text-gray-500 border-blue-200 border-2 rounded bg-white"
placeholder="Paste query string here"
ref="originalQueryString"
v-model="original"></textarea>
<div class="ml-4 flex flex-col">
<button class="rounded bg-blue-400 text-white p-2 mb-2" @click="refresh">Parse</button>
<button class="rounded bg-blue-400 text-white p-2" @click="copy(original)">Copy</button>
</div>
</section>
<div class="grid grid-cols-2 gap-4">
<section v-if="hasPayload" class="p-4">
<h2 class="text-2xl mb-2">Payload</h2>
<table class="border-2 border-blue-300 w-full">
<tr v-for="key in sortedKeys" :key="key" class="border-2 border-blue-300">
<td class="p-2">
<input type="checkbox" :id="key" :value="key" class="mr-2" v-model="checkedKeys"
@click="interceptCheckboxToggle" />
<label :for="key">{{ key }}</label>
</td>
<td class="p-2">{{ payload[key] || "-" }}</td>
</tr>
</table>
</section>
<section v-if="hasPayload" class="p-4">
<h2 class="text-2xl mb-2">Modified</h2>
<div class="flex">
<textarea
class="p-2 font-mono w-full text-gray-500 border-blue-200 border-2 rounded bg-white"
ref="modifiedQueryString"
readonly>{{ modified }}</textarea>
<div class="ml-4">
<button class="rounded bg-blue-400 text-white p-2" @click="copy(modified)">Copy</button>
</div>
</div>
</section>
</div>
</main>
</body>
</html>
<script type="application/javascript">
const QSI = {
data () {
return {
original: '',
payload: {},
checkedKeys: [],
previousCheckboxSelection: null,
}
},
computed: {
hasPayload () {
return Object.keys(this.payload).length > 0
},
sortedKeys () {
return Object.keys(this.payload).sort()
},
modified () {
return '?' + this.checkedKeys.map(key => {
if (Array.isArray(this.payload[key])) {
return this.payload[key].map(value => {
return key + "=" + value
})
}
return key + "=" + this.payload[key]
}).flat().join('&')
}
},
methods: {
refresh () {
if (this.original.includes('?')) {
const index = this.original.lastIndexOf('?')
this.original = this.original.slice(index)
}
return this.original.startsWith('?')
? window.location.assign(location.pathname + this.original)
: window.location.assign(location.pathname + '?' + this.original)
},
parse (query) {
query = this.decode(query)
this.payload = {}
query.split('&').forEach(entry => {
[key, value] = (entry.startsWith('?') ? entry.substring(1) : entry).split('=')
// An empty key indicates an empty query string
if (key.length == 0) {
return
}
// Collect array values
if (key.endsWith('[]')) {
if (!this.payload[key]) {
this.payload[key] = []
this.checkedKeys.push(key)
}
if (value.length) {
this.payload[key].push(value)
}
}
// Collect plain values
else {
this.payload[key] = value
this.checkedKeys.push(key)
}
this.checkedKeys.sort()
})
},
async copy (s) {
// https://www.raymondcamden.com/2020/03/04/vue-quick-shot-copy-to-the-clipboard
await navigator.clipboard.writeText(s)
alert('Copied!')
},
interceptCheckboxToggle (event) {
// Here we will intercept the checkbox click to see if a modifier key was used.
// Ctrl - modify all checkboxes
if (event.ctrlKey && !event.target.checked) {
this.checkedKeys = []
this.previousCheckboxSelection = null
} else if (event.ctrlKey && event.target.checked) {
this.checkedKeys = this.sortedKeys
this.previousCheckboxSelection = null
}
// Shift - range selection
if (event.shiftKey && this.previousCheckboxSelection) {
const firstIndex = this.sortedKeys.findIndex(el => el === this.previousCheckboxSelection)
const secondIndex = this.sortedKeys.findIndex(el => el === event.target.value)
for (let i = Math.min(firstIndex, secondIndex); i <= Math.max(firstIndex, secondIndex); i++) {
// Are we selecting or deselecting?
if (event.target.checked) {
this.checkedKeys.push(this.sortedKeys[i])
} else {
this.checkedKeys = this.checkedKeys.filter(el => el !== this.sortedKeys[i])
}
}
}
// keep a note of the most recently selected checkbox
this.previousCheckboxSelection = event.target.value
},
// Decode URL parameters
decode (input) {
return decodeURIComponent(input)
}
},
updated () {
this.$refs.modifiedQueryString.style.height = (this.$refs.modifiedQueryString.scrollHeight) + "px"
this.$refs.originalQueryString.style.height = (this.$refs.originalQueryString.scrollHeight) + "px"
},
mounted () {
this.original = this.decode(window.location.search)
this.parse(this.original)
},
}
Vue.createApp(QSI).mount('#app')
</script>