benzBrake / FirefoxCustomize

Ryan 收集的 Firefox 个性化相关资源
150 stars 33 forks source link

[Bug]: Multiple bugs with addToolbarInsideLocationBar.uc.js #16

Closed Sneakpeakcss closed 1 year ago

Sneakpeakcss commented 1 year ago

What browser are you using?

Firefox

Browser version

ESR 102.8.0 / Nightly 112.0a1

What UC scripts loader are you using?

xiaoxiaoflood / MrOtherGuy(fx-autoconfig)

What's wrong with the script?

1

Script removes "Paste and Go" option from urlbar context menu when flexible space doesn't exist between "Go forwards button" and "Location Bar", additionally "Reload button" is required to always be placed before the flexible space, the moment either of them are removed the "Paste and Go" also dissapears:

https://user-images.githubusercontent.com/77424331/222953281-769fe54e-28f6-4c2c-9977-1227d9880722.mp4

While opening "Customise Toolbar" brings it back, it doesn't persist after firefox restart unless the previously mentioned conditions are met.

It seems to have something to do with this part of the code:

https://github.com/benzBrake/FirefoxCustomize/blob/13a59a912cfd2097a716a494253be2ce9f1cc44a/userChromeJS/addToolbarInsideLocationBar.uc.js#L181-L194

Deleting this part stops the script from removing "Paste and Go" no matter the conditions and script seemingly works without any problems at first glance.

 

2

The moment "Customise Toolbar" is opened some of the placed buttons stop working until Firefox restart:

https://user-images.githubusercontent.com/77424331/222953568-5331e7db-91be-4938-a235-22ad2215ff5e.mp4

TypeError: can't access property "area", this.getPlacementOfWidget(...) is null3 [CustomizableUI.jsm:2141:21](resource:///modules/CustomizableUI.jsm)
    showWidgetView resource:///modules/CustomizableUI.jsm:2141
    handleWidgetCommand resource:///modules/CustomizableUI.jsm:2184

 

3

This might've never been the intent of this script, but it's possible to place Searchbar into the locationBar, but it disappears after Firefox restart and requires to open "Customise Toolbar" 2 times for it to show again. There's also slight misalignment of buttons when Searchbar is placed there.

https://user-images.githubusercontent.com/77424331/222953949-17784ff7-8a73-4f35-9a2d-b3cc4f47baa6.mp4

 

 

Also as a slight BTW the source of dav_LinkifiesLocationBar.uc.js is here. It doesn't link to anything on the table and 'reviewURL' in the script links to camp-firefox.de forums where the code was pasted.

benzBrake commented 1 year ago

I have mod a script from statusbar.uc.js serval weeks ago, but no one here seems to be using it. try it(I can't guarantee perfect work).

// ==UserScript==
// @name            LocationBar.uc.js
// @description     地址栏内工具栏
// @license         MIT License
// @compatibility   Firefox 107
// @version         0.0.1
// @charset         UTF-8
// @include         chrome://browser/content/browser.xul
// @include         chrome://browser/content/browser.xhtml
// @homepageURL     https://github.com/benzBrake/FirefoxCustomize/tree/master/userChromeJS
// @note            参考自 Floorp 浏览器的状态栏脚本
// ==/UserScript==
(function (css) {
    const Services = globalThis.Services || Cu.import("resource://gre/modules/Services.jsm").Services;
    const MENU_LABEL = "地址栏快捷工具";

    window.LocationBar = {
        init: function () {
            const toolbarElem = window.MozXULElement.parseXULToFragment(
                `
            <toolbar id="location-bar" customizable="true"
                class="browser-toolbar customization-target" mode="icons" context="toolbar-context-menu" accesskey="A">
            </toolbar>
            `
            );

            //insert style
            this.style = addStyle(css);

            document.getElementById("navigator-toolbox").appendChild(toolbarElem);

            CustomizableUI.registerArea("location-bar", {
                type: CustomizableUI.TYPE_TOOLBAR
            });

            CustomizableUI.registerToolbarNode(document.getElementById("location-bar"));

            //move elem into urlbar
            document.getElementById("page-action-buttons").after(document.getElementById("location-bar"));

            //menuitem for status bar
            let toggleItem = $C("menuitem", {
                id: "toggle_location-bar",
                label: MENU_LABEL,
                type: "checkbox",
                accesskey: "S",
                checked: String(Services.prefs.getBoolPref("browser.display.locationbar", false)),
                oncommand: "LocationBar.togglePref();",
            });
            document.getElementById("toolbarItemsMenuSeparator").after(toggleItem);

            let checked = Services.prefs.getBoolPref("browser.display.locationbar", false);
            document.getElementById("toggle_location-bar").setAttribute("checked", String(checked));
            if (checked) {
                this.show();
            } else {
                this.hide();
            }

            Services.prefs.addObserver("browser.display.locationbar", function () {
                let checked = Services.prefs.getBoolPref("browser.display.locationbar", false);
                document.getElementById("toggle_location-bar").setAttribute("checked", String(checked));
                if (checked) {
                    LocationBar.show();
                } else {
                    LocationBar.hide();
                }
            });
            window.addEventListener("beforecustomization", this, false);
            window.addEventListener("aftercustomization", this, false);
        },
        togglePref: function () {
            let checked = document.getElementById("toggle_location-bar").getAttribute("checked") == "true";
            Services.prefs.setBoolPref("browser.display.locationbar", checked);
        },
        show: function () {
            document.getElementById("location-bar").classList.remove("optional-hidden");
        },
        hide: function () {
            document.getElementById("location-bar").classList.add("optional-hidden");
        },
        handleEvent: function (event) {
            switch (event.type) {
                case "beforecustomization":
                    document.getElementById("nav-bar").appendChild(document.getElementById("location-bar"));
                    break;
                case "aftercustomization":
                    document.getElementById("page-action-buttons").after(document.getElementById("location-bar"));
                    break;
            }
        }
    }

    function $C(name, attr) {
        const appVersion = Services.appinfo.version.split(".")[0];
        attr || (attr = {});
        var el;
        if (appVersion >= 69) {
            el = document.createXULElement(name);
        } else {
            el = document.createElement(name);
        }
        if (attr) Object.keys(attr).forEach(function (n) {
            el.setAttribute(n, attr[n])
        });
        return el;
    }

    function addStyle(css) {
        var pi = document.createProcessingInstruction(
            'xml-stylesheet',
            'type="text/css" href="data:text/css;utf-8,' + encodeURIComponent(css) + '"'
        );
        return document.insertBefore(pi, document.documentElement);
    }

    if (gBrowserInit.delayedStartupFinished) window.LocationBar.init();
    else {
        let delayedListener = (subject, topic) => {
            if (topic == "browser-delayed-startup-finished" && subject == window) {
                Services.obs.removeObserver(delayedListener, topic);
                window.LocationBar.init();
            }
        };
        Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");
    }
})(`
#urlbar-input-container .optional-hidden {
    visibility: collapse;
}
`)
Sneakpeakcss commented 1 year ago

(..) but no one here seems to be using it. try it(I can't guarantee perfect work).

It actually works! In both Nightly and ESR (xiaoxiaoflood loader) without any of the mentioned problems, thanks for that.

I can't find it on this repository at all so this might be why no one is using it, might be worth it to replace addToolbarInsideLocationBar with this one on the table.

In this case this can be closed, unless you are interested in still maintaining and fixing those bugs in addToolbarInsideLocationBar.uc.js for some reason.

benzBrake commented 1 year ago

I can't find it on this repository at all so this might be why no one is using it.

I used wrong word here, it means a forum where I often share scripts. No one was interested in location bar in urlbar, so I have not uploaded it here.