summaryrefslogtreecommitdiff
path: root/source/github-helpers/get-current-git-ref.ts
diff options
context:
space:
mode:
authorGravatar Federico Brigante <me@fregante.com> 2023-05-10 22:58:08 +0800
committerGravatar GitHub <noreply@github.com> 2023-05-10 22:58:08 +0800
commit579afb1fae65bc4cd7ef22e6526fce510133d731 (patch)
treebcc780181157d0f0d5fb2bef23f00bc5e6bcda00 /source/github-helpers/get-current-git-ref.ts
parent1f1045fb2f25580c76ad23dd2312eafb2c9a4073 (diff)
downloadrefined-github-579afb1fae65bc4cd7ef22e6526fce510133d731.tar.gz
refined-github-579afb1fae65bc4cd7ef22e6526fce510133d731.tar.zst
refined-github-579afb1fae65bc4cd7ef22e6526fce510133d731.zip
Default branch and current branch detector improvements (#6636)
Diffstat (limited to 'source/github-helpers/get-current-git-ref.ts')
-rw-r--r--source/github-helpers/get-current-git-ref.ts48
1 files changed, 39 insertions, 9 deletions
diff --git a/source/github-helpers/get-current-git-ref.ts b/source/github-helpers/get-current-git-ref.ts
index 4eefbabb..71b27222 100644
--- a/source/github-helpers/get-current-git-ref.ts
+++ b/source/github-helpers/get-current-git-ref.ts
@@ -1,10 +1,30 @@
-import {getCurrentBranchFromFeed} from './index.js';
+import {isRepoCommitList} from 'github-url-detection';
+import select from 'select-dom';
+
+import {branchSelector} from './selectors.js';
const typesWithGitRef = new Set(['tree', 'blob', 'blame', 'edit', 'commit', 'commits', 'compare']);
const titleWithGitRef = / at (?<branch>[.\w-/]+)( ยท [\w-]+\/[\w-]+)?$/i;
-/** This only works with URL and page title. Must not be async because it's used by GitHubURL */
-export default function getCurrentGitRef(pathname = location.pathname, title = document.title): string | undefined {
+/** Must not be async because it's used by GitHubURL. May return different results depending on whether it's called before or after DOM ready */
+export default function getCurrentGitRef(): string | undefined {
+ // Note: This is not in the <head> so it's only available on AJAXed loads.
+ // It appears on every Code page except `commits` on folders/files
+ const refViaPicker = select(branchSelector)?.textContent!.trim();
+ if (refViaPicker) {
+ return refViaPicker;
+ }
+
+ // Slashed branches on `commits`, including pages without a branch picker
+ const branchFromFeed = getCurrentBranchFromFeed();
+ if (branchFromFeed) {
+ return branchFromFeed;
+ }
+
+ return getGitRef(location.pathname, document.title);
+}
+
+export function getGitRef(pathname: string, title: string): string | undefined {
if (!pathname.startsWith('/')) {
throw new TypeError(`Expected pathname starting with /, got "${pathname}"`);
}
@@ -15,11 +35,6 @@ export default function getCurrentGitRef(pathname = location.pathname, title = d
return;
}
- // Slashed branches on `commits`
- if (type === 'commits') {
- return getCurrentBranchFromFeed()!;
- }
-
// Slashed branches on `blob` and `tree`
const parsedTitle = titleWithGitRef.exec(title);
if (parsedTitle) {
@@ -27,6 +42,21 @@ export default function getCurrentGitRef(pathname = location.pathname, title = d
}
// Couldn't ensure it's not slashed, so we'll return the first piece whether correct or not
- // TODO: extract from `ref-selector` if available https://github.com/refined-github/refined-github/issues/6557
return gitRefIfNonSlashed;
}
+
+// In <head>, but not reliable https://github.com/refined-github/refined-github/assets/1402241/50357d94-505f-48dc-bd54-74e86b19d642
+function getCurrentBranchFromFeed(): string | undefined {
+ const feedLink = isRepoCommitList() && select('link[type="application/atom+xml"]');
+ if (!feedLink) {
+ // Do not throw errors, the element may be missing after AJAX navigation even if on the right page
+ return;
+ }
+
+ return new URL(feedLink.href)
+ .pathname
+ .split('/')
+ .slice(4) // Drops the initial /user/repo/route/ part
+ .join('/')
+ .replace(/\.atom$/, '');
+}