protocol-handlers
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

Protocol Handlers

Utilities to work with protocol handlers on the web.

Why?

While the Navigator API provides methods to .registerProtocolHandler() and .unregisterProtocolHandler(), there's no way to check if a given protocol has a handler. Instead, when trying to open a URL with an unsupported protocol the browser will throw an error that you cannot handle by any normal means (can be handled only in Firefox).

This package fills in the gap and provides API to work with protocol handlers.

Install

npm install protocol-handlers

API

openUrl

Returns a promise that resolves if the given URL has successfuly opened (has a registered handler for its protocol) or rejects if it doesn't have a registered handler.

import { openUrl, UnsupportedProtocolError } from 'protocol-handlers'

// For example, try to open a Visual Studio Code Insiders URI.
// If that application is not installed, handle the rejection.
openUrl('vscode-insiders://resource').catch((error) => {
  if (error instanceof UnsupportedProtocolError) {
    console.log('The "%s" protocol is not supported!', error.protocol)
  }
})

Options

timeout: number

A custom timeout duration.

openUrl('my-app://resource', {
  // Wait for 2s for the navigation modal to appear
  // before rejecting this Promise.
  timeout: 2000,
})

Package Sidebar

Install

npm i protocol-handlers

Weekly Downloads

53

Version

0.1.3

License

MIT

Unpacked Size

5.75 kB

Total Files

11

Last publish

Collaborators

  • kettanaito