Creating Plugins
Step-by-step guide to creating custom MP4E plugins using the Plugin Editor.
Getting Started
Custom plugins let you create any interactive content using HTML, CSS, and JavaScript. They run in a sandboxed iframe and communicate with the player through the mp4e bridge API.
{
id: 'acme:countdown',
name: 'Countdown Timer',
type: 'overlay',
description: 'Displays a countdown to a specific date',
configSchema: {
type: 'object',
properties: {
targetDate: { type: 'string', format: 'date-time', title: 'Target Date' },
title: { type: 'string', title: 'Title', default: 'Coming Soon' },
showSeconds: { type: 'boolean', title: 'Show Seconds', default: true }
},
required: ['targetDate']
},
emits: {
complete: { label: 'Countdown Complete', description: 'Fired when countdown reaches zero' }
},
content: {
html: `
<div class="countdown">
<h3 class="title">{{title}}</h3>
<div class="time-display">
<div class="unit"><span class="val" id="days">00</span><span class="lbl">Days</span></div>
<div class="unit"><span class="val" id="hours">00</span><span class="lbl">Hours</span></div>
<div class="unit"><span class="val" id="minutes">00</span><span class="lbl">Minutes</span></div>
<div class="unit seconds" id="sec-wrap"><span class="val" id="seconds">00</span><span class="lbl">Seconds</span></div>
</div>
</div>`,
css: `
.countdown { text-align:center; padding:24px; background:linear-gradient(135deg,#667eea,#764ba2); border-radius:16px; color:#fff; font-family:system-ui,sans-serif; }
.title { margin:0 0 16px; font-size:18px; font-weight:600; opacity:.9; }
.time-display { display:flex; gap:16px; justify-content:center; }
.unit { display:flex; flex-direction:column; align-items:center; }
.val { font-size:36px; font-weight:700; line-height:1; font-variant-numeric:tabular-nums; }
.lbl { font-size:12px; text-transform:uppercase; opacity:.7; margin-top:4px; }
.seconds.hidden { display:none; }`,
script: `
var config = mp4e.getConfig();
var targetDate = new Date(config.targetDate);
if (!config.showSeconds) {
document.getElementById('sec-wrap').classList.add('hidden');
}
function tick() {
var diff = targetDate - new Date();
if (diff <= 0) {
mp4e.emit('complete');
document.querySelector('.time-display').innerHTML = '<span class="val">Done!</span>';
clearInterval(iv);
return;
}
document.getElementById('days').textContent = String(Math.floor(diff/864e5)).padStart(2,'0');
document.getElementById('hours').textContent = String(Math.floor(diff%864e5/36e5)).padStart(2,'0');
document.getElementById('minutes').textContent = String(Math.floor(diff%36e5/6e4)).padStart(2,'0');
document.getElementById('seconds').textContent = String(Math.floor(diff%6e4/1e3)).padStart(2,'0');
}
tick();
var iv = setInterval(tick, 1000);`
}
}Using the Plugin Editor
The easiest way to create plugins is through the Studio's built-in Plugin Editor:
- Create an overlay in the Overlays panel and select "Custom Plugin" as the type
- Open the Plugin Editor — the full-screen editor appears with a two-panel layout
- Write your HTML in the HTML tab, using
{{variable}}syntax for dynamic values - Style with CSS in the CSS tab — use
var(--s)for responsive scaling - Add interactivity in the Script tab using the
mp4ebridge API - Define inputs in the Inputs tab — creates form fields in the overlay settings
- Define events in the Emits tab — makes events available as rule triggers
- Preview live in the Preview tab — changes reflect instantly
Cmd/Ctrl and click any tab to open it in the right panel. A common workflow: HTML on the left, Preview on the right. Or Script on the left, API Docs on the right.Editor Tabs
| Tab | Purpose | Maps To |
|---|---|---|
| HTML | Plugin markup with template interpolation | content.html |
| CSS | Scoped styles, responsive via CSS custom properties | content.css |
| Script | JavaScript — use mp4e API here, NOT template syntax | content.script |
| Inputs | JSON Schema for configuration fields (generates form UI) | configSchema |
| Events | Custom events with data schemas (become rule triggers) | emits |
| Actions | Actions other plugins/rules can call on this plugin | actions |
| Variables | The values the plugin keeps (scope, read-only); everyone can read them | variables |
| Display | Position, animation, auto-close settings | displaySettings |
| Details | Name, version, type, category, author, license, tags | name, version, etc. |
| Preview | Live iframe preview — updates as you type | UI only |
| API | Every call a plugin script may make on mp4e, generated from the manifest | UI only |
Plugin Structure
A plugin has three content sections plus metadata:
HTML
Structure and content. Supports {{variable}} template interpolation for dynamic values from config and project variables.
CSS
Full CSS including animations, media queries, and {{variable}} interpolation via custom properties.
JavaScript
Logic and interactivity via the mp4e bridge API. Template syntax does NOT work here.
HTML Section
HTML templates support double-curly-brace syntax for interpolation:
<div class="product-card">
<!-- Simple interpolation from config -->
<h2>{{title}}</h2>
<!-- Object data (for object-display plugins) -->
<p>{{object.userLabel}}</p>
<span class="price">${{object.data.price}}</span>
<!-- Project variables -->
<span class="cart-badge">{{cartCount}} items</span>
<!-- HTML escaping (default) vs unescaped -->
<p>{{description}}</p> <!-- Escaped -->
<div>{{{richContent}}}</div> <!-- Unescaped HTML -->
</div>CSS Section
Full CSS support. Use var(--s) for scale-responsive sizing:
/* Scale-responsive sizing via CSS custom property */
.card {
padding: calc(16px * var(--s, 1));
font-size: calc(14px * var(--s, 1));
border-radius: calc(8px * var(--s, 1));
}
/* Config value interpolation via custom properties */
:root {
--accent: {{accentColor}};
}
.button { background: var(--accent); }
/* Animations */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.animated { animation: fadeIn 0.3s ease-out; }
/* Responsive */
@media (max-width: 480px) {
.hide-mobile { display: none; }
}JavaScript Section
{{variable}} interpolation only works in HTML and CSS. In JavaScript, use the mp4e API:
var config = mp4e.getConfig(); var title = config.title;var title = "{{title}}";// Access configuration (sync)
var config = mp4e.getConfig();
var title = config.title;
var price = config.price;
// Access bound object data (for object-display/group-bound plugins)
var obj = window.object; // { id, label, userLabel, data, position }
// DOM manipulation
document.getElementById('title').textContent = title;
// Event listeners
document.getElementById('buy-btn').addEventListener('click', function() {
// Emit custom event (triggers rules)
mp4e.emit('purchased', { productId: config.productId, price: price });
// Execute player actions
mp4e.executeActions([
{ type: 'setVariable', variableId: 'cartCount', operation: 'increment', value: 1 },
{ type: 'showNotification', message: 'Added to cart!', variant: 'success' }
]);
});
// Listen for variable changes
mp4e.onVariableChange(function(variables, changedIds) {
// changedIds is an array of changed variable IDs, or undefined for full broadcast
if (!changedIds || changedIds.includes('cartCount')) {
document.getElementById('cart-badge').textContent = variables.cartCount + ' items';
}
});
// Listen for config updates (when overlay config changes in studio)
mp4e.onConfigUpdate(function(newConfig) {
document.getElementById('title').textContent = newConfig.title;
});
// Video events
mp4e.on('mp4e:video:timeupdate', function(state) {
console.log('Time:', state.currentTime);
});
// Async operations
mp4e.getVariable('score').then(function(score) {
mp4e.setVariable('score', score + 10);
});
// Cleanup intervals when plugin is destroyed
var iv = setInterval(tick, 1000);
// Note: intervals are automatically cleared when iframe is destroyedConfiguration Schema (Inputs)
Define configurable properties using JSON Schema in the Inputs tab. The Studio automatically generates form UI based on your schema:
{
configSchema: {
type: 'object',
properties: {
// String — text input
title: {
type: 'string',
title: 'Title',
description: 'Product title',
default: 'Product Name'
},
// Number — numeric input with constraints
price: {
type: 'number',
title: 'Price',
minimum: 0,
maximum: 999999,
default: 0
},
// Enum — dropdown select
currency: {
type: 'string',
title: 'Currency',
enum: ['USD', 'EUR', 'GBP', 'JPY'],
default: 'USD'
},
// Boolean — checkbox/toggle
showRating: {
type: 'boolean',
title: 'Show Rating',
default: true
},
// Color — color picker
accentColor: {
type: 'string',
format: 'color',
title: 'Accent Color',
default: '#3b82f6'
},
// Variable reference — dropdown of project variables
scoreVariable: {
type: 'string',
title: 'Score Variable',
'x-ref': 'variable' // Shows variable picker in Studio
},
// Array
tags: {
type: 'array',
title: 'Tags',
items: { type: 'string' },
default: []
},
// Nested object
styling: {
type: 'object',
title: 'Styling',
properties: {
borderRadius: { type: 'number', title: 'Border Radius', default: 8 },
shadow: { type: 'boolean', title: 'Shadow', default: true }
}
}
},
required: ['title', 'price']
}
}Config values are accessible in HTML via {{fieldName}} and in JavaScript via mp4e.getConfig().fieldName.
Sizing
The optional sizing block tells the Studio what shape a new overlay for this plugin should start out as. The author can always move and resize it afterwards — these are starting values, not constraints.
{
sizing: {
// Start the overlay at position 0,0 filling the whole video (100% x 100%),
// whatever the video's resolution. For plugins that place their own content
// inside the frame — full-screen cards, chapter panels. Takes precedence
// over preferredWidth/preferredHeight.
fullFrame: false,
// Starting size in video-native pixels. 'auto' is not content measurement:
// it resolves to 400 x 300.
preferredWidth: 400,
preferredHeight: 300,
// 'auto' lets the plugin's content determine the overlay's size;
// 'fixed' uses the size above
defaultSizeMode: 'fixed',
// The frame this plugin was designed against. Documentation for whoever
// reads the manifest — nothing in the runtime reads these.
baseWidth: 1920,
baseHeight: 1080
}
}--s only for chrome that should stay a constant size on screen however large the video is rendered: calc(16px / var(--s, 1)). Multiplying scales twice and shrinks. The value is always the video's rendered width divided by 1920, whatever a plugin declares for baseWidth.Content-sized overlays (Auto)
A new overlay for your plugin starts in Auto size unless your manifest sets sizing.defaultSizeMode: 'fixed'. In Auto size your content is laid out at the overlay's width — the widest it may be; text wraps there — and the overlay is as tall as your content. The overlay then shows only what your plugin draws: the empty margin around your content is cut away, and taps there reach whatever is underneath (the video, objects, other overlays). Nothing moves: your content stays exactly where it was laid out, for any scale, flip or rotation. Your clickable regions (mp4e.setHitRegions) and the focus ring of a TV remote are cut to the part that shows.
What counts as drawn
- An element with a background colour or image, a border or a box shadow.
- Images, video, canvas, SVG, embedded frames and form controls (inputs, buttons, selects, progress bars).
- Text.
- An element that draws a
::beforeor::afterwith content (counted as the element's own box). - The reach of shadows (
box-shadow,text-shadow), outlines and filters (drop-shadow(),blur(), so a glow is shown whole), plus a small margin.
What does not show does not count: display: none, visibility: hidden and opacity: 0 (except an element that is being animated, so a fade-in is shown). An overflow: hidden element cuts what lies outside it, as the browser does; an overflow on html or body never cuts your content.
Hover and animation
One rule: the shown area never shrinks while the pointer is over your overlay or anything in your document animates (a CSS animation or transition, or element.animate(), finite or infinite, including the delay before it starts) — it only grows. Once the pointer has left and the animations have ended, the overlay is measured again and may shrink. While something animates it is measured several times a second until the shown area stops growing, so a pulsing badge or a looping glow is shown at its full extent, and after that it costs nothing.
What can be cut, and how to avoid it
- A
::beforeor::afterdrawn outside its element — a tooltip tail, a badge made from a pseudo-element. Draw it with a real element, or give its element room. - Children that overflow a button, an image-like element or an inline SVG with
overflow: visible: those are measured as boxes. - Content that grows (a drawer opening, a new line of text) can show cut at its old size for up to a fifth of a second, until the next measure.
- A document with more than 500 drawn elements and pieces of text is not measured: the overlay shows its whole box, and the browser console says once that the plugin's box does not follow its content.
- An animation that runs forever anywhere in your document keeps the box from ever shrinking — even one inside something hidden with
opacity: 0. Hide a spinner withdisplay: none, which stops its animation.
Turning it off
Put data-mp4e-hug="off" on any element of your document — for example <body data-mp4e-hug="off"> — and the overlay keeps its whole laid-out box. A plugin that fills a box it is given should declare a fixed box instead:
{
sizing: {
defaultSizeMode: 'fixed'
}
}Some overlays always show their whole box, whatever their size mode: an overlay whose own style paints its box (a background, a shadow, a backdrop filter or rounded corners), one that shows a part of the video, and a hotspot bound to a group that follows each of its objects. Object cards, subtitles and the player's controls are not overlays and are never cut.
Emits (Events)
Define events the plugin can emit. These become available as rule triggers in the Studio, so users can attach actions to your plugin's events — for every instance, for one overlay, or for one card (see Where an Event Is Handled).
emits is a map of event name to { label, description, dataSchema }, as below, or simply a list of names (emits: ['answered', 'closed']). Use the map when you can: its dataSchema fields are what the Studio offers as {{eventData.*}} values and Event Data conditions.
{
emits: {
// Simple event (no data)
clicked: {
label: 'Clicked',
description: 'Fired when the plugin is clicked'
},
// Event with typed data payload
purchased: {
label: 'Purchase',
description: 'Fired when user completes a purchase',
dataSchema: {
type: 'object',
properties: {
productId: { type: 'string', description: 'Product ID' },
price: { type: 'number', description: 'Purchase price' },
quantity: { type: 'number', description: 'Quantity' }
}
}
}
}
}
// Emitting from JavaScript:
mp4e.emit('purchased', {
productId: mp4e.getConfig().productId,
price: mp4e.getConfig().price,
quantity: 1
});
// The event is routed to rules as: plugin:<namespace>:<identifier>:purchased
// Rules can filter on event data using eventData conditions:
// { type: 'eventData', path: 'price', operator: 'gte', value: 100 }Reference fields
When a field carries the ID of something in the video rather than free text, say so with x-ref. The Studio then offers that entity's picker as the Value control on an Event Data condition, so the author chooses Short from a list instead of typing a scene group ID they have no way of knowing. The value that gets stored is the same plain ID string either way, and the author can still switch to a template such as {{chosenCut}}.
{
emits: {
'cut:selected': {
label: 'Cut Selected',
dataSchema: {
type: 'object',
properties: {
groupId: {
type: 'string',
description: 'Scene group id of the cut',
'x-ref': 'sceneGroup' // -> scene group picker
},
sceneId: { type: 'string', 'x-ref': 'scene' },
overlayId: { type: 'string', 'x-ref': 'overlay' },
layerId: { type: 'string', 'x-ref': 'layer' },
objectId: { type: 'string', 'x-ref': 'object' }
}
}
}
}
}Values are scene, sceneGroup, overlay, layer, object, group, variable, plugin, subtitleTrack and audioTrack. Leave x-ref off any field that holds ordinary text or a number.
x-ref is part of the plugin manifest, so it is authored in the manifest you seed or import rather than in the Emits tab, whose property rows cover name, type and description. An annotation written in the manifest survives editing the plugin in the Studio.Actions
Actions are methods that other plugins or rules can call on your plugin via the pluginAction action type.
{
actions: [
{
id: 'nextQuestion',
label: 'Next Question',
description: 'Advance to next quiz question',
configSchema: {
type: 'object',
properties: {
skipAnimation: { type: 'boolean', default: false }
}
}
},
{
id: 'reset',
label: 'Reset',
description: 'Reset plugin to initial state'
}
]
}
// Handle incoming actions in JavaScript:
mp4e.onAction('nextQuestion', function(config) {
var skip = config && config.skipAnimation;
advanceToNextQuestion(skip);
});
mp4e.onAction('reset', function() {
resetState();
});
// Other plugins/rules call these via:
// { type: 'pluginAction', pluginId: 'quiz', actionId: 'nextQuestion', config: { skipAnimation: true } }Variables
A plugin declares the variables it keeps. Each belongs to someone, given by its scope, and the player creates it from the declaration:
| Prop | Type | Default | Description |
|---|---|---|---|
scope: 'plugin' | string | 'plugin' | One value for the plugin, shared by all its uses. Its id is <plugin key>:<id> (mp4e:cart:total). The plugin's own. |
scope: 'instance' | string | - | One value per use of the plugin: per overlay (<plugin key>:<id>@<overlayId>), per object for an overlay bound to a group (@<overlayId>:<objectId>), per object for a card (@display:<objectId>), so closing and reopening a card finds its value again. The plugin's own. A service plugin runs once per video, so its instance variable is its plugin variable. |
scope: 'global' | string | - | One value for the video, under its own id (cart). It belongs to the video, not to the plugin: it is created only when the video has no variable with that id (an existing one is used as it is), anyone may change it, and it stays when the plugin is removed. Plugins that agree on a name and a shape share it. |
{
variables: [
// The plugin's own state: only its script changes it
{ id: 'total', name: 'Total', type: 'number', default: 0, readOnly: true },
// One score per quiz in the video
{ id: 'score', name: 'Score', type: 'number', default: 0, scope: 'instance', readOnly: true },
// A page that rules may turn
{ id: 'page', name: 'Page', type: 'number', default: 1 },
// Shared with other plugins by name
{ id: 'cart', type: 'json', default: {}, scope: 'global' },
// Kept for the viewer's next visit
{ id: 'items', name: 'Items', type: 'list', default: [], readOnly: true, persist: true }
]
}In the plugin's own script a short id is its own variable. mp4e.getVariable('score') reads this use's score, and mp4e.setVariable('total', 42) sets the plugin's total. A short id the plugin does not declare means a variable of that name in the video (a global, or the creator's). Templates and rules on the plugin's overlay or card reach the same variables as {{self.score}}; anywhere else they use the full id.
readOnly: true means only the plugin's own script changes the value while the video plays: a rule's or another plugin's change is refused, reported as action:blocked with reason: "readOnly" and warned in the console, and the diagnostics flag a stored rule that would make one (variable.readOnlyWrite). Everyone may read it, and the host may always change it. Give a readOnly variable actions (an addItem for a cart's items) when others need to change it. A global cannot be readOnly: it belongs to the video.
persist: true keeps the value for the viewer's next visit (a cart's items, a course's progress). It is your default: the creator can turn it off or on for each variable in their project (“Remember between visits”), and the site playing the video decides whether and where anything is kept (see Keeping State Between Visits). On an instance variable each use keeps its own value. Kept values come back when the viewer returns, before the first frame: your script, templates and rules see them, except the video's onReady rules, which run before they are back and see the defaults. A value that no longer fits the variable is left out. Read-only variables come back too. When the viewer chooses Start over, every kept variable goes back to its default.
Permissions
A plugin that reads the video's Intelligence (chapters, key moments, things, people, the transcript, …) names the scopes it needs in permissions.intelligence. The Studio shows the list to the creator when the plugin is installed — the way a phone asks before an app reads your contacts — and the creator decides what it may read. people and transcript start unticked; the other scopes start ticked. A plugin can never read a scope its manifest does not request.
{
permissions: {
// summary, chapters, moments, questions, things, scenes, people, transcript
intelligence: ['chapters', 'moments', 'things', 'transcript']
}
}Request only what the plugin uses: every scope you list is a line the creator has to accept. Read it with mp4e.getIntelligence() — see Intelligence in the API reference for the scopes, what each returns and how a refusal is reported.
A plugin that calls a server with mp4e.fetch() (the request the host page makes for it) lists the server's domains in permissions.network.domains. By default that list is all mp4e.fetch may reach; a request to any other domain goes to the host's onNetworkGate for approval and is denied without one, and a host's plugin security rules can narrow or replace the list. An entry is a host name (api.example.com) or a wildcard (*.example.com, which also covers example.com). It names hosts only: it does not limit the port, and the scheme is always http: or https: (the proxy fetches nothing else).
{
permissions: {
network: {
domains: ['api.example.com', '*.cdn.example.com']
}
}
}Remote Control
A viewer with a TV remote or a keyboard reaches your overlay with the arrow keys, and OK presses the focused button, so make controls real buttons. To choose where focus starts, listen for it arriving. An arrow or Back you handle yourself must be prevented with preventDefault(): one you leave alone moves focus to the nearest control that way, or out of your plugin, and at an edge with nothing beyond it, focus stays. You can also call focus() on your own control; when your overlay hides, focus goes back to the player. An arrow stops on a text field without opening a TV's on-screen keyboard: OK or a typed character starts editing, the arrows then move the caret, and Back leaves editing. With a remote, a select or a radio button is a stop like a button, and OK changes its value; a keyboard viewer on an ordinary page keeps their arrows, as anywhere else. On a page given to the remote, an overlay that stops the video takes focus by itself when the viewer is not already on something: one that paused it with executeActions or mp4e.pause(), or whose events play it again, like a quiz a scene pauses for.
mp4e.on('focus:enter', ({ direction }) => {
// direction: 'up' | 'down' | 'left' | 'right', the way the viewer was moving
document.getElementById('first').focus();
});Shortcuts
A plugin can offer keyboard shortcuts for its actions. Each entry names a key and the action it runs:
{
actions: [
{ id: 'open', label: 'Open search' }
],
shortcuts: [
// key: a KeyboardEvent.key value — '/', 's', 'ArrowRight', …
{ id: 'open', key: '/', action: 'open', label: 'Open search' }
]
}Installing the plugin writes each shortcut into the project as an ordinary input shortcut (Studio: Project → Events → Input Shortcuts): pressing the key runs pluginAction with the plugin's type and the shortcut's action. The Studio's install prompt lists them ("Open search with /") and the creator can leave any of them out. Nothing is overwritten: a shortcut is skipped when the project already uses that key for another shortcut, and the prompt says so. From then on the shortcut belongs to the creator, who can change or delete it like any other; installing the plugin again adds back one that was deleted.
Best Practices
Performance
- Minimize DOM operations — batch updates when possible
- Use CSS animations instead of JavaScript animations
- Filter
onVariableChangebychangedIdsto avoid unnecessary work - Use
var(--s)CSS scaling instead of JavaScript-based resizing
Accessibility
- Use semantic HTML elements
- Add ARIA labels for interactive elements
- Ensure sufficient color contrast
- Support keyboard navigation (focusable elements)
Error Handling
- Wrap async operations in try/catch
- Provide fallback content for failed loads
- Use
mp4e.emit()to surface errors to rules - Validate config values before using (defaults may be undefined)
Config Gotchas
mp4e.getConfig()returns saved config, NOT merged with defaults- If a field was never saved, it will be
undefinedeven if schema has a default - Always provide JS fallbacks:
var mode = config.mode || 'sequential' - Template
{{variable}}works in HTML/CSS only, usemp4e.getConfig()in JS