Skip to content

Commit

Permalink
Migrate the UserChrome modifications to ESM
Browse files Browse the repository at this point in the history
This is necessary as support for JSM has been removed from Firefox 136.

It also introduces a slight change for packaging. Instead of changing the version in `native/userchrome/profile/chrome/pwa/chrome.jsm`, the version now needs to be set in `native/userchrome/profile/chrome/pwa/chrome.sys.mjs`.
  • Loading branch information
filips123 committed Jan 31, 2025
1 parent 42153c1 commit 8c5c323
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/native.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ jobs:
VERSION=${GITHUB_REF/refs\/tags\/v}
echo "VERSION=$VERSION" >> $GITHUB_ENV
sed -i "s/version = \"0.0.0\"/version = \"$VERSION\"/g" Cargo.toml
sed -i "s/DISTRIBUTION_VERSION = '0.0.0'/DISTRIBUTION_VERSION = '$VERSION'/g" userchrome/profile/chrome/pwa/chrome.jsm
sed -i "s/DISTRIBUTION_VERSION = '0.0.0'/DISTRIBUTION_VERSION = '$VERSION'/g" userchrome/profile/chrome/pwa/chrome.sys.mjs
else
echo "VERSION=0.0.0" >> $GITHUB_ENV
fi
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/help/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ To load custom JS into the web app profile:
1. Locate your web app profile inside [the profiles directory](../resources/installation-directories.md#profiles).
2. Inside the profile directory, create a `chrome` directory (if it does not exist yet).
3. Inside the `chrome` directory, create a `user` directory (if it does not exist yet).
4. Inside the `user` directory, create a `boot.jsm` or `boot.sys.mjs` file (if it does not exist yet).
4. Inside the `user` directory, create a `boot.sys.mjs` file (if it does not exist yet).
5. Copy your JS into the correct file (depending on the module format).
6. Relaunch the web app.

Expand Down
2 changes: 1 addition & 1 deletion native/packages/brew/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LIBEXEC=$3

# Set the correct version in the source files
sed -i"" -e "s/version = \"0.0.0\"/version = \"$VERSION\"/g" Cargo.toml
sed -i"" -e "s/DISTRIBUTION_VERSION = '0.0.0'/DISTRIBUTION_VERSION = '$VERSION'/g" userchrome/profile/chrome/pwa/chrome.jsm
sed -i"" -e "s/DISTRIBUTION_VERSION = '0.0.0'/DISTRIBUTION_VERSION = '$VERSION'/g" userchrome/profile/chrome/pwa/chrome.sys.mjs

# Set the path in the manifest to the Homebrew libexec directory
cp manifests/macos.json manifests/brew.json
Expand Down
4 changes: 2 additions & 2 deletions native/packages/deb/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ Copyright: Chris Simpson (@chrismsimpson)
License: Unlicense
Comment: Download: https://fontsarena.com/metropolis-by-chris-simpson/

Files: userchrome/profile/utils/hookFunction.jsm
Files: userchrome/profile/utils/hookFunction.sys.mjs
Copyright: xiaoxiaoflood <[email protected]>
Copyright: filips <[email protected]>
License: MPL-2.0
Comment: Original source: https://github.com/xiaoxiaoflood/firefox-scripts/blob/69675c7f09e9009b63b1cc239b94c03c5962a9d7/chrome/utils/hookFunction.jsm

Files: userchrome/profile/utils/xPref.jsm
Files: userchrome/profile/utils/xPref.sys.mjs
Copyright: xiaoxiaoflood <[email protected]>
Copyright: filips <[email protected]>
License: MPL-2.0
Expand Down
2 changes: 1 addition & 1 deletion native/packages/gentoo/firefoxpwa.ebuild
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ src_prepare() {
# Set version in source files as per build instructions
sed -i "s/version = \"0.0.0\"/version = \"${PV}\"/g" Cargo.toml || die
sed -i "s/DISTRIBUTION_VERSION = '0.0.0'/DISTRIBUTION_VERSION = '${PV}'/g" \
userchrome/profile/chrome/pwa/chrome.jsm || die
userchrome/profile/chrome/pwa/chrome.sys.mjs || die
}

src_configure() {
Expand Down
16 changes: 8 additions & 8 deletions native/scripts/set-version.ds
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ end
# Replace versions in files
cargo_toml_path = set "./Cargo.toml"
cargo_lock_path = set "./Cargo.lock"
chrome_jsm_path = set "./userchrome/profile/chrome/pwa/chrome.jsm"
chrome_mjs_path = set "./userchrome/profile/chrome/pwa/chrome.sys.mjs"
cargo_toml_exists = is_file ${cargo_toml_path}
cargo_lock_exists = is_file ${cargo_lock_path}
chrome_jsm_exists = is_file ${chrome_jsm_path}
if ${cargo_toml_exists} and ${cargo_lock_exists} and ${chrome_jsm_exists}
chrome_mjs_exists = is_file ${chrome_mjs_path}
if ${cargo_toml_exists} and ${cargo_lock_exists} and ${chrome_mjs_exists}
echo "Setting version in Cargo.toml to \"${pwa_ver}\""
cargo_toml = readfile ${cargo_toml_path}
cargo_toml = replace_version ${cargo_toml} "# Version will be set by CI from the Git tag when building and releasing\nversion = \"" ${pwa_ver} "\""
Expand All @@ -51,10 +51,10 @@ if ${cargo_toml_exists} and ${cargo_lock_exists} and ${chrome_jsm_exists}
cargo_lock = readfile ${cargo_lock_path}
cargo_lock = replace_version ${cargo_lock} "name = \"firefoxpwa\"\nversion = \"" ${pwa_ver} "\""
writefile ${cargo_lock_path} ${cargo_lock}
echo "Setting version in chrome.jsm to \"${pwa_ver}\""
chrome_jsm = readfile ${chrome_jsm_path}
chrome_jsm = replace_version ${chrome_jsm} "DISTRIBUTION_VERSION = '" ${pwa_ver} "'"
writefile ${chrome_jsm_path} ${chrome_jsm}
echo "Setting version in chrome.sys.mjs to \"${pwa_ver}\""
chrome_mjs = readfile ${chrome_mjs_path}
chrome_mjs = replace_version ${chrome_mjs} "DISTRIBUTION_VERSION = '" ${pwa_ver} "'"
writefile ${chrome_mjs_path} ${chrome_mjs}
else
assert_fail "Unable to locate ./Cargo.toml, ./Cargo.lock and ./userchrome/profile/chrome/pwa/chrome.jsm"
assert_fail "Unable to locate ./Cargo.toml, ./Cargo.lock and ./userchrome/profile/chrome/pwa/chrome.sys.mjs"
end
6 changes: 3 additions & 3 deletions native/src/console/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ impl Run for SiteLaunchCommand {
// Force patching if this is enabled
true
} else {
// Uses "chrome.jsm" file because it contains version info
let source = dirs.sysdata.join("userchrome/profile/chrome/pwa/chrome.jsm");
// Uses "chrome.sys.mjs" file because it contains version info
let source = dirs.sysdata.join("../../userchrome/profile/chrome/pwa/chrome.sys.mjs");
let target = dirs
.userdata
.join("profiles")
.join(profile.ulid.to_string())
.join("chrome/pwa/chrome.jsm");
.join("chrome/pwa/chrome.sys.mjs");

// Only patch if modification dates of source and target are different
// In case any error happens, just force patching
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
const EXPORTED_SYMBOLS = [];

const { XPCOMUtils } = ChromeUtils.import('resource://gre/modules/XPCOMUtils.jsm');
const Services = globalThis.Services || ChromeUtils.import('resource://gre/modules/Services.jsm').Services;
ChromeUtils.defineLazyGetter(this, 'gSystemPrincipal', () => Services.scriptSecurityManager.getSystemPrincipal());
XPCOMUtils.defineLazyModuleGetters(this, {
AppConstants: 'resource://gre/modules/AppConstants.jsm',
BrowserWindowTracker: 'resource:///modules/BrowserWindowTracker.jsm',
NetUtil: 'resource://gre/modules/NetUtil.jsm',
LangPackMatcher: 'resource://gre/modules/LangPackMatcher.jsm',
applySystemIntegration: 'resource://pwa/utils/systemIntegration.jsm',
});
import { AppConstants } from 'resource://gre/modules/AppConstants.sys.mjs';
import { NetUtil } from 'resource://gre/modules/NetUtil.sys.mjs';
import { nsDefaultCommandLineHandler, nsBrowserContentHandler } from 'resource:///modules/BrowserContentHandler.sys.mjs';
import { BrowserWindowTracker } from 'resource:///modules/BrowserWindowTracker.sys.mjs';

import { applySystemIntegration } from 'resource://pwa/utils/systemIntegration.sys.mjs';

/**
* Reads the PWAsForFirefox config file and parses it as JSON.
Expand Down Expand Up @@ -126,7 +120,6 @@ Services.prefs.getDefaultBranch(null).setBoolPref('browser.privateWindowSeparati
Services.prefs.getDefaultBranch(null).setBoolPref('browser.privacySegmentation.createdShortcut', true);

// Override command line helper to intercept PWAsForFirefox arguments and start loading the site
const { nsDefaultCommandLineHandler } = Cu.import('resource:///modules/BrowserContentHandler.jsm');
nsDefaultCommandLineHandler.prototype._handle = nsDefaultCommandLineHandler.prototype.handle;
nsDefaultCommandLineHandler.prototype.handle = function (cmdLine) {
const isStartup = cmdLine.state === Ci.nsICommandLine.STATE_INITIAL_LAUNCH;
Expand Down Expand Up @@ -183,7 +176,6 @@ nsDefaultCommandLineHandler.prototype.handle = function (cmdLine) {
// Still does not work when multiple web apps are used in the same profile
// This does not matter currently because of #81, but once it is fixed, this also needs to be reworked
if (AppConstants.platform === 'macosx') {
const { nsBrowserContentHandler } = Cu.import('resource:///modules/BrowserContentHandler.jsm');
nsBrowserContentHandler.prototype._getNewWindowArgs = nsBrowserContentHandler.prototype.getNewWindowArgs;
nsBrowserContentHandler.prototype.getNewWindowArgs = function () {
if (globalThis.gFFPWASiteConfig) {
Expand Down Expand Up @@ -220,4 +212,4 @@ Services.obs.addObserver(async subject => {
}, 'webextension-langpack-startup');

// Import browser chrome modifications
ChromeUtils.import('resource://pwa/chrome.jsm');
ChromeUtils.importESModule('resource://pwa/chrome.sys.mjs');
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const EXPORTED_SYMBOLS = [];
import { AppConstants } from 'resource://gre/modules/AppConstants.sys.mjs';

const { XPCOMUtils } = ChromeUtils.import('resource://gre/modules/XPCOMUtils.jsm');
const Services = globalThis.Services || ChromeUtils.import('resource://gre/modules/Services.jsm').Services;
XPCOMUtils.defineLazyModuleGetters(this, {
AppConstants: 'resource://gre/modules/AppConstants.jsm',
applySystemIntegration: 'resource://pwa/utils/systemIntegration.jsm',
});
import { applySystemIntegration } from 'resource://pwa/utils/systemIntegration.sys.mjs';

const SSS = Cc['@mozilla.org/content/style-sheet-service;1'].getService(Ci.nsIStyleSheetService);

Expand All @@ -16,11 +11,11 @@ class ChromeLoader {

static FILES_BASE = Services.io.getProtocolHandler('file').QueryInterface(Ci.nsIFileProtocolHandler).getURLSpecFromDir(Services.dirsvc.get('UChrm', Ci.nsIFile));

static BROWSER_SCRIPT = 'pwa/content/browser.jsm';
static BROWSER_SCRIPT = 'pwa/content/browser.sys.mjs';
static BROWSER_STYLES = 'pwa/content/browser.css';
static PREFERENCES_SCRIPT = 'pwa/content/preferences.jsm';
static PREFERENCES_SCRIPT = 'pwa/content/preferences.sys.mjs';
static PREFERENCES_STYLES = 'pwa/content/preferences.css';
static MACOS_HIDDEN_WINDOW_SCRIPT = 'pwa/content/macosHiddenWindow.jsm';
static MACOS_HIDDEN_WINDOW_SCRIPT = 'pwa/content/macosHiddenWindow.sys.mjs';

static DISTRIBUTION_ID = 'firefoxpwa';
static DISTRIBUTION_VERSION = '0.0.0';
Expand Down Expand Up @@ -79,7 +74,7 @@ class ChromeLoader {

// Load a site config from a global object - Fix for reopening web app after closing all windows on macOS
// Cannot be applied to other OSes - Does not work with multiple web apps in the same profile
// Also has some other problems - See `nsBrowserContentHandler` in `boot.jsm` for more details
// Also has some other problems - See `nsBrowserContentHandler` in `boot.sys.mjs` for more details
if (AppConstants.platform === 'macosx' || AppConstants.platform === 'linux') {
if (!window.gFFPWASiteConfig && globalThis.gFFPWASiteConfig) window.gFFPWASiteConfig = globalThis.gFFPWASiteConfig;
globalThis.gFFPWASiteConfig = window.gFFPWASiteConfig;
Expand Down Expand Up @@ -115,7 +110,8 @@ class ChromeLoader {
}

loadUserScript (scriptFilename, window) {
return Services.scriptloader.loadSubScript(ChromeLoader.FILES_BASE + scriptFilename, window, 'UTF-8');
const script = `data:application/javascript,ChromeUtils.importESModule('resource://${scriptFilename}', { global: 'current' });`;
return Services.scriptloader.loadSubScript(script, window, 'UTF-8');
}
}

Expand Down
Loading

0 comments on commit 8c5c323

Please sign in to comment.