Skip to content

Commit 8c5c323

Browse files
committed
Migrate the UserChrome modifications to ESM
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`.
1 parent 42153c1 commit 8c5c323

18 files changed

+153
-141
lines changed

.github/workflows/native.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ jobs:
245245
VERSION=${GITHUB_REF/refs\/tags\/v}
246246
echo "VERSION=$VERSION" >> $GITHUB_ENV
247247
sed -i "s/version = \"0.0.0\"/version = \"$VERSION\"/g" Cargo.toml
248-
sed -i "s/DISTRIBUTION_VERSION = '0.0.0'/DISTRIBUTION_VERSION = '$VERSION'/g" userchrome/profile/chrome/pwa/chrome.jsm
248+
sed -i "s/DISTRIBUTION_VERSION = '0.0.0'/DISTRIBUTION_VERSION = '$VERSION'/g" userchrome/profile/chrome/pwa/chrome.sys.mjs
249249
else
250250
echo "VERSION=0.0.0" >> $GITHUB_ENV
251251
fi

docs/docs/help/faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ To load custom JS into the web app profile:
380380
1. Locate your web app profile inside [the profiles directory](../resources/installation-directories.md#profiles).
381381
2. Inside the profile directory, create a `chrome` directory (if it does not exist yet).
382382
3. Inside the `chrome` directory, create a `user` directory (if it does not exist yet).
383-
4. Inside the `user` directory, create a `boot.jsm` or `boot.sys.mjs` file (if it does not exist yet).
383+
4. Inside the `user` directory, create a `boot.sys.mjs` file (if it does not exist yet).
384384
5. Copy your JS into the correct file (depending on the module format).
385385
6. Relaunch the web app.
386386

native/packages/brew/configure.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LIBEXEC=$3
1616

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

2121
# Set the path in the manifest to the Homebrew libexec directory
2222
cp manifests/macos.json manifests/brew.json

native/packages/deb/copyright

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ Copyright: Chris Simpson (@chrismsimpson)
2525
License: Unlicense
2626
Comment: Download: https://fontsarena.com/metropolis-by-chris-simpson/
2727

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

34-
Files: userchrome/profile/utils/xPref.jsm
34+
Files: userchrome/profile/utils/xPref.sys.mjs
3535
Copyright: xiaoxiaoflood <[email protected]>
3636
Copyright: filips <[email protected]>
3737
License: MPL-2.0

native/packages/gentoo/firefoxpwa.ebuild

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ src_prepare() {
6262
# Set version in source files as per build instructions
6363
sed -i "s/version = \"0.0.0\"/version = \"${PV}\"/g" Cargo.toml || die
6464
sed -i "s/DISTRIBUTION_VERSION = '0.0.0'/DISTRIBUTION_VERSION = '${PV}'/g" \
65-
userchrome/profile/chrome/pwa/chrome.jsm || die
65+
userchrome/profile/chrome/pwa/chrome.sys.mjs || die
6666
}
6767

6868
src_configure() {

native/scripts/set-version.ds

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ end
3838
# Replace versions in files
3939
cargo_toml_path = set "./Cargo.toml"
4040
cargo_lock_path = set "./Cargo.lock"
41-
chrome_jsm_path = set "./userchrome/profile/chrome/pwa/chrome.jsm"
41+
chrome_mjs_path = set "./userchrome/profile/chrome/pwa/chrome.sys.mjs"
4242
cargo_toml_exists = is_file ${cargo_toml_path}
4343
cargo_lock_exists = is_file ${cargo_lock_path}
44-
chrome_jsm_exists = is_file ${chrome_jsm_path}
45-
if ${cargo_toml_exists} and ${cargo_lock_exists} and ${chrome_jsm_exists}
44+
chrome_mjs_exists = is_file ${chrome_mjs_path}
45+
if ${cargo_toml_exists} and ${cargo_lock_exists} and ${chrome_mjs_exists}
4646
echo "Setting version in Cargo.toml to \"${pwa_ver}\""
4747
cargo_toml = readfile ${cargo_toml_path}
4848
cargo_toml = replace_version ${cargo_toml} "# Version will be set by CI from the Git tag when building and releasing\nversion = \"" ${pwa_ver} "\""
@@ -51,10 +51,10 @@ if ${cargo_toml_exists} and ${cargo_lock_exists} and ${chrome_jsm_exists}
5151
cargo_lock = readfile ${cargo_lock_path}
5252
cargo_lock = replace_version ${cargo_lock} "name = \"firefoxpwa\"\nversion = \"" ${pwa_ver} "\""
5353
writefile ${cargo_lock_path} ${cargo_lock}
54-
echo "Setting version in chrome.jsm to \"${pwa_ver}\""
55-
chrome_jsm = readfile ${chrome_jsm_path}
56-
chrome_jsm = replace_version ${chrome_jsm} "DISTRIBUTION_VERSION = '" ${pwa_ver} "'"
57-
writefile ${chrome_jsm_path} ${chrome_jsm}
54+
echo "Setting version in chrome.sys.mjs to \"${pwa_ver}\""
55+
chrome_mjs = readfile ${chrome_mjs_path}
56+
chrome_mjs = replace_version ${chrome_mjs} "DISTRIBUTION_VERSION = '" ${pwa_ver} "'"
57+
writefile ${chrome_mjs_path} ${chrome_mjs}
5858
else
59-
assert_fail "Unable to locate ./Cargo.toml, ./Cargo.lock and ./userchrome/profile/chrome/pwa/chrome.jsm"
59+
assert_fail "Unable to locate ./Cargo.toml, ./Cargo.lock and ./userchrome/profile/chrome/pwa/chrome.sys.mjs"
6060
end

native/src/console/site.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ impl Run for SiteLaunchCommand {
7575
// Force patching if this is enabled
7676
true
7777
} else {
78-
// Uses "chrome.jsm" file because it contains version info
79-
let source = dirs.sysdata.join("userchrome/profile/chrome/pwa/chrome.jsm");
78+
// Uses "chrome.sys.mjs" file because it contains version info
79+
let source = dirs.sysdata.join("../../userchrome/profile/chrome/pwa/chrome.sys.mjs");
8080
let target = dirs
8181
.userdata
8282
.join("profiles")
8383
.join(profile.ulid.to_string())
84-
.join("chrome/pwa/chrome.jsm");
84+
.join("chrome/pwa/chrome.sys.mjs");
8585

8686
// Only patch if modification dates of source and target are different
8787
// In case any error happens, just force patching

native/userchrome/profile/chrome/pwa/boot.jsm native/userchrome/profile/chrome/pwa/boot.sys.mjs

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
const EXPORTED_SYMBOLS = [];
2-
3-
const { XPCOMUtils } = ChromeUtils.import('resource://gre/modules/XPCOMUtils.jsm');
4-
const Services = globalThis.Services || ChromeUtils.import('resource://gre/modules/Services.jsm').Services;
5-
ChromeUtils.defineLazyGetter(this, 'gSystemPrincipal', () => Services.scriptSecurityManager.getSystemPrincipal());
6-
XPCOMUtils.defineLazyModuleGetters(this, {
7-
AppConstants: 'resource://gre/modules/AppConstants.jsm',
8-
BrowserWindowTracker: 'resource:///modules/BrowserWindowTracker.jsm',
9-
NetUtil: 'resource://gre/modules/NetUtil.jsm',
10-
LangPackMatcher: 'resource://gre/modules/LangPackMatcher.jsm',
11-
applySystemIntegration: 'resource://pwa/utils/systemIntegration.jsm',
12-
});
1+
import { AppConstants } from 'resource://gre/modules/AppConstants.sys.mjs';
2+
import { NetUtil } from 'resource://gre/modules/NetUtil.sys.mjs';
3+
import { nsDefaultCommandLineHandler, nsBrowserContentHandler } from 'resource:///modules/BrowserContentHandler.sys.mjs';
4+
import { BrowserWindowTracker } from 'resource:///modules/BrowserWindowTracker.sys.mjs';
5+
6+
import { applySystemIntegration } from 'resource://pwa/utils/systemIntegration.sys.mjs';
137

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

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

222214
// Import browser chrome modifications
223-
ChromeUtils.import('resource://pwa/chrome.jsm');
215+
ChromeUtils.importESModule('resource://pwa/chrome.sys.mjs');

native/userchrome/profile/chrome/pwa/chrome.jsm native/userchrome/profile/chrome/pwa/chrome.sys.mjs

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
const EXPORTED_SYMBOLS = [];
1+
import { AppConstants } from 'resource://gre/modules/AppConstants.sys.mjs';
22

3-
const { XPCOMUtils } = ChromeUtils.import('resource://gre/modules/XPCOMUtils.jsm');
4-
const Services = globalThis.Services || ChromeUtils.import('resource://gre/modules/Services.jsm').Services;
5-
XPCOMUtils.defineLazyModuleGetters(this, {
6-
AppConstants: 'resource://gre/modules/AppConstants.jsm',
7-
applySystemIntegration: 'resource://pwa/utils/systemIntegration.jsm',
8-
});
3+
import { applySystemIntegration } from 'resource://pwa/utils/systemIntegration.sys.mjs';
94

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

@@ -16,11 +11,11 @@ class ChromeLoader {
1611

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

19-
static BROWSER_SCRIPT = 'pwa/content/browser.jsm';
14+
static BROWSER_SCRIPT = 'pwa/content/browser.sys.mjs';
2015
static BROWSER_STYLES = 'pwa/content/browser.css';
21-
static PREFERENCES_SCRIPT = 'pwa/content/preferences.jsm';
16+
static PREFERENCES_SCRIPT = 'pwa/content/preferences.sys.mjs';
2217
static PREFERENCES_STYLES = 'pwa/content/preferences.css';
23-
static MACOS_HIDDEN_WINDOW_SCRIPT = 'pwa/content/macosHiddenWindow.jsm';
18+
static MACOS_HIDDEN_WINDOW_SCRIPT = 'pwa/content/macosHiddenWindow.sys.mjs';
2419

2520
static DISTRIBUTION_ID = 'firefoxpwa';
2621
static DISTRIBUTION_VERSION = '0.0.0';
@@ -79,7 +74,7 @@ class ChromeLoader {
7974

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

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

0 commit comments

Comments
 (0)