Why is chrome.fileBrowserHandler API undefined when reading onExecute?

39 Views Asked by At

Overview

I'm writing a ChromeOS Files extension for encrypting files. For that I'm using the Google Chrome chrome.fileBrowserHandler API. When opening the Files context menu, I get an "Encrypt" option (Open with… > Encrypt):

ChromeOS file manager with a right-click context menu open. The mouse is hovering over the extension menu icon "Encrypt My Files".

The issue I'm facing

The example code snipped provided by the API documentation does throw an Uncaught TypeError.

The chrome://extension page with the developer settings turned on. The page returns one warning and one error with an uncaught TypeError.

The error message written for accessibility:

// Warning: Service worker registration failed. Status code: 15
"service_worker": "background.js"

// Error: Uncaught TypeError: Cannot read properties of undefined (reading 'onExecute')
// Context
// background.js
// Stack Trace
// background.js:3 (anonymous function)
chrome.fileBrowserHandler.onExecute.addListener((id, details) => {

Why am I facing the error message?

Source Code

// background.js
"use strict";

chrome.fileBrowserHandler.onExecute.addListener((id, details) => {
  if (id === "encrypt") {
    chrome.windows.create({
      url: chrome.runtime.getURL('popup.html'),
      type: "popup"
    });
  }
});
// manifest.json
{
  "manifest_version": 3,
  "name": "Encrypt My Files",
  "description": "Encrypt files natively on ChromeOS",
  "version": "0.1.0",
  "permissions": ["fileBrowserHandler"],
  "background": {
    "service_worker": "background.js"
  },
  "action": {
    "default_popup": "popup.html"
  },
  "file_browser_handlers": [
    {
      "id": "encrypt",
      "default_title": "Encrypt",
      "file_filters": [
        "filesystem:*.*"
      ]
    }
  ],
  "minimum_chrome_version": "120"
}

Things I noticed

When specifying just a console.log statement in background.js, the extension can be turned on without any issues. Also, using a chrome.runtime.onInstalled.addListener in background.js (e.g. for context menus in web pages) works.

0

There are 0 best solutions below