2016-11-04 20:25:32 +02:00
# Hooks
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
Hooks exist so that users can globally hook into certain Video.js lifecycle moments.
2016-11-04 20:25:32 +02:00
2016-12-20 23:55:59 +02:00
## Table of Contents
* [Current Hooks ](#current-hooks )
* [beforesetup ](#beforesetup )
* [setup ](#setup )
* [Usage ](#usage )
* [Adding ](#adding )
* [Getting ](#getting )
* [Removing ](#removing )
2016-11-04 20:25:32 +02:00
## Current Hooks
2016-12-20 23:55:59 +02:00
2017-10-07 03:01:54 +02:00
Currently, the following hooks are available:
2016-11-04 20:25:32 +02:00
### beforesetup
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
`beforesetup` occurs just before a player is created. This allows:
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
* Modification of the options passed to the Video.js function (e.g., `videojs('some-id, options)` ).
* Modification of the DOM video element that will be used for the player that will be created.
2016-11-04 20:25:32 +02:00
`beforesetup` hook functions should:
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
* Take two arguments:
1. `videoEl` : DOM `<video>` element that Video.js is going to use to create a player.
1. `options` : The options object that Video.js was called with and will be passed to the player during creation.
* Return an options object that will be merged with the originally provided options.
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
#### Example
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
```js
2017-10-30 23:56:21 +02:00
videojs.hook('beforesetup', function(videoEl, options) {
// videoEl will be the video element with id="some-id" since that
// gets passed to videojs() below. On subsequent calls, it will be
// different.
2016-11-04 20:25:32 +02:00
2016-12-20 23:55:59 +02:00
videoEl.className += ' some-super-class';
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
// autoplay will be true here, since we passed it as such.
if (options.autoplay) {
2016-12-20 23:55:59 +02:00
options.autoplay = false
}
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
// Options that are returned here will be merged with old options.
//
// In this example options will now be:
// {autoplay: false, controls: true}
//
// This has the practical effect of always disabling autoplay no matter
// what options are passed to videojs().
2016-12-20 23:55:59 +02:00
return options;
2017-10-30 23:56:21 +02:00
});
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
// Create a new player.
2016-12-20 23:55:59 +02:00
videojs('some-id', {autoplay: true, controls: true});
2016-11-04 20:25:32 +02:00
```
### setup
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
`setup` occurs just after a player is created. This allows:
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
* Plugins or other custom functionality to initialize on the player.
* Changes to the player object itself.
2016-11-04 20:25:32 +02:00
`setup` hook functions:
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
* Take one argument:
1. `player` : the player that Video.js created
2016-11-04 20:25:32 +02:00
* Don't have to return anything
2017-10-30 23:56:21 +02:00
#### Example
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
```js
2017-10-30 23:56:21 +02:00
videojs.registerPlugin('foo', function() {
// This basic plugin will add the "some-super-class" class to a player.
this.addClass('some-super-class');
});
videojs.hook('setup', function(player) {
// Initialize the foo plugin after any player is created.
player.foo();
});
// Create a new player.
videojs('some-id', {autoplay: true, controls: true});
2016-11-04 20:25:32 +02:00
```
## Usage
### Adding
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
Hooks can be added using `videojs.hook(<name>, function)` before running the `videojs()` function.
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
#### Example
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
```js
2016-12-20 23:55:59 +02:00
videojs.hook('beforesetup', function(videoEl, options) {
2017-10-30 23:56:21 +02:00
// This hook will be called twice. Once for "vid1" and once for "vid2".
// The options will match what is passed to videojs() for each of them.
2016-12-20 23:55:59 +02:00
});
2017-10-30 23:56:21 +02:00
2016-12-20 23:55:59 +02:00
videojs.hook('setup', function(player) {
2017-10-30 23:56:21 +02:00
// This hook will be called twice. Once for "vid1" and once for "vid2".
// The player value will be the player that is created for each element.
2016-12-20 23:55:59 +02:00
});
2017-10-30 23:56:21 +02:00
videojs('vid1', {autoplay: false});
videojs('vid2', {autoplay: true});
2016-11-04 20:25:32 +02:00
```
2017-10-30 23:56:21 +02:00
After adding your hooks, they will automatically be run at the correct time in the Video.js lifecycle.
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
### Adding Once
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
In some cases, you may only want your hook to run once. In these cases, use `videojs.hookOnce(<name>, function)` before running the `videojs()` function.
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
#### Example
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
```js
2017-10-30 23:56:21 +02:00
videojs.hookOnce('beforesetup', function(videoEl, options) {
// This hook will be called once for "vid1", but not for "vid2".
// The options will match what is passed to videojs().
});
videojs.hookOnce('setup', function(player) {
// This hook will be called once for "vid1", but not for "vid2".
// The player value will be the player that is created for each element.
});
videojs('vid1', {autoplay: false});
videojs('vid2', {autoplay: true});
2016-11-04 20:25:32 +02:00
```
2017-10-30 23:56:21 +02:00
### Getting
2016-12-20 23:55:59 +02:00
2017-10-30 23:56:21 +02:00
To access the array of functions that currently exists for any hook, use the `videojs.hooks` function.
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
#### Example
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
```js
2017-10-30 23:56:21 +02:00
// Get an array of all the 'beforesetup' hooks.
var beforeSetupHooks = videojs.hooks('beforesetup');
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
// Get an array of all the 'setup' hooks.
var setupHooks = videojs.hooks('setup');
2016-11-04 20:25:32 +02:00
```
2017-10-30 23:56:21 +02:00
### Removing
To stop hooks from being executed during any future Video.js lifecycles you can remove them using `videojs.removeHook` .
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
#### Example
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
```js
2017-10-30 23:56:21 +02:00
var beforeSetup = function(videoEl, options) {};
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
// Add the hook.
videojs.hook('beforesetup', beforeSetup);
2016-11-04 20:25:32 +02:00
2017-10-30 23:56:21 +02:00
// Remove the same hook.
videojs.removeHook('beforesetup', beforeSetup);
2016-11-04 20:25:32 +02:00
```