06 Your second plugin: a browser page (3 minutes)

/plugins/hello-web/manifest.json

  {
    "title": "Hello Web",
    "description": "Shows device info in the browser.",
    "version": "1.0.0"
  }

/plugins/hello-web/plugin.js

  // registerPlugin MUST be called synchronously when the script runs.
  CrossPoint.registerPlugin(async (container, api) => {
    container.innerHTML = '<h2>Hello Web</h2><p id="hw-out">Loading...</p>';
    const out = container.querySelector('#hw-out');
  
    // Same-origin device endpoints are plain fetch() calls.
    const status = await (await fetch('/api/status')).json();
    out.textContent =
      `Firmware ${status.version} on ${status.device}, ` +
      `free heap ${status.freeHeap} bytes. I live in ${api.dir}.`;
  });

Upload the folder (File Manager -> create /plugins/hello-web/, upload both files), then open /plugins. Your card appears.

Note: always look up elements with container.querySelector(...) instead of document.getElementById(...). Every plugin shares one page, so IDs can clash with other plugins. If you do use IDs, prefix them (hw-out).
‹ PreviousReader file (.txt)Next ›