11---
22import { Icon } from ' @astrojs/starlight/components' ;
3+ import { Tabs , TabItem } from ' @astrojs/starlight/components' ;
34---
45
56<script >
@@ -8,22 +9,88 @@ import { Icon } from '@astrojs/starlight/components';
89
910 document.addEventListener('DOMContentLoaded', init);
1011
11- function init() {
12- const input = document.querySelector('#input');
13- const markup = document.querySelector('#markup');
14- const output = document.querySelector('#output');
12+ let markup, input, output, reminder, virtualConsole, url;
1513
16- const virtualConsole = new VirtualConsole(output);
14+ function init() {
15+ virtualConsole = new VirtualConsole(output);
16+ input = document.querySelector('#input');
17+ output = document.querySelector('#output');
18+ reminder = document.querySelector('#reminder');
19+ markup = document.querySelector('#markup');
20+ url = document.querySelector('#url');
21+
22+ initUrl();
23+ virtualConsole = new VirtualConsole(output);
1724
1825 input.addEventListener('submit', e => {
1926 try {
20- run(markup.value, virtualConsole );
27+ handleSubmit( );
2128 } catch (e) {
22- console.error(e);
29+ console.error('Capo error', e);
2330 }
2431 e.preventDefault();
32+ return false;
2533 });
2634 }
35+
36+ async function initUrl() {
37+ const thisUrl = new URL(location.href);
38+ const urlParam = thisUrl.searchParams.get('url');
39+ if (!urlParam) {
40+ return;
41+ }
42+ url.value = urlParam;
43+ document.querySelector('a[role=tab]:not([aria-selected])').click();
44+ try {
45+ await handleSubmit();
46+ } catch (e) {
47+ console.error('Capo error', e);
48+ }
49+ }
50+
51+ function setUrl(urlParam) {
52+ const thisUrl = new URL(location.href);
53+ thisUrl.searchParams.set('url', urlParam);
54+ history.replaceState(null, null, thisUrl);
55+ }
56+
57+ async function handleSubmit() {
58+ reminder.classList.remove('highlight');
59+ const html = await getHtml(virtualConsole);
60+ virtualConsole.clear();
61+ run(html, virtualConsole);
62+ reminder.classList.add('highlight');
63+ }
64+
65+ async function getHtml() {
66+ let html;
67+ const selectedTab = document.querySelector('a[role=tab][aria-selected=true]').innerText;
68+
69+ if (selectedTab === 'URL') {
70+ if (!url.value || !url.validity.valid) {
71+ throw new Error(`Invalid URL "${url.value}"`);
72+ }
73+ if (url.value != url.getAttribute('placeholder')) {
74+ setUrl(url.value);
75+ }
76+ html = await getStaticHTML(url.value);
77+ } else {
78+ html = markup.value;
79+ }
80+
81+ if (!html) {
82+ throw new Error('Unable to run. Invalid HTML.');
83+ }
84+
85+ return html;
86+ }
87+
88+ async function getStaticHTML(url) {
89+ const proxy = new URL('https://capo.rviscomi.workers.dev/');
90+ proxy.searchParams.set('url', url);
91+ const response = await fetch(proxy);
92+ return await response.text();
93+ }
2794</script >
2895
2996<style >
@@ -58,11 +125,67 @@ import { Icon } from '@astrojs/starlight/components';
58125 vertical-align: middle;
59126 margin-left: 0.5rem;
60127 }
128+
129+ input {
130+ width: 100%;
131+ }
132+
133+ input:invalid {
134+ border-color: #9e0142;
135+ }
136+
137+ #output .warn {
138+ background-color: rgba(255, 159, 67, 0.1);
139+ color: orange;
140+ }
141+
142+ #output .error {
143+ background-color: rgba(255, 99, 132, 0.1);
144+ color: red;
145+ }
146+
147+ #reminder {
148+ transition: background-color 0.5s;
149+ }
150+
151+ #reminder.highlight {
152+ animation: highlight 1s ease-in-out;
153+ }
154+
155+ @keyframes highlight {
156+ 0% {
157+ background-color: transparent;
158+ }
159+ 10% {
160+ background-color: rgba(255, 255, 0, 0.4);
161+ }
162+ end {
163+ background-color: transparent;
164+ }
165+ }
61166</style >
62167
168+ <p >To see capo.js in action, enter HTML or a URL in the form field below and click <strong >Run</strong >.</p >
169+
170+ <p ><strong id =" reminder" >Results are logged to the DevTools console.</strong ></p >
171+
63172<form id =" input" >
64- <textarea id =" markup" placeholder =" Enter HTML" >
65- <title >test</title >
66- </textarea >
173+ <Tabs >
174+ <TabItem label =" HTML" >
175+ <textarea id =" markup" placeholder =" Enter HTML" >
176+ <title >test</title >
177+ <meta charset =" utf-8" >
178+ <meta name =" description" content =" Try capo.js" >
179+ <style ></style >
180+ <meta http-equiv =" origin-trial" content =" AuNyVoVDAnYrBa2cL89WmgDSi1Os1UAt4SmcY1vXSJKDlIlBNfD4SEpIfg3LNDexEWv6N2kHnJ17MT4cVmRhQgIAAABueyJvcmlnaW4iOiJodHRwczovL3J2aXNjb21pLmdpdGh1Yi5pbzo0NDMiLCJmZWF0dXJlIjoiQmFja0ZvcndhcmRDYWNoZU5vdFJlc3RvcmVkUmVhc29ucyIsImV4cGlyeSI6MTY5MTUzOTE5OX0=" >
181+ <script ></script >
182+ </textarea >
183+ </TabItem >
184+ <TabItem label =" URL" >
185+ <input id =" url" type =" url" value =" https://www.example.com/" placeholder =" https://www.example.com/" >
186+ </TabItem >
187+ </Tabs >
67188 <button >Run <Icon name =" rocket" color =" inherit" size =" 1.5rem" /></button >
68189</form >
190+
191+ <div id =" output" ></div >
0 commit comments