2016-11-04 20:25:32 +02:00
# Hooks
2016-12-20 23:55:59 +02:00
2017-08-14 23:02:48 +02:00
Hooks exist so that users can "hook" on to certain Video.js player lifecycle
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
2016-11-04 20:25:32 +02:00
`beforesetup` is called just before the player is created. This allows:
2016-12-20 23:55:59 +02:00
2017-08-14 23:02:48 +02:00
* modification of the options passed to the Video.js function (`videojs('some-id, options)`)
2016-11-04 20:25:32 +02:00
* modification of the dom video element that will be used for the player
`beforesetup` hook functions should:
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
* take two arguments
2017-08-14 23:02:48 +02:00
1. videoEl: dom video element that Video.js is going to use to create a player
1. options: options that Video.js was intialized with and will later pass to the player during creation
* return options that will merge and override options that Video.js with intialized with
2016-11-04 20:25:32 +02:00
Example: adding beforesetup hook
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
var beforeSetup = function(videoEl, options) {
2017-08-14 23:02:48 +02:00
// videoEl.id will be some-id here, since that is what Video.js
2016-12-20 23:55:59 +02:00
// was created with
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
2016-12-20 23:55:59 +02:00
// autoplay will be true here, since we passed in as such
(options.autoplay) {
options.autoplay = false
}
2016-11-04 20:25:32 +02:00
2016-12-20 23:55:59 +02:00
// options that are returned here will be merged with old options
// in this example options will now be
// {autoplay: false, controls: true}
return options;
};
2016-11-04 20:25:32 +02:00
2016-12-20 23:55:59 +02:00
videojs.hook('beforesetup', beforeSetup);
videojs('some-id', {autoplay: true, controls: true});
2016-11-04 20:25:32 +02:00
```
### setup
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
`setup` is called just after the player is created. This allows:
2016-12-20 23:55:59 +02:00
2017-08-14 23:02:48 +02:00
* plugin or custom functionality to intialize on the player
2016-11-04 20:25:32 +02:00
* changes to the player object itself
`setup` hook functions:
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
* Take one argument
2017-08-14 23:02:48 +02:00
* player: the player that Video.js created
2016-11-04 20:25:32 +02:00
* Don't have to return anything
2017-08-14 23:02:48 +02:00
Example: adding a setup hook
2016-12-20 23:55:59 +02:00
2016-11-04 20:25:32 +02:00
```js
var setup = function(player) {
// initialize the foo plugin
player.foo();
};
var foo = function() {};
2017-08-14 23:02:48 +02:00
videojs.registerPlugin('foo', foo);
2016-11-04 20:25:32 +02:00
videojs.hook('setup', setup);
var player = videojs('some-id', {autoplay: true, controls: true});
```
## Usage
### Adding
2016-12-20 23:55:59 +02:00
2017-08-14 23:02:48 +02:00
In order to use hooks you must first include Video.js in the page or script that you are using. Then you add hooks using `videojs.hook(<name>, function)` before running the `videojs()` function.
2016-11-04 20:25:32 +02:00
Example: adding hooks
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) {
// videoEl will be the element with id=vid1
// options will contain {autoplay: false}
});
videojs.hook('setup', function(player) {
// player will be the same player that is defined below
// as `var player`
});
var player = videojs('vid1', {autoplay: false});
2016-11-04 20:25:32 +02:00
```
2017-08-14 23:02:48 +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
### Getting
2016-12-20 23:55:59 +02:00
2017-08-14 23:02:48 +02:00
To access the array of hooks that currently exists and will be run on the Video.js object you can use the `videojs.hooks` function.
2016-11-04 20:25:32 +02:00
2017-08-14 23:02:48 +02:00
Example: getting all hooks attached to Video.js
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
var beforeSetupHooks = videojs.hooks('beforesetup');
var setupHooks = videojs.hooks('setup');
2016-11-04 20:25:32 +02:00
```
### Removing
2016-12-20 23:55:59 +02:00
2017-08-14 23:02:48 +02:00
To stop hooks from being executed during the Video.js lifecycle you will remove them using `videojs.removeHook` .
2016-11-04 20:25:32 +02:00
Example: remove a hook that was defined by you
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
var beforeSetup = function(videoEl, options) {};
2016-11-04 20:25:32 +02:00
2016-12-20 23:55:59 +02:00
// add the hook
videojs.hook('beforesetup', beforeSetup);
2016-11-04 20:25:32 +02:00
2016-12-20 23:55:59 +02:00
// remove that same hook
videojs.removeHook('beforesetup', beforeSetup);
2016-11-04 20:25:32 +02:00
```
You can also use `videojs.hooks` in conjunction with `videojs.removeHook` but it may have unexpected results if used during an asynchronous callbacks as other plugins/functionality may have added hooks.
Example: using `videojs.hooks` and `videojs.removeHook` to remove a hook
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
// add the hook
videojs.hook('setup', function(videoEl, options) {});
2016-11-04 20:25:32 +02:00
2016-12-20 23:55:59 +02:00
var setupHooks = videojs.hooks('setup');
2016-11-04 20:25:32 +02:00
2016-12-20 23:55:59 +02:00
// remove the hook you just added
videojs.removeHook('setup', setupHooks[setupHooks.length - 1]);
2016-11-04 20:25:32 +02:00
```