Hooks
Choose where your command appears
By default, all commands appear at the Command Bar root. However, in some cases, you may want your command to show up in another context, e.g. when there is an active web page in focus.
Hooks let you achieve this. There is currently only one hook supported – URL Hook.

URL Hook

URL hook allows you to "attach" your command to the "Active Tab" group in the Command Bar. This group appears at the top of the Command Bar root when there is a browser tab in focus.
"Active Tab" group in the Command Bar
When your command is run, the page URL will be passed in the url parameter.
A URL Hook is a JSON object with the following properties:
Property
Description
type
required. The type of the hook. Must be "url".
url
The URL match pattern. Allows showing the command only when the page URL matches this pattern. For example, https://github.com/* matches HTTPS URLs only at "github.com", with any URL path and URL query string. Check out WebExtensions Match Pattern for more examples.

Examples

Any URL
Github URLs only
Show the command for any URL
1
{
2
"type": "url"
3
}
Copied!
Show the command for Github URLs only
1
{
2
"type": "url",
3
"url": "https://github.com/*"
4
}
Copied!

Define Hooks

You can define Hooks using HTTP Headers for Cloud Commands or code comments for Local Commands. In both cases, the hooks must be string representations of the JSON object. You can define a single Hook or multiple Hooks using an array.

Cloud Commands

Your endpoint can define Hooks by sending a custom HTTP header – X-Slapdash-Hooks. For example, in NodeJS this can look like:
1
async function myCommand(req, res) {
2
// Your command logic is here.
3
res.setHeader("Access-Control-Expose-Headers", "X-Slapdash-Hooks");
4
res.setHeader("X-Slapdash-Hooks", JSON.stringify({type: "url"}));
5
}
Copied!
Note, you also need to set Access-Control-Expose-Headers to the list of custom hooks that you want to expose. This ensures Slapdash can access X-Slapdash-Hooks when making a request to your endpoint.

Local Commands

To specify Hooks for Local Commands, you can use code comments in the following format:
command.js
1
// @slapdash.hooks '{"type":"url"}'
2
3
// You command logic is here.
Copied!
Last modified 1mo ago