Search code examples
firefox-addonfirefox-addon-sdk

How to load CSS file from profile directory (how to create URI from filepath)


My extension has saved a CSS file to the user's profile directory. Now, I want to load this CSS file into a window.

sheetsheet/utils seems to have a loadSheet(window, uri, type) method for this (https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/stylesheet_utils) but I can't figure out how to convert my CSS file path into a URI object that is expected.

My code is something like this:

const ssutils   = require("sdk/stylesheet/utils"),
      windows   = require("sdk/windows");

var path_to_file = "c:\users\myname\appdata\local\temp\tmppr9imy.mozrunner\myextension\mycssfile.css"

for (let wind of windows.browserWindows) {
    // What is the magic function I need to use?
    ssutils.loadSheet(wind, someMagicFunctionHere(path_to_file), "user");
}

Solution

  • The sdk/url module prvcides the function you ask.

    const { fromFilename } = require("sdk/url");
    
    ...
    
    ssutils.loadSheet(wind, fromFilename(path_to_file), "user");
    

    fromFilename converts a path to a file: URI