2018-10-19, updated: 2024-03-12

Tested with Nyxt 2.0.0.

Tags: feature, howto.

Hooks in practice

Hooks in practice

By John Mercouris

Update: This article was updated for Nyxt 2 for which the hook system has been completely rewritten.

Using hooks to smart-program your browser

Hooks are a great way to extend a workflow by triggering actions upon events. Simply put, a hook is an object that holds a list of handlers, which are functions called when the hook is run at a precisely defined point in the program.

In the world of a web browser, there are a ton of events: page loaded, DOM available, page rendered, etc. In addition to the events fired off by normal processing of web pages there are a large set of events which include actions by the user: tab deleted, page bookmarked, command called, mode enabled, etc.

In Nyxt, all these events are hookable.

Hooking into the events fired off by the browser or by the user allows the creation of extendable and tailored workflows.

The Nyxt hook system

Many hooks are as such executed at different points in Nyxt:

For the full list, run describe-variable and describe-slot over the names ending with -hook.

Practical examples

Let's consider a practical example. If you want to force the redirection of a domain to another, you can use the request-resource-hook to change the URL and return a new one. In the example below, we make sure we always visit old.reddit.com instead of the new Reddit interface:

You can ask Nyxt to automatically enable or disable modes depending on the URL, for instance, you can toggle the proxy mode per domain, which can be very convenient if you would like to, say, disable Tor for some resource intensive domains.

Another cool example would be automatically downloading any YouTube video we visit:

Adjust it to your taste!

All user commands have hooks

One feature that makes Nyxt unique is the ability to extend commands exposed to the user.

Because the Common Lisp language allows it, one could replace a command definition by another function. This is however not the recommended approach, most notably because it could break the built-in behavior.

We can then use the "before" and "after" hooks to extend the built-in commands. It is as simple as defining a new function with no parameters:

and adding it to the list of hooks:

And voila!

Conclusions

In summary, hooks present a very simple and effective mechanism to chain behavior in your workflows. We are looking forward to seeing what you can create with them!

For more about details on the hook internals and advanced usage, see our other article.

Thanks for reading!


Did you enjoy this article? Register for our newsletter to receive the latest hacker news from the world of Lisp and browsers!