refactor: waterfox utils
This commit is contained in:
@@ -16,10 +16,10 @@ const lazy = {};
|
||||
|
||||
XPCOMUtils.defineLazyModuleGetters(lazy, {
|
||||
AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
|
||||
BrowserUtils: "resource:///modules/BrowserUtils.jsm",
|
||||
BrowserUtils: "resource:///modules/BrowserUtils.sys.mjs",
|
||||
ChromeManifest: "resource:///modules/ChromeManifest.sys.mjs",
|
||||
Overlays: "resource:///modules/Overlays.sys.mjs",
|
||||
PrefUtils: "resource:///modules/PrefUtils.jsm",
|
||||
PrefUtils: "resource:///modules/PrefUtils.sys.mjs",
|
||||
PrivateTab: "resource:///modules/PrivateTab.sys.mjs",
|
||||
StatusBar: "resource:///modules/StatusBar.sys.mjs",
|
||||
TabFeatures: "resource:///modules/TabFeatures.sys.mjs",
|
||||
|
||||
@@ -2,32 +2,20 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/* global */
|
||||
import { CustomizableUI } from "resource:///modules/CustomizableUI.sys.mjs";
|
||||
import { PanelMultiView } from "resource:///modules/PanelMultiView.sys.mjs";
|
||||
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
|
||||
|
||||
const EXPORTED_SYMBOLS = ["BrowserUtils"];
|
||||
|
||||
const { XPCOMUtils } = ChromeUtils.import(
|
||||
"resource://gre/modules/XPCOMUtils.jsm"
|
||||
);
|
||||
|
||||
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
const { CustomizableUI } = ChromeUtils.import(
|
||||
"resource:///modules/CustomizableUI.jsm"
|
||||
);
|
||||
|
||||
const { PanelMultiView } = ChromeUtils.import(
|
||||
"resource:///modules/PanelMultiView.jsm"
|
||||
);
|
||||
const lazy = {};
|
||||
|
||||
XPCOMUtils.defineLazyServiceGetter(
|
||||
this,
|
||||
lazy,
|
||||
"styleSheetService",
|
||||
"@mozilla.org/content/style-sheet-service;1",
|
||||
"nsIStyleSheetService"
|
||||
);
|
||||
|
||||
const BrowserUtils = {
|
||||
export const BrowserUtils = {
|
||||
// internal functions/props
|
||||
get mostRecentWindow() {
|
||||
return Services.wm.getMostRecentWindow("navigator:browser");
|
||||
@@ -38,8 +26,8 @@ const BrowserUtils = {
|
||||
},
|
||||
createElement(aDoc, aTag, aAttrs) {
|
||||
// Create element
|
||||
let el = aDoc.createXULElement(aTag);
|
||||
for (let att in aAttrs) {
|
||||
const el = aDoc.createXULElement(aTag);
|
||||
for (const att in aAttrs) {
|
||||
// don't set null attrs
|
||||
if (aAttrs[att]) {
|
||||
el.setAttribute(att, aAttrs[att]);
|
||||
@@ -50,9 +38,9 @@ const BrowserUtils = {
|
||||
|
||||
// api endpoints
|
||||
createAndPositionElement(aWindow, aTag, aAttrs, aAdjacentTo, aPosition) {
|
||||
let doc = aWindow.document;
|
||||
const doc = aWindow.document;
|
||||
// Create element
|
||||
let el = this.createElement(doc, aTag, aAttrs);
|
||||
const el = this.createElement(doc, aTag, aAttrs);
|
||||
// Place it in certain location
|
||||
let pos = doc.getElementById(aAdjacentTo);
|
||||
if (aPosition) {
|
||||
@@ -65,7 +53,7 @@ const BrowserUtils = {
|
||||
if (pos) {
|
||||
pos.insertAdjacentElement(aPosition, el);
|
||||
}
|
||||
} else if (aAdjacentTo == "gNavToolbox") {
|
||||
} else if (aAdjacentTo === "gNavToolbox") {
|
||||
aWindow.gNavToolbox.appendChild(el);
|
||||
} else {
|
||||
pos.appendChild(el);
|
||||
@@ -76,21 +64,23 @@ const BrowserUtils = {
|
||||
* Helper function to execute a given function with some args in every open browser window.
|
||||
* Window must be the functions first arg, subsequent args are passed in the same manner
|
||||
* as to executeInAllWindows().
|
||||
*
|
||||
* @param func - The function to be called in each open browser window.
|
||||
* @param args - The arguments to supply to the function.
|
||||
* Example:
|
||||
* BrowserUtils.executeInAllWindows(Urlbar.addDynamicStylesheet, "chrome://browser/skin/waterfox.css")
|
||||
*/
|
||||
executeInAllWindows(func, ...args) {
|
||||
let windows = Services.wm.getEnumerator("navigator:browser");
|
||||
const windows = Services.wm.getEnumerator("navigator:browser");
|
||||
while (windows.hasMoreElements()) {
|
||||
let window = windows.getNext();
|
||||
const window = windows.getNext();
|
||||
func(window, ...args);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Helper method to register or unregister a given stylesheet depending on the bool arg passed.
|
||||
*
|
||||
* @param uri - The URI of the stylesheet to register or unregister.
|
||||
* @param enabled - A boolean indicating whether to register or unregister the sheet.
|
||||
*/
|
||||
@@ -103,26 +93,36 @@ const BrowserUtils = {
|
||||
},
|
||||
|
||||
registerStylesheet(uri) {
|
||||
let url = Services.io.newURI(uri);
|
||||
let type = styleSheetService.USER_SHEET;
|
||||
styleSheetService.loadAndRegisterSheet(url, type);
|
||||
if (!this.sheetRegistered(uri)) {
|
||||
const url = Services.io.newURI(uri);
|
||||
const type = lazy.styleSheetService.USER_SHEET;
|
||||
lazy.styleSheetService.loadAndRegisterSheet(url, type);
|
||||
}
|
||||
},
|
||||
|
||||
unregisterStylesheet(uri) {
|
||||
let url = Services.io.newURI(uri);
|
||||
let type = styleSheetService.USER_SHEET;
|
||||
styleSheetService.unregisterSheet(url, type);
|
||||
if (this.sheetRegistered(uri)) {
|
||||
const url = Services.io.newURI(uri);
|
||||
const type = lazy.styleSheetService.USER_SHEET;
|
||||
lazy.styleSheetService.unregisterSheet(url, type);
|
||||
}
|
||||
},
|
||||
|
||||
sheetRegistered(uri) {
|
||||
const url = Services.io.newURI(uri);
|
||||
const type = lazy.styleSheetService.USER_SHEET;
|
||||
return lazy.styleSheetService.sheetRegistered(url, type);
|
||||
},
|
||||
|
||||
setStyle(aStyleSheet) {
|
||||
let styleSheetService = Cc[
|
||||
const styleSheetService = Cc[
|
||||
"@mozilla.org/content/style-sheet-service;1"
|
||||
].getService(Ci.nsIStyleSheetService);
|
||||
|
||||
let url = Services.io.newURI(
|
||||
"data:text/css;charset=UTF-8," + encodeURIComponent(aStyleSheet)
|
||||
const url = Services.io.newURI(
|
||||
`data:text/css;charset=UTF-8,${encodeURIComponent(aStyleSheet)}`
|
||||
);
|
||||
let type = styleSheetService.USER_SHEET;
|
||||
const type = styleSheetService.USER_SHEET;
|
||||
|
||||
styleSheetService.loadAndRegisterSheet(url, type);
|
||||
},
|
||||
@@ -2,18 +2,14 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
const EXPORTED_SYMBOLS = ["PrefUtils"];
|
||||
|
||||
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
var PrefUtils = {
|
||||
export const PrefUtils = {
|
||||
get(prefPath, valueIfUndefined, def = false, setDefault = true) {
|
||||
let sPrefs = def ? Services.prefs.getDefaultBranch(null) : Services.prefs;
|
||||
const sPrefs = def ? Services.prefs.getDefaultBranch(null) : Services.prefs;
|
||||
|
||||
try {
|
||||
switch (sPrefs.getPrefType(prefPath)) {
|
||||
case 0:
|
||||
if (valueIfUndefined != undefined) {
|
||||
if (valueIfUndefined !== undefined) {
|
||||
return this.set(prefPath, valueIfUndefined, setDefault);
|
||||
}
|
||||
return undefined;
|
||||
@@ -24,12 +20,12 @@ var PrefUtils = {
|
||||
case 128:
|
||||
return sPrefs.getBoolPref(prefPath);
|
||||
}
|
||||
} catch (ex) {}
|
||||
} catch (_ex) {}
|
||||
return undefined;
|
||||
},
|
||||
|
||||
set(prefPath, value, def = false) {
|
||||
let sPrefs = def ? Services.prefs.getDefaultBranch(null) : Services.prefs;
|
||||
const sPrefs = def ? Services.prefs.getDefaultBranch(null) : Services.prefs;
|
||||
|
||||
switch (typeof value) {
|
||||
case "string":
|
||||
@@ -43,7 +39,7 @@ var PrefUtils = {
|
||||
},
|
||||
|
||||
lock(prefPath, value) {
|
||||
let sPrefs = Services.prefs;
|
||||
const sPrefs = Services.prefs;
|
||||
this.lockedBackupDef[prefPath] = this.get(prefPath, true);
|
||||
if (sPrefs.prefIsLocked(prefPath)) {
|
||||
sPrefs.unlockPref(prefPath);
|
||||
@@ -57,8 +53,8 @@ var PrefUtils = {
|
||||
|
||||
unlock(prefPath) {
|
||||
Services.prefs.unlockPref(prefPath);
|
||||
let bkp = this.lockedBackupDef[prefPath];
|
||||
if (bkp == undefined) {
|
||||
const bkp = this.lockedBackupDef[prefPath];
|
||||
if (bkp === undefined) {
|
||||
Services.prefs.deleteBranch(prefPath);
|
||||
} else {
|
||||
this.set(prefPath, bkp, true);
|
||||
@@ -68,9 +64,8 @@ var PrefUtils = {
|
||||
clear: Services.prefs.clearUserPref,
|
||||
|
||||
addObserver(aPrefPath, aCallback) {
|
||||
this.observer = function(aSubject, aTopic, prefPath) {
|
||||
return aCallback(PrefUtils.get(prefPath), prefPath);
|
||||
};
|
||||
this.observer = (_aSubject, _aTopic, prefPath) =>
|
||||
aCallback(PrefUtils.get(prefPath), prefPath);
|
||||
|
||||
Services.prefs.addObserver(aPrefPath, this.observer);
|
||||
return {
|
||||
@@ -5,8 +5,8 @@
|
||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
EXTRA_JS_MODULES += [
|
||||
"BrowserUtils.jsm",
|
||||
"PrefUtils.jsm",
|
||||
"BrowserUtils.sys.mjs",
|
||||
"PrefUtils.sys.mjs",
|
||||
]
|
||||
|
||||
BROWSER_CHROME_MANIFESTS += [
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
"use strict";
|
||||
|
||||
// Test setting and getting different types of pref
|
||||
add_task(async function testGetSetPrefs() {
|
||||
// String pref
|
||||
PrefUtils.set(STRING_PREF, "some string");
|
||||
let strPref = PrefUtils.get(STRING_PREF);
|
||||
const strPref = PrefUtils.get(STRING_PREF);
|
||||
is(typeof strPref, "string", "String pref is string");
|
||||
is(strPref, "some string", "String pref is set");
|
||||
|
||||
// Int pref
|
||||
PrefUtils.set(INT_PREF, 999);
|
||||
let intPref = PrefUtils.get(INT_PREF);
|
||||
const intPref = PrefUtils.get(INT_PREF);
|
||||
is(typeof intPref, "number", "Int pref is int");
|
||||
is(intPref, 999, "Int pref is set");
|
||||
|
||||
// Bool pref
|
||||
PrefUtils.set(BOOL_PREF, false);
|
||||
let boolPref = PrefUtils.get(BOOL_PREF);
|
||||
const boolPref = PrefUtils.get(BOOL_PREF);
|
||||
is(typeof boolPref, "boolean", "Bool pref is bool");
|
||||
is(boolPref, false, "Bool pref is set");
|
||||
|
||||
@@ -28,13 +26,13 @@ add_task(async function testGetSetPrefs() {
|
||||
|
||||
// Test observing a pref
|
||||
add_task(async function testObservePref() {
|
||||
let msg = "Callback succeeded";
|
||||
const msg = "Callback succeeded";
|
||||
|
||||
// Set up the observer
|
||||
async function callback(pref, path) {
|
||||
async function callback(_pref, _path) {
|
||||
Services.prefs.setCharPref(STRING_PREF, msg);
|
||||
}
|
||||
let obs = PrefUtils.addObserver(BOOL_PREF, callback);
|
||||
const obs = PrefUtils.addObserver(BOOL_PREF, callback);
|
||||
|
||||
// Trigger the obs callback
|
||||
is(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
const { PrefUtils } = ChromeUtils.importESModule(
|
||||
"resource:///modules/PrefUtils.sys.mjs"
|
||||
);
|
||||
|
||||
const { PrefUtils } = ChromeUtils.import("resource:///modules/PrefUtils.jsm");
|
||||
|
||||
const STRING_PREF = "browser.test.stringPref";
|
||||
const INT_PREF = "browser.test.intPref";
|
||||
const BOOL_PREF = "browser.test.boolPref";
|
||||
const _STRING_PREF = "browser.test.stringPref";
|
||||
const _INT_PREF = "browser.test.intPref";
|
||||
const _BOOL_PREF = "browser.test.boolPref";
|
||||
|
||||
Reference in New Issue
Block a user