|
1 | 1 | <script>
|
2 |
| - const defaultUrl = 'https://github.com/refined-github/github-url-detection'; |
3 |
| - export let url = ''; |
| 2 | +
|
| 3 | + import parseUrl from './parse-url.js'; |
4 | 4 | import * as urlDetection from '../index';
|
5 | 5 | import { getAllUrls } from '../collector';
|
6 | 6 |
|
| 7 | + const defaultUrl = 'https://github.com/refined-github/github-url-detection'; |
| 8 | + const urlParameter = new URLSearchParams(location.search); |
| 9 | + const parsedUrlParameter = parseUrl(urlParameter.get('url'), 'https://github.com').href; |
| 10 | + // Parse partial URL in the parameter so that it's shown as full URL in the field |
| 11 | + let urlField = parsedUrlParameter || ''; |
| 12 | +
|
7 | 13 | const allUrls = [...getAllUrls()].sort();
|
8 | 14 |
|
9 |
| - let isUrlValid; |
10 |
| - $: { |
11 |
| - try { |
12 |
| - new URL(url || defaultUrl); |
13 |
| - isUrlValid = true; |
14 |
| - } catch { |
15 |
| - isUrlValid = false; |
16 |
| - } |
17 |
| - } |
| 15 | + let parsedUrl; |
| 16 | + // Do not use ?? because it should work on empty strings |
| 17 | + $: parsedUrl = parseUrl(urlField || defaultUrl); |
18 | 18 |
|
19 | 19 | let detections = [];
|
20 | 20 | $: {
|
21 |
| - if (isUrlValid) { |
| 21 | + if (parsedUrl) { |
| 22 | + if (urlField) { |
| 23 | + urlParameter.set('url', urlField.replace('https://github.com', '')); |
| 24 | + history.replaceState(null, '', `?${urlParameter}`); |
| 25 | + } else { |
| 26 | + history.replaceState(null, '', location.pathname); |
| 27 | + } |
22 | 28 | detections = Object.entries(urlDetection)
|
23 | 29 | .map(([name, detect]) => {
|
24 | 30 | if (typeof detect !== 'function') {
|
|
29 | 35 | return {
|
30 | 36 | name,
|
31 | 37 | detect,
|
32 |
| - result: detect(new URL(url || defaultUrl)) |
| 38 | + result: detect(parsedUrl) |
33 | 39 | };
|
34 | 40 | } else {
|
35 | 41 | return {name};
|
|
75 | 81 | <span>URL:</span>
|
76 | 82 | <input
|
77 | 83 | type="search"
|
78 |
| - bind:value={url} |
| 84 | + bind:value={urlField} |
79 | 85 | placeholder={defaultUrl}
|
80 | 86 | autocomplete="off"
|
81 | 87 | autocorrect="off"
|
|
88 | 94 | {/each}
|
89 | 95 | </datalist>
|
90 | 96 |
|
91 |
| -{#if isUrlValid} |
| 97 | +{#if parsedUrl} |
92 | 98 | <pre><code>
|
93 | 99 | {#each detections as {name, detect, result} (name)}
|
94 | 100 | {#if detect}
|
|
0 commit comments