Skip to content

Commit

Permalink
Updated fx_autoconfig from 0.10.1 to 0.10.4
Browse files Browse the repository at this point in the history
Why was this update needed?
- This fixes userScript menu's menuitems not working when clicked on.
  • Loading branch information
angelbruni committed Feb 5, 2025
1 parent aff46bc commit 86d3615
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 48 deletions.
51 changes: 30 additions & 21 deletions Profile Folder/chrome/utils/boot.sys.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
import { loaderModuleLink, Pref, FileSystem, windowUtils, showNotification, startupFinished, restartApplication, escapeXUL } from "chrome://userchromejs/content/utils.sys.mjs";
import { loaderModuleLink, Pref, FileSystem, windowUtils, showNotification, startupFinished, restartApplication, escapeXUL, toggleScript } from "chrome://userchromejs/content/utils.sys.mjs";

const FX_AUTOCONFIG_VERSION = "0.10.1";
const FX_AUTOCONFIG_VERSION = "0.10.4";
console.warn( "Browser is executing custom scripts via autoconfig" );

const APP_VARIANT = (() => {
Expand Down Expand Up @@ -306,15 +306,7 @@ function maybeShowBrokenNotification(window){
label: "fx-autoconfig: Startup might be broken",
image: "chrome://browser/skin/notification-icons/popup.svg",
priority: "critical"
},
[{
label: "Enable workaround",
callback: (notification) => {
Services.prefs.setBoolPref("userChromeJS.gBrowser_hack.required",true);
restartApplication(false);
return false
}
}]
}
);
}

Expand All @@ -330,7 +322,7 @@ function updateMenuStatus(event){
if(item.getAttribute("type") != "checkbox"){
continue
}
if (disabledScripts.includes(item.getAttribute("filename"))){
if (disabledScripts.includes(item.dataset.filename)){
item.removeAttribute("checked");
}else{
item.setAttribute("checked","true");
Expand Down Expand Up @@ -415,7 +407,7 @@ class UserChrome_js{
this.registerScript(style,!disabledScripts.includes(style.filename));
}
}
this.addAgentStyles(this.styles.filter(style => style.styleSheetMode === "agent"));
this.addAgentStyles(this.styles.filter(style => style.styleSheetMode === "agent" && !disabledScripts.includes(style.filename)));
}
this.scripts.sort((a,b) => a.loadOrder - b.loadOrder);
this.styles.sort((a,b) => a.loadOrder - b.loadOrder);
Expand Down Expand Up @@ -522,9 +514,9 @@ class UserChrome_js{
<menu id="userScriptsMenu" label="userScripts">
<menupopup id="menuUserScriptsPopup">
<menuseparator></menuseparator>
<menuitem id="userScriptsMenu-OpenFolder" label="Open folder" oncommand="UC_API.Scripts.openScriptDir()"></menuitem>
<menuitem id="userScriptsMenu-Restart" label="Restart" oncommand="UC_API.Runtime.restart(false)" tooltiptext="Toggling scripts requires restart"></menuitem>
<menuitem id="userScriptsMenu-ClearCache" label="Restart and clear startup cache" oncommand="UC_API.Runtime.restart(true)" tooltiptext="Toggling scripts requires restart"></menuitem>
<menuitem id="userScriptsMenu-OpenFolder" label="Open folder"></menuitem>
<menuitem id="userScriptsMenu-Restart" label="Restart" tooltiptext="Toggling scripts requires restart"></menuitem>
<menuitem id="userScriptsMenu-ClearCache" label="Restart and clear startup cache" tooltiptext="Toggling scripts requires restart"></menuitem>
</menupopup>
</menu>
`);
Expand All @@ -541,9 +533,27 @@ class UserChrome_js{
if(!this.IS_ENABLED){
itemsFragment.append(window.MozXULElement.parseXULToFragment('<menuitem label="&lt;fx-autoconfig is disabled&gt;" disabled="true"></menuitem>'));
}
menuFragment.getElementById("menuUserScriptsPopup").prepend(itemsFragment);
let menupopup = menuFragment.getElementById("menuUserScriptsPopup");
menupopup.prepend(itemsFragment);
popup.prepend(menuFragment);
popup.querySelector("#menuUserScriptsPopup").addEventListener("popupshown",updateMenuStatus);
menupopup.addEventListener("popupshown",updateMenuStatus);
menupopup.addEventListener("command",ev => {
switch(ev.target.id){
case "userScriptsMenu-OpenFolder":
FileSystem.getScriptDir().showInFileManager();
break;
case "userScriptsMenu-Restart":
restartApplication(false);
break;
case "userScriptsMenu-ClearCache":
restartApplication(true);
break;
default:
if(ev.target.dataset.filename){
toggleScript(ev.target.dataset.filename);
}
}
});
aDoc.l10n.formatValues(["restart-button-label","clear-startup-cache-label","show-dir-label"])
.then(values => {
let baseTitle = `${values[0]} ${BRAND_NAME}`;
Expand All @@ -558,9 +568,8 @@ class UserChrome_js{
aWindow.MozXULElement.parseXULToFragment(`
<menuitem type="checkbox"
label="${escapeXUL(aScript.name || aScript.filename)}"
filename="${escapeXUL(aScript.filename)}"
checked="true"
oncommand="UC_API.Scripts.toggleScript(this)">
data-filename="${escapeXUL(aScript.filename)}"
checked="true">
</menuitem>
`)
);
Expand Down
65 changes: 38 additions & 27 deletions Profile Folder/chrome/utils/utils.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ export class Hotkey{
constructor(hotkeyDetails,commandDetails){
this.command = commandDetails;
this.trigger = hotkeyDetails;
this.#matchingSelector = `key[modifiers="${hotkeyDetails.modifiers}"][${hotkeyDetails.key?'key="'+hotkeyDetails.key:'keycode="'+hotkeyDetails.keycode}"]`;
this.#matchingSelector = null;
}
get matchingSelector(){
if(!this.#matchingSelector){
let trigger = this.trigger;
this.#matchingSelector = `key[modifiers="${trigger.modifiers}"][${trigger.key?'key="'+trigger.key:'keycode="'+trigger.keycode}"]`
}
return this.#matchingSelector
}
autoAttach(opt){
async autoAttach(opt){
const suppress = opt?.suppressOriginal || false;
await startupFinished();
for (let window of windowUtils.getAll()){
if(window.document.getElementById(this.trigger.id)){
continue
Expand All @@ -42,13 +47,13 @@ export class Hotkey{
}
}
suppressOriginalKey(window){
let oldKey = window.document.querySelector(this.#matchingSelector);
let oldKey = window.document.querySelector(this.matchingSelector);
if(oldKey){
oldKey.setAttribute("disabled","true")
}
}
restoreOriginalKey(window){
let oldKey = window.document.querySelector(this.#matchingSelector);
let oldKey = window.document.querySelector(this.matchingSelector);
oldKey.removeAttribute("disabled");
}
static #createKey(doc,details){
Expand All @@ -72,43 +77,47 @@ export class Hotkey{
console.warn("Fx-autoconfig: command with id '"+details.id+"' already exists");
return
}
let command = createElement(doc,"command",{id: details.id,oncommand: "this._oncommand(event);"});
let command = createElement(doc,"command",{id: details.id});
commandSet.insertBefore(command,commandSet.firstChild||null);
const fun = details.command;
command._oncommand = (e) => fun(e.view,e);
command.addEventListener("command",ev => fun(ev.view,ev))
return
}
static ERR_KEY = 0;
static NORMAL_KEY = 1;
static FUN_KEY = 2;
static VK_KEY = 4;

static #getKeyCategory(key){
return (/^[\w-]$/).test(key)
? Hotkey.NORMAL_KEY
: (/^F(?:1[0,2]|[1-9])$/)
.test(key)
? Hotkey.FUN_KEY
: Hotkey.ERR_KEY
: (/^VK_[A-Z]+/).test(key)
? Hotkey.VK_KEY
: (/^F(?:1[0,1,2]|[1-9])$/).test(key)
? Hotkey.FUN_KEY
: Hotkey.ERR_KEY
}

static define(desc){
let keyCategory = Hotkey.#getKeyCategory(desc.key);
if(keyCategory === Hotkey.ERR_KEY){
throw new Error("Provided key '"+desc.key+"' is invalid")
}
if(keyCategory === Hotkey.FUN_KEY){
throw new Error("Registering a hotkey with no modifiers is not supported, except for function keys F1-F12")
}
let commandType = typeof desc.command;
if(!(commandType === "string" || commandType === "function")){
throw new TypeError("command must be either a string or function")
throw new Error("command must be either a string or function")
}
if(commandType === "function" && !desc.id){
throw new Error("command id must be specified when callback is a function")
}

const validMods = ["accel","alt","ctrl","meta","shift"];
const mods = desc.modifiers.toLowerCase().split(" ").filter(a => validMods.includes(a));
const mods = desc.modifiers?.toLowerCase().split(" ").filter(a => validMods.includes(a));
if(keyCategory === Hotkey.NORMAL_KEY && !(mods && mods.length > 0)){
throw new Error("Registering a hotkey with no modifiers is not supported, except for function keys F1-F12")
}
let keyDetails = {
id: desc.id,
modifiers: mods.join(",").replace("ctrl","accel"),
modifiers: mods?.join(",").replace("ctrl","accel") ?? "",
command: commandType === "string"
? desc.command
: `cmd_${desc.id}`
Expand All @@ -119,7 +128,7 @@ export class Hotkey{
if(keyCategory === Hotkey.NORMAL_KEY){
keyDetails.key = desc.key.toUpperCase();
}else{
keyDetails.keycode = `VK_${desc.key}`;
keyDetails.keycode = keyCategory === Hotkey.FUN_KEY ? `VK_${desc.key}` : desc.key;
}
return new Hotkey(
keyDetails,
Expand Down Expand Up @@ -604,8 +613,12 @@ export function createWidget(desc){
for (let p in props){
toolbaritem.setAttribute(p, props[p]);
}

if(typeof callback === "function"){
toolbaritem.setAttribute("onclick",`${desc.allEvents?"":"event.button===0 && "}UC_API.SharedStorage.widgetCallbacks.get(this.id)(event,window)`);
const allEvents = !!desc.allEvents;
toolbaritem.addEventListener("click",(ev) => {
allEvents || ev.button === 0 && SharedGlobal.widgetCallbacks.get(ev.target.id)(ev,ev.target.ownerGlobal)
})
}
for (let attr in desc){
if(attr != "callback" && !(attr in props)){
Expand Down Expand Up @@ -774,15 +787,13 @@ export function startupFinished(){
return new Promise(resolve => lazy.startupPromises.add(resolve))
}

export function toggleScript(el){
let isElement = !!el.tagName;
if(!isElement && typeof el != "string"){
return
export function toggleScript(aFilename){
if(typeof aFilename != "string"){
throw new Error("expected name of the script as string")
}
const name = isElement ? el.getAttribute("filename") : el;
let script = name.endsWith("js")
? getScriptData(name)
: getStyleData(name);
let script = aFilename.endsWith("js")
? getScriptData(aFilename)
: getStyleData(aFilename);
if(!script){
return null
}
Expand Down

0 comments on commit 86d3615

Please sign in to comment.