-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathjsonpath-tool.html
More file actions
197 lines (183 loc) · 4.97 KB
/
jsonpath-tool.html
File metadata and controls
197 lines (183 loc) · 4.97 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
<!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 {
font-size: medium;
}
#jsonpath-result {
background-color: #eee;
flex-grow: 1;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h2>uBO-flavored JSONPath tool</h2>
<main>
<section>
</section>
<input id="jsonpath-input" placeholder="JSON path expression" spellcheck="false" value="$..book[?@.price<10]" />
<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';
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');
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);
cmMergeView.b.dispatch({
changes: {
from: 0, to: cmMergeView.b.state.doc.length,
insert: bText,
},
});
}
const jsonp = new JSONPath();
let jsonDataIn = {};
let processTimer;
const cmMergeView = self.cm6.createMergeView({
aDoc: aLastText,
aUpdateListener: info => {
if ( info.docChanged === false ) { 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.addEventListener('input', ( ) => {
process();
});
}
process();
</script>
</body>
</html>