forked from gorhill/uBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonpath-tool.html
More file actions
243 lines (227 loc) · 6.64 KB
/
Copy pathjsonpath-tool.html
File metadata and controls
243 lines (227 loc) · 6.64 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<!--
Requires a local server in root of uBlock repo:
python3 -m http.server
Then load the following URL in the browser:
http://localhost:8000/tools/jsonpath-tool.html
-->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JSONPath tool</title>
<style>
body {
box-sizing: border-box;
display: flex;
flex-direction: column;
font-size: 16px;
gap: 0;
height: 100vh;
margin: 0;
padding: 0.5em;
width: 100vw;
}
h2 {
margin: 0;
}
main {
box-sizing: border-box;
display: flex;
flex-direction: column;
flex-grow: 1;
gap: 0.5em;
}
section {
box-sizing: border-box;
display: flex;
gap: 0.5em;
max-height: 80cqh;
overflow: auto;
}
section > * {
border: 1px solid gray;
box-sizing: border-box;
flex-grow: 1;
font-family: monospace;
font-size: small;
}
section .cm-lineWrapping {
word-break: break-all !important;
}
#jsondata-in {
min-height: 50vh;
resize: vertical;
}
#jsonpath-input {
display: flex;
gap: 1em;
}
#jsonpath-input > input {
font-size: medium;
flex-grow: 1;
}
#jsonpath-input > button {
font-size: larger;
}
#jsonpath-input > button::after {
content: '\21C8';
}
section.collapsed + #jsonpath-input > button::after {
content: '\21CA';
}
#jsonpath-result {
background-color: #eee;
flex-grow: 1;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h2>uBO-flavored JSONPath tool</h2>
<main>
<section></section>
<div id="jsonpath-input">
<input placeholder="JSON path expression" spellcheck="false" value="$..book[[email protected]<10]" />
<button type="button"></button>
</div>
<div id="jsonpath-result"> </div>
</main>
<script src="../platform/mv3/extension/lib/codemirror/codemirror-ubol/dist/cm6.bundle.ubol.min.js"></script>
<script type="module">
// Requires a local server in root of repo:
// python3 -m http.server
//
// Then load the following URL in the browser:
// http://localhost:8000/tools/jsonpath-tool.html
import { JSONPath } from '../src/js/jsonpath.js';
//import { JSONPath } from 'https://raw.rawgit.net/gorhill/uBlock/refs/tags/1.71.0/src/js/jsonpath.js';
let aLastText = JSON.stringify({
store: {
book: [ {
category: "reference",
author: "Nigel Rees",
title: "Sayings of the Century",
price: 8.95
}, {
category: "fiction",
author: "Evelyn Waugh",
title: "Sword of Honour",
price: 12.99
}, {
category: "fiction",
author: "Herman Melville",
title: "Moby Dick",
isbn: "0-553-21311-3",
price: 8.99
}, {
category: "fiction",
author: "J. R. R. Tolkien",
title: "The Lord of the Rings",
isbn: "0-395-19395-8",
price: 22.99
} ],
bicycle: {
color: "red",
price: 19.95
}
}
}, null, 2);
function readJSON() {
const textarea = document.querySelector('#jsondata-in');
let data;
try {
const text = cmMergeView.a.state.doc.toString();
data = JSON.parse(text);
} catch {
data = {};
}
if ( typeof data !== 'object' || data === null ) {
data = {};
}
return data;
}
function formatResult(a) {
if ( a === undefined ) { return 'undefined'; }
if ( jsonp.valid === false ) { return 'bad expression'; }
if ( Array.isArray(a) === false ) {
return JSON.stringify(a, null, 2);
}
const out = [];
for ( const i of a ) {
out.push(`[ ${i.map(j => JSON.stringify(j)).join(', ')} ]`);
}
return out.join('\n');
}
function process() {
const input = document.querySelector('#jsonpath-input > input');
const jsonpath = input.value;
jsonp.compile(jsonpath);
const jsonDataIn = readJSON();
const left = formatResult(jsonp.evaluate(jsonDataIn));
const pathsDiv = document.querySelector('#jsonpath-result');
pathsDiv.textContent = left;
const jsonDataOut = readJSON();
const objAfter = jsonp.apply(jsonDataOut);
const bText = JSON.stringify(objAfter !== undefined ? objAfter : jsonDataOut, null, 2);
collapse(false);
cmMergeView.b.dispatch({
changes: {
from: 0, to: cmMergeView.b.state.doc.length,
insert: bText,
},
});
}
function collapse(afterState) {
const section = document.querySelector('main > section');
const beforeState = section.classList.contains('collapsed');
afterState ??= !beforeState;
if ( afterState === beforeState ) { return; }
section.classList.toggle('collapsed', afterState);
if ( afterState ) {
cmMergeView.reconfigure({ collapseUnchanged: { } });
} else {
cmMergeView.reconfigure({ collapseUnchanged: undefined });
}
}
const jsonp = new JSONPath();
let jsonDataIn = {};
let processTimer;
const cmMergeView = self.cm6.createMergeView({
aDoc: aLastText,
aUpdateListener: info => {
if ( info.docChanged === false ) { return; }
for ( const transaction of info.transactions ) {
if ( transaction.isUserEvent('input.paste') === false ) { continue; }
const { doc } = info.state;
const before = doc.toString();
let insert;
try { insert = JSON.stringify(JSON.parse(before), null, 2); }
catch { }
if ( Boolean(insert) === false ) { break; }
collapse(false);
info.view.dispatch({ changes: { from: 0, to: doc.length, insert } });
return;
}
if ( processTimer !== undefined ) { return; }
processTimer = setTimeout(( ) => {
processTimer = undefined;
const aNewText = info.state.doc.toString();
if ( aNewText === aLastText ) { return; }
aLastText = aNewText;
process();
}, 71);
},
}, document.querySelector('section'));
{
const input = document.querySelector('#jsonpath-input > input');
input.addEventListener('input', ( ) => { process(); });
}
{
const button = document.querySelector('#jsonpath-input > button');
button.addEventListener('click', ( ) => { collapse(); });
}
process();
</script>
</body>
</html>