forked from WICG/attribution-reporting-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-headers.html
160 lines (137 loc) · 3.57 KB
/
validate-headers.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
<!DOCTYPE html>
<meta charset=utf-8>
<title>Attribution Reporting Header Validation</title>
<script type=module src="validate-json.mjs"></script>
<style>
* {
box-sizing: border-box;
}
html {
height: 100vh;
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 1em;
}
body,
form,
textarea,
#output {
height: 100%;
width: 100%;
}
body,
form,
#right {
display: flex;
}
body,
#right {
flex-direction: column;
}
#header {
flex: none;
}
#input {
flex: 1;
}
textarea {
resize: none;
}
</style>
<body>
<h1>Attribution Reporting Header Validation</h1>
<form>
<noscript>JavaScript is required to use this form.</noscript>
<fieldset id=input>
<legend>JSON</legend>
<textarea></textarea>
</fieldset>
<div id="right">
<fieldset id=header>
<legend>Header</legend>
<p><label><input type=radio name=header value=source><code>Attribution-Reporting-Register-Source</code></label>
<p><label><input type=radio name=header value=trigger><code>Attribution-Reporting-Register-Trigger</code></label>
</fieldset>
<fieldset id=output>
<legend>Validation Result</legend>
<button type=button id=linkify>Copy Link</button>
<output>
<div id=success></div>
<p>Errors:
<ul id=errors></ul>
<p>Warnings:
<ul id=warnings></ul>
</output>
</fieldset>
</div>
</form>
<template id="pathful-issue">
<li><code></code> <span></span></li>
</template>
<script type=module>
import {validateJSON, validateSource, validateTrigger} from './validate-json.mjs';
const form = document.querySelector('form');
const input = form.querySelector('textarea');
const errorList = document.querySelector('#errors');
const warningList = document.querySelector('#warnings');
const successDiv = document.querySelector('#success');
const pathfulTmpl = document.querySelector('#pathful-issue');
const pathPart = p =>
typeof p === 'string' ? `["${p}"]` : `[${p}]`;
const makeLi = ({path, msg}) => {
let li;
if (path instanceof Array) {
if (path.length === 0) {
li = document.createElement('li');
li.textContent = `JSON root ${msg}`;
} else {
li = pathfulTmpl.content.cloneNode(true);
li.querySelector('code').textContent = path.map(pathPart).join('');
li.querySelector('span').textContent = msg;
}
} else {
li = document.createElement('li');
li.textContent = msg;
}
return li;
};
const header = () => form.elements['header'].value;
form.addEventListener('input', () => {
const validate = header() === 'source'
? validateSource
: validateTrigger;
const result = validateJSON(input.value, validate);
const successEl = document.createElement('div');
if (result?.errors?.length === 0 && result?.warnings?.length === 0) {
successEl.textContent = 'The header is valid.';
} else {
successEl.textContent = '';
}
successDiv.replaceChildren(successEl);
errorList.replaceChildren(...result.errors.map(makeLi));
warningList.replaceChildren(...result.warnings.map(makeLi));
});
document.querySelector('#linkify').addEventListener('click', async () => {
const url = new URL(location);
url.search = '';
url.searchParams.set('header', header());
url.searchParams.set('json', input.value);
await navigator.clipboard.writeText(url.toString());
});
const params = new URLSearchParams(location.search);
const json = params.get('json');
if (json) {
input.value = json;
}
switch (params.get('header')) {
case 'trigger':
form.querySelector('input[value=trigger]').click();
break;
default:
form.querySelector('input[value=source]').click();
break;
}
</script>