npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mapbox/link-hijacker

v1.1.0

Published

Hijack clicks on and within links, probably for client-side routing

Downloads

1,268

Readme

@mapbox/link-hijacker

Build Status

Hijack clicks on and within links, probably for client-side routing.

Pirates hijacking a ship

Imagine you're using client-side routing on your website, because you live in The Future. You want nice, smooth, fast client-side routing whenever a link points to a client-side route (regardless of whether the code author remembered this). And you want a regular old page transition whenever a link does not point to a client-side route. The easiest way to do this would be to use regular <a> elements all over your site, for both of these types of links. That would be convenient, and also wouldn't force you or others to pay attention to which links should do client-side routing and which ones should not. That seems like a better situation than, for example, using a special component to distinguish between regular and client-side links.

link-hijacker provides the means to do this by hijacking all clicks on and within links, allowing you to determine how they are treated.

  • Listens for clicks.
  • Determines which clicks are on or within <a> elements with href attributes.
  • Determines whether those links should be hijacked, based on your options.
  • If a link should be hijacked, prevents default behavior and calls your callback.
  • In your callback, you might programmatically change pages.

This pattern has been implemented before, because it's clearly useful (see "Similar work"). But there didn't seem to be a full-featured, well-tested, and actively maintained implementation that was not tied to a larger library. So we made this one.

Typical example

const linkHijacker = require('@mapbox/link-hijacker');

const unhijack = linkHijacker.hijack((clickedLink, clickEvent) => {
  // Determine whether the link points to a client-side route ...
  if (linkPointsToClientSideRoute) {
    // Use JS for to programmatically change the page ...
  } else {
    // Or else allow it to work like a regular link.
    window.location.assign(clickedLink);
  }
});

// Later, you can unhijack links.
unhijack();

You can now use <a> elements indiscriminately.

API

hijack

linkHijacker.hijack([options], callback)

Returns a function that can be used to remove event listeners, unhijacking links. Calls the callback whenever a link is hijacked.

options

root

Type: HtmlElement. Default: document.documentElement.

Links will be hijacked within this element.

skipModifierKeys

Type: boolean. Default: true.

By default, clicks paired with modifiers keys (ctrlKey, altKey, metaKey, shiftKey) are not hijacked. If this option is false, these clicks will be hijacked.

skipDownload

Type: boolean. Default: true.

By default, links with the download attribute are not hijacked. If this option is false, these links will be hijacked.

skipTargetBlank

Type: boolean. Default: true.

By default, links with the attribute target="_blank" are not hijacked. If this option is false, these links will be hijacked.

skipExternal

Type: boolean. Default: true.

By default, links with the attribute rel="external" are not hijacked. If this option is false, these links will be hijacked.

skipMailTo

Type: boolean. Default: true.

By default, links whose href attributes start with mailto: are not hijacked. If this option is false, these links will be hijacked.

skipOtherOrigin

Type: boolean. Default: true.

By default, links pointing to other origins (protocol + domain) are not hijacked. If this option is false, these links will be hijacked.

skipFragment

Type: boolean. Default: true.

By default, links with href attributes starting with fragments (e.g. href="#foo") are not hijacked. (Links with href attributes that include fragments, but don't start with them, will still be hijacked, e.g. href="some/page#foo".) If this option is false, these links will be hijacked.

skipFilter

Type: Function.

A filter function that receives the clicked link element and returns a truthy or falsey value indicating whether the link should be hijacked or not. If it returns a falsey value, the link will be hijacked. If the function returns a truthy value, the link will not be hijacked.

preventDefault

Type: boolean. Default: true.

By default, event.preventDefault() will be called on any click events that are hijacked (are not skipped). If this option is false, event.preventDefault() will not be called. You could let the event continue as normal, or prevent default behavior yourself.

callback

Type: Function. Required.

Whenever a link is clicked, the callback will be invoked with two arguments:

  • link: The link element that was clicked on or within.
  • event: The click event.

Similar work