Skip to content

How the extension works

Avery Crespi edited this page Nov 5, 2020 · 2 revisions

This page explains how the VTT Bridge extension works.

VTT Bridge diagram

The extension runs 2 content scripts and 1 background script, which perform the following tasks:

  • dmv.js: Add event listeners to DMV and send commands to background.js.
  • background.js: Receive commands from dmv.js and send commands to roll20.js.
  • roll20.js: Receive commands from background.js and run commands on Roll20.

This structure is necessary because of the limits of browser extensions.

Ideally, we would like to send commands directly from DMV to Roll20.

// Ideal case: direct message passing
DMV content script -> Roll20 content script

However, you cannot send messages from one content script to another, so we must use a background script as a relay.

// Workaround: background script relay
DMV content script -> background script -> Roll20 content script

Unfortunately, sending a message from a background script to an arbitrary content script requires the tabs permission. This permission allows the extension to "create, modify, and rearrange tabs in the browser", which is unnecessarily powerful. To sidestep this permission, we implement a polling mechanism.

// Workaround: polling mechanism
DMV content script -> background script <-> Roll20 content script

At regular intervals (e.g. every second), the Roll20 content script will send a request to the background script. The background script will receive this request, then reply with a list of commands. Neither the request nor the reply require additional permissions.

In the future, this polling mechanism could be improved by using the runtime.Port API.

Clone this wiki locally