Which GitHub page are you on? Is it an issue? Is it a list? Perfect for your WebExtension or userscript.
Battle-tested in Refined GitHub extension.
- Try the live demo
- See the code and expected URLs for every detection
npm install github-url-detection// This package is pure ESMimport*aspageDetectfrom'github-url-detection';consthref='https://github.com/refined-github/github-url-detection/issues/1';if(pageDetect.isIssue(newURL(href))){// Pass the URL as an `URL` objectalert('The passed URL is of an issue!')}if(pageDetect.isRepo()){// Uses `window.location.href` by defaultalert('You’re looking at a repo!')}if(pageDetect.isIssueOrPRList()){alert('You’re looking at a issues and PRs list!')}Most detections are URL-based while others need access to the current document. You can determine which ones are URL-based by looking at their signature: URL-based functions have a url parameter.
By default, URL-based detections use the location global if you don't pass a url argument.
if(pageDetect.isIssueOrPRList()){alert('You’re looking at a issues or PRs list!')}if(pageDetect.isIssueOrPRList(newURL('https://github.com/refined-github/github-url-detection/pulls'))){alert('You’re looking at a issues or PRs list!')}Notice that the url parameter is not a plain string but it has to be a proper URL or location object.
By default, document-based detections use the document global, which means they can only be used if you have the whole page, you can't just test any random URL string.
if(pageDetect.isOrganizationProfile()){alert('You’re on an organization profile, like https://github.com/babel')}The waitFor helper function allows you to wait for a detection to become true by repeatedly checking it on each animation frame. This is useful for DOM-based detections that need to wait for elements to appear before the document is fully loaded.
import{utils,isOrganizationProfile}from'github-url-detection';asyncfunctioninit(){// Wait for the detection to return true or for the document to be completeif(!awaitutils.waitFor(isOrganizationProfile)){return;// Not an organization profile}// The page is now confirmed to be an organization profileconsole.log('On organization profile!');}The waitFor function:
- Repeatedly calls the detection function on each animation frame
- Stops when the detection returns
trueor whendocument.readyStateis'complete' - Returns the final result of the detection
- Works with any detection function that returns a boolean
- github-reserved-names - Get a list, or check if a user or organization name is reserved by GitHub.
- shorten-repo-url - Shorten GitHub links like GitHub shortens Issues and Commit links.
MIT © Federico Brigante
