Skip to content

Core File: main.js ​

The core file is the script Acode runs when your plugin loads. It can have any name and location, as long as plugin.json points to it with the main field.

It has two jobs:

  1. Register an init function that starts your plugin.
  2. Register an unmount function that undoes everything init did.

For when and how Acode calls them, see Understanding Plugins.

You rarely write this by hand

The official templates already contain the registration code below. You only fill in the init and destroy methods of the AcodePlugin class.

Register the plugin ​

Your script has access to the global acode object. Register the init function with acode.setPluginInit:

js
acode.setPluginInit(pluginId, init, settings?)
ParameterDescription
pluginIdThe id from your plugin.json.
initFunction Acode calls to start the plugin.
settingsOptional. Adds a settings page for your plugin. See Plugin settings.

init arguments ​

init receives three arguments:

ArgumentTypeDescription
baseUrlstringURL of your plugin folder, for loading bundled files.
$pageWcPageA page object that facilitates the display of content within Acode.
optionsobjectExtra information, described below.

options contains:

PropertyDescription
cacheFileUrlURL of your plugin's cache file.
cacheFileFile object for the cache file, so you can read and write it.
firstInittrue only on the run right after installation.
ctxEncrypted secret storage and permission checks. See Plugin Context.
fileIconsPlugin-bound File Icons API. Available from versionCode 1012.

Every property is documented in more detail under acode.setPluginInit.

Register the unmount handler ​

js
acode.setPluginUnmount(pluginId, unmount)

Acode calls unmount when the plugin is disabled, uninstalled or reloaded. Use it to remove everything you added: commands, event listeners, timers, UI elements and formatters. Anything left behind stays active until Acode restarts.

WARNING

Do not skip this. A plugin that does not clean up will leave duplicate commands and listeners behind every time it is reloaded during development.

Full example ​

This is the shape used by the official templates:

js
import plugin from "../plugin.json";

class AcodePlugin {
  baseUrl = "";

  async init($page, cacheFile, cacheFileUrl, firstInit, ctx, fileIcons) {
    const commands = acode.require("commands");

    commands.addCommand({
      name: "example-plugin",
      description: "Open the example plugin",
      bindKey: { win: "Ctrl-Alt-E", mac: "Command-Alt-E" },
      exec: () => {
        $page.innerHTML = `
          <h1>Example Plugin</h1>
          <p>This is an example plugin.</p>
        `;
        $page.show();
      },
    });
  }

  async destroy() {
    const commands = acode.require("commands");
    commands.removeCommand("example-plugin");
  }
}

if (window.acode) {
  const acodePlugin = new AcodePlugin();

  acode.setPluginInit(
    plugin.id,
    async (baseUrl, $page, { cacheFileUrl, cacheFile, firstInit, ctx, fileIcons }) => {
      // Make sure baseUrl ends with "/" so you can append file names to it
      acodePlugin.baseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
      await acodePlugin.init($page, cacheFile, cacheFileUrl, firstInit, ctx, fileIcons);
    },
  );

  acode.setPluginUnmount(plugin.id, () => {
    acodePlugin.destroy();
  });
}

The if (window.acode) check lets the same bundle be loaded outside Acode (for example in tests) without throwing.

Released under the MIT License.