GithubHelp home page GithubHelp logo

kieferro / github-url-detection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from refined-github/github-url-detection

0.0 0.0 0.0 2.43 MB

Which GitHub page are you on? Is it an issue? Is it a list? Perfect for your WebExtension or userscript.

Home Page: https://npm.im/github-url-detection

License: MIT License

TypeScript 100.00%

github-url-detection's Introduction

github-url-detection

Which GitHub page are you on? Is it an issue? Is it a list? Perfect for your WebExtension or userscript.

Battle-tested by and extracted from the Refined GitHub extension.

Install

npm install github-url-detection
// This package is pure ESM
import * as pageDetect from 'github-url-detection';

Usage

const href = 'https://github.com/refined-github/github-url-detection/issues/1';
if (pageDetect.isIssue(new URL(href))) { // Pass the URL as an `URL` object
	alert('The passed URL is of an issue!')
}

if (pageDetect.isRepo()) { // Uses `window.location.href` by default
	alert('You’re looking at a repo!')
}

if (pageDetect.isIssueOrPRList()) {
	alert('You’re looking at a issues and PRs list!')
}

API

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.

URL-based detections

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(new URL('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.

Document-based detections

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')
}

Related

  • 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.

License

MIT © Federico Brigante

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.