Aris-t2 / CustomJSforFx

custom scripts
GNU General Public License v3.0
239 stars 22 forks source link

[Bug Fx release/beta] Addon buttons do not work with multiples windows (addon_buttons_in_overflow_menu.uc.js) #80

Open sdavidg opened 1 year ago

sdavidg commented 1 year ago

document.getElementById("unified-extensions-area") returns null when you open a new window.

sdavidg commented 1 year ago

This issue is about https://github.com/Aris-t2/CustomJSforFx/blob/master/scripts/addon_buttons_in_overflow_menu.uc.js

Aris-t2 commented 1 year ago

Not sure, what exactly causes this to fail, but I'm looking into to.

Current status: Nightly 113: works fine in multi-window environment Beta 112: does not work at all Release 111: works on first window only

sdavidg commented 1 year ago

Beta 112 works on first window if you set a delay:

setTimeout(function/(){
    let wofl = document.getElementById("widget-overflow-fixed-list");
    let uea = document.getElementById("unified-extensions-area");
    console.log(wofl, uea);
}, 1000)

I suppose the "document" is not ready when the script is executed, but uea is null on new windows.

But if you use this code

setTimeout(function() {
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                .getService(Components.interfaces.nsIWindowMediator);
    var ws = wm.getEnumerator(null);
    while(ws.hasMoreElements()) {
        let w = ws.getNext();
        let wofl = w.document.getElementById("widget-overflow-fixed-list");
        let uea = w.document.getElementById("unified-extensions-area");
        console.log("window", wofl, uea);
    }   
}, 1000);

uea is ok on first window, null on second window, but if you open a third window, is ok on first and second. uea is null on the last window

Aris-t2 commented 1 year ago

Did some testing based on the above code. Sometimes it refuses to work on any window, sometimes it works on more then 5 windows. SetInverval is a dirty function to achieve this, but it at least offers some kind of results on beta for me.

(function() {
/*
    try {
      document.getElementById("widget-overflow-fixed-list").appendChild(document.getElementById("unified-extensions-area"));
    } catch (e) {}
*/  
setInterval(function() {
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                .getService(Components.interfaces.nsIWindowMediator);
    var ws = wm.getEnumerator(null);
    while(ws.hasMoreElements()) {
        let w = ws.getNext();
        let wofl = w.document.getElementById("widget-overflow-fixed-list");
        let uea = w.document.getElementById("unified-extensions-area");
        //console.log("window", wofl, uea);

        try {
          wofl.appendChild(uea);
        } catch (e) {}
    }   
}, 1000);

setInterval(function() {
    // style sheet
    Components.classes['@mozilla.org/content/style-sheet-service;1'].getService(Components.interfaces.nsIStyleSheetService).loadAndRegisterSheet(
      Services.io.newURI('data:text/css;charset=utf-8,' + encodeURIComponent(`
          #unified-extensions-area .unified-extensions-item-contents, 
          #unified-extensions-area toolbarbutton + toolbarbutton.subviewbutton {
            display: none !important;
          }
          #unified-extensions-area toolbarbutton .toolbarbutton-text {
            display: flex !important;
          }
          #unified-extensions-area .toolbarbutton-icon {
            width: 16px !important;
            height: 16px !important;
          }
          #unified-extensions-button {
            position: absolute !important;
            right: 0 !important;
            opacity: 0 !important;
            z-index: -1000 !important;
          }
      `), null, null),
      Components.classes['@mozilla.org/content/style-sheet-service;1'].getService(Components.interfaces.nsIStyleSheetService).AGENT_SHEET
    );
}, 1100);

}());
sdavidg commented 1 year ago

A code proposal canceling the interval

// ==UserScript==
// @name            addon_buttons_in_overflow_menu
// @author          Aris
// @version         1.0
// @homepage        https://raw.githubusercontent.com/Aris-t2/CustomJSforFx/master/scripts/addon_buttons_in_overflow_menu.uc.js
// ==/UserScript==

// 'Add-on buttons in overflow menu' script for Firefox 111+ by Aris
// At least one default toolbar button has to be inside overflow menu for it to show up on navigation toolbar.
// Pin buttons to toolbar or move buttons to overflow menu using 'right-click' context menus 'Pin to Toolbar'.
// Unified extension button gets hidden and moved to toolbars end for extension popups to appear there.

setTimeout(function() {

    var idInterval = setInterval(function() {
        let wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                    .getService(Components.interfaces.nsIWindowMediator);
        let ws = wm.getEnumerator(null);
        let allWinOK = true;
        while(ws.hasMoreElements()) {
            let w = ws.getNext();
            if(!w.addon_buttons_in_overflow_menu){
                let wofl = w.document.getElementById("widget-overflow-fixed-list");
                let uea = w.document.getElementById("unified-extensions-area");
                if(wofl && uea){
                    wofl.appendChild(uea);
                    addStyle();
                    w.addon_buttons_in_overflow_menu = true;
                }
            }

            allWinOK = allWinOK && w.addon_buttons_in_overflow_menu;
        }

        if(allWinOK){
            clearInterval(idInterval);
        }
    }, 2000);

    function addStyle(){
        Components.classes['@mozilla.org/content/style-sheet-service;1'].getService(Components.interfaces.nsIStyleSheetService).loadAndRegisterSheet(
            Services.io.newURI('data:text/css;charset=utf-8,' + encodeURIComponent(`
                #unified-extensions-area .unified-extensions-item-contents, 
                #unified-extensions-area toolbarbutton + toolbarbutton.subviewbutton {
                    display: none !important;
                }
                #unified-extensions-area toolbarbutton .toolbarbutton-text {
                    display: flex !important;
                }
                #unified-extensions-area .toolbarbutton-icon {
                    width: 16px !important;
                    height: 16px !important;
                }
                #unified-extensions-button {
                    position: absolute !important;
                    right: 0 !important;
                    opacity: 0 !important;
                    z-index: -1000 !important;
                }
            `), null, null),
            Components.classes['@mozilla.org/content/style-sheet-service;1'].getService(Components.interfaces.nsIStyleSheetService).AGENT_SHEET
        );
    }
}, 1000);   
sdavidg commented 1 year ago

I'm not sure it's right to close it

Aris-t2 commented 1 year ago

Let leave this open for now.

The original script works fine on Nightly (113) without intervals and timeouts on as many windows I open. We will see what happens once 113 will be on release channel.

mzso commented 1 year ago

Well, it works. Though this way it's little different from unified menu. (It lacks some features compared to it, on the other hand it's not cluttered with disabled items.) I either have clutter of unwanted icons in the overflow menu, or on the toolbar. I can't remove unwanted ones to configure mode's repository of elements. It thought movability was just some attribute that may be restored/forced-on toolbar buttons.