| 13 | } |
| 14 | |
| 15 | async function updateLink(link: HTMLAnchorElement): Promise<void> { |
| 16 | if (link.host !== location.host) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | // Avoid conflict with `new-tab-links` in New Issue modal |
| 21 | // https://github.com/refined-github/refined-github/pull/9640/changes#r3328459390 |
| 22 | if (link.matches(newIssueModalDeadLinks)) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | // Exclude /pulls/ global pages since they're already sorted by update time #9604 |
| 27 | // This also breaks the feature on the header button regardless of the PR inbox beta status. This is because the feature breaks the inbox, overriding the user's preference. |
| 28 | if (/^[/]pulls[/]\w+$/.test(link.pathname) || link.matches('[href="/pulls"][class*="appHeaderButton"]')) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Pick only links to lists, not single issues |
| 33 | // + skip pagination links |
| 34 | // + skip pr/issue filter dropdowns (some are lazyloaded) |
| 35 | if (pageDetect.isIssueOrPRList(link)) { |
| 36 | // Avoid rewriting /labels/ URLs until the last moment |
| 37 | // https://github.com/refined-github/refined-github/issues/7205 |
| 38 | if (pageDetect.isRepoTaxonomyIssueOrPRList(link)) { |
| 39 | await oneEvent(link, 'click', {filter: event => (event as MouseEvent).which < 2}); |
| 40 | } |
| 41 | |
| 42 | saveOriginalHref(link); |
| 43 | |
| 44 | const newUrl = SearchQuery.from(link).prepend('sort:updated-desc').href; |
| 45 | |
| 46 | // Preserve relative attributes as such #5435 |
| 47 | const isRelativeAttribute = link.getAttribute('href')!.startsWith('/'); |
| 48 | link.href = isRelativeAttribute ? newUrl.replace(location.origin, '') : newUrl; |
| 49 | } |
| 50 | |
| 51 | // Also sort projects #4957 |
| 52 | if (pageDetect.isProjects()) { |
| 53 | saveOriginalHref(link); |
| 54 | |
| 55 | // Projects use a different parameter name so don't use SearchQuery |
| 56 | const search = new URLSearchParams(link.search); |
| 57 | const query = search.get('query') ?? 'state:open'; // Default value query is missing |
| 58 | search.set('query', `sort:updated-desc ${query}`); |
| 59 | link.search = search.toString(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | function init(signal: AbortSignal): void { |
| 64 | // Get links that don't already have a specific sorting or pagination applied |