|
1 | 1 | import * as pageDetect from 'github-url-detection'; |
2 | 2 |
|
| 3 | +import delay from '../helpers/delay.js'; |
3 | 4 | import features from '../feature-manager.js'; |
4 | 5 |
|
5 | | -function maybeCleanUrl(event?: NavigateEvent): void { |
6 | | - // TODO [2027-01-01]: Drop setInterval and optional chaining, it's only needed to support Safari <26.2 |
7 | | - const parsed = new URL(event?.destination.url ?? location.href); |
8 | | - if (parsed.searchParams.get('tab') === 'readme-ov-file') { |
9 | | - parsed.searchParams.delete('tab'); |
10 | | - history.replaceState(history.state, '', parsed.href); |
| 6 | +async function maybeCleanUrl(): Promise<void> { |
| 7 | + const parsed = new URL(location.href); |
| 8 | + if (parsed.searchParams.get('tab') !== 'readme-ov-file') { |
| 9 | + return; |
11 | 10 | } |
| 11 | + |
| 12 | + // GitHub has some delayed logic to deal with this internally |
| 13 | + // https://github.com/refined-github/refined-github/issues/9908 |
| 14 | + await delay(500); |
| 15 | + parsed.searchParams.delete('tab'); |
| 16 | + history.replaceState(history.state, '', parsed.href); |
12 | 17 | } |
13 | 18 |
|
14 | 19 | function init(signal: AbortSignal): void { |
15 | | - maybeCleanUrl(); |
16 | | - let interval: NodeJS.Timeout; |
17 | | - if ('navigation' in globalThis) { |
18 | | - navigation.addEventListener('navigate', maybeCleanUrl, {signal}); |
19 | | - } else { |
20 | | - interval = setInterval(() => { |
21 | | - maybeCleanUrl(); |
22 | | - }, 1000); |
23 | | - signal.addEventListener('abort', () => { |
24 | | - clearInterval(interval); |
25 | | - }); |
26 | | - } |
| 20 | + void maybeCleanUrl(); |
| 21 | + |
| 22 | + // TODO [2027-01-01]: Only needed to avoid breaking on Safari <26.2 (<2026) |
| 23 | + navigation?.addEventListener('navigatesuccess', maybeCleanUrl, {signal}); |
27 | 24 | } |
28 | 25 |
|
29 | 26 | void features.add(import.meta.url, { |
|
0 commit comments