Skip to content

Commit

Permalink
minor changes, update a script.
Browse files Browse the repository at this point in the history
  • Loading branch information
aminomancer committed Jun 12, 2022
1 parent d1848e0 commit a414802
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 141 deletions.
282 changes: 146 additions & 136 deletions JS/openBookmarksHistoryEtcInNewTabs.uc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name Open Bookmarks, History, etc. in New Tabs
// @version 1.1
// @version 1.2.0
// @author aminomancer
// @homepage https://github.com/aminomancer/uc.css.js
// @description In vanilla Firefox, browser.tabs.loadBookmarksInTabs only affects bookmark items. When you enable this pref and left-click a bookmark (e.g., in the bookmarks toolbar or menu) it opens in a new tab instead of in the current tab. But if you left-click a history entry or a synced tab, it will still open in the current tab. So you'd have to middle click or ctrl+click to avoid losing your current tab's navigation state. This script just makes that preference apply to history and synced tabs too. It's relatively easy to do this because there isn't any technical reason it doesn't work for history or synced tabs. For whatever reason, the command handler for clicking these places nodes just intentionally checks if it's a bookmark and opens it in a new tab only if it passes the check. I guess this is probably because the preference was called "loadBookmarksInTabs" a long time ago, so making it affect other items might be confusing, but changing the name of a longstanding pref would also be confusing. So I guess Firefox is just stuck with the ramifications of a minor mistake somebody ostensibly made years ago, which is a perfect use case for an autoconfig script.
Expand All @@ -10,143 +10,153 @@
// ==/UserScript==

(function () {
function init() {
function getBrowserWindow(aWindow) {
return aWindow &&
aWindow.document.documentElement.getAttribute("windowtype") == "navigator:browser"
? aWindow
: BrowserWindowTracker.getTopWindow();
}
if (window.PlacesUIUtils && !PlacesUIUtils._hasBeenModifiedForOBHNT) {
eval(
`PlacesUIUtils.openNodeWithEvent = function ` +
PlacesUIUtils.openNodeWithEvent
.toSource()
.replace(/\(function PUIU_openNodeWithEvent/, "")
.replace(/ && PlacesUtils\.nodeIsBookmark\(aNode\)/, "")
.replace(/\)$/, "")
);
PlacesUIUtils._hasBeenModifiedForOBHNT = true;
}
if (window.HistoryMenu) {
let proto = HistoryMenu.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
proto._onCommand = function (e) {
e = getRootEvent(e);
let placesNode = e.target._placesNode;
if (placesNode) {
if (!PrivateBrowsingUtils.isWindowPrivate(window))
PlacesUIUtils.markPageAsTyped(placesNode.uri);
let where = whereToOpenLink(e, false, true);
if (PlacesUIUtils.loadBookmarksInTabs) {
if (where == "current") where = "tab";
if (where == "tab" && gBrowser.selectedTab.isEmpty) where = "current";
}
openUILinkIn(placesNode.uri, where, {
triggeringPrincipal:
Services.scriptSecurityManager.getSystemPrincipal(),
});
}
};
proto._onClick = function (e) {
let modifKey =
AppConstants.platform == "macosx"
? e.metaKey || e.shiftKey
: e.ctrlKey || e.shiftKey;
if (e.button == 2 || (e.button == 0 && !modifKey)) return;
let target = e.originalTarget;
let tag = target.tagName;
if (PlacesUIUtils.openInTabClosesMenu && (tag == "menuitem" || tag == "menu"))
closeMenus(e.target);
if (e.button == 1 && !(tag == "menuitem" || tag == "menu")) this.onCommand(e);
};
proto._onMouseUp = function (e) {
if (e.button == 2 || PlacesUIUtils.openInTabClosesMenu) return;
let target = e.originalTarget;
if (target.tagName != "menuitem") return;
let modifKey = AppConstants.platform === "macosx" ? e.metaKey : e.ctrlKey;
if (modifKey || e.button == 1) {
target.setAttribute("closemenu", "none");
let menupopup = target.parentNode;
menupopup.addEventListener(
"popuphidden",
() => target.removeAttribute("closemenu"),
{ once: true }
);
} else target.removeAttribute("closemenu");
};
let popup = document.getElementById("historyMenuPopup");
popup.setAttribute("onclick", `this.parentNode._placesView._onClick(event);`);
popup.setAttribute("onmouseup", `this.parentNode._placesView._onMouseUp(event);`);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (window.PlacesPanelview) {
let proto = PlacesPanelview.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
eval(
`proto._onCommand = function ` +
proto._onCommand
.toSource()
.replace(/_onCommand/, "")
.replace(
/(button\.parentNode\.id == \"panelMenu_bookmarksMenu\")/,
`$1 || button.parentNode.id == "appMenu_historyMenu"`
)
.replace(
/(button\.parentNode\.id != \"panelMenu_bookmarksMenu\")/,
`($1 && button.parentNode.id != "appMenu_historyMenu")`
)
);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (window.SyncedTabsPanelList) {
let proto = SyncedTabsPanelList.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
eval(
`proto._createSyncedTabElement = function ` +
proto._createSyncedTabElement
.toSource()
.replace(/_createSyncedTabElement/, "")
.replace(/document\.defaultView\.whereToOpenLink\(e\)/, "preWhere")
.replace(
/document\.defaultView\.openUILink\(tabInfo\.url, e,/,
`let where = document.defaultView.whereToOpenLink(e, false, true);\n let preWhere = where;\n if (document.defaultView.PlacesUIUtils.loadBookmarksInTabs) {\n if (where == "current") where = "tab";\n if (where == "tab" && document.defaultView.gBrowser.selectedTab.isEmpty) where = "current";\n }\n document.defaultView.openUILinkIn(tabInfo.url, where,`
)
);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (location.href === `chrome://browser/content/syncedtabs/sidebar.xhtml`) {
let proto = syncedTabsDeckComponent.tabListComponent._View.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
proto.onOpenSelected = function (url, e) {
let browserWindow = this._window.browsingContext.topChromeWindow;
let where = browserWindow.whereToOpenLink(e, false, true);
if (browserWindow.PlacesUIUtils.loadBookmarksInTabs) {
if (where == "current") where = "tab";
if (where == "tab" && browserWindow.gBrowser.selectedTab.isEmpty)
where = "current";
}
this.props.onOpenTab(url, where, {});
};
proto._hasBeenModifiedForOBHNT = true;
}
}
function init() {
if (window.PlacesUIUtils && !PlacesUIUtils._hasBeenModifiedForOBHNT) {
const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
CustomizableUI: "resource:///modules/CustomizableUI.jsm",
MigrationUtils: "resource:///modules/MigrationUtils.jsm",
OpenInTabsUtils: "resource:///modules/OpenInTabsUtils.jsm",
PlacesTransactions: "resource://gre/modules/PlacesTransactions.jsm",
PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
PluralForm: "resource://gre/modules/PluralForm.jsm",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
PromiseUtils: "resource://gre/modules/PromiseUtils.jsm",
Weave: "resource://services-sync/main.js",
});
XPCOMUtils.defineLazyGetter(lazy, "bundle", function () {
return Services.strings.createBundle("chrome://browser/locale/places/places.properties");
});
function getBrowserWindow(aWindow) {
return aWindow &&
aWindow.document.documentElement.getAttribute("windowtype") == "navigator:browser"
? aWindow
: BrowserWindowTracker.getTopWindow();
}
eval(
`PlacesUIUtils.openNodeWithEvent = function ` +
PlacesUIUtils.openNodeWithEvent
.toSource()
.replace(/\(function PUIU_openNodeWithEvent/, "")
.replace(/ && PlacesUtils\.nodeIsBookmark\(aNode\)/, "")
.replace(/\)$/, "")
);
PlacesUIUtils._hasBeenModifiedForOBHNT = true;
}
if (
location.href !== `chrome://browser/content/browser.xhtml` ||
gBrowserInit.delayedStartupFinished
)
init();
else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
init();
if (window.HistoryMenu) {
let proto = HistoryMenu.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
proto._onCommand = function (e) {
e = getRootEvent(e);
let placesNode = e.target._placesNode;
if (placesNode) {
if (!PrivateBrowsingUtils.isWindowPrivate(window))
PlacesUIUtils.markPageAsTyped(placesNode.uri);
let where = whereToOpenLink(e, false, true);
if (PlacesUIUtils.loadBookmarksInTabs) {
if (where == "current") where = "tab";
if (where == "tab" && gBrowser.selectedTab.isEmpty) where = "current";
}
openUILinkIn(placesNode.uri, where, {
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
});
}
};
proto._onClick = function (e) {
let modifKey =
AppConstants.platform == "macosx" ? e.metaKey || e.shiftKey : e.ctrlKey || e.shiftKey;
if (e.button == 2 || (e.button == 0 && !modifKey)) return;
let target = e.originalTarget;
let tag = target.tagName;
if (PlacesUIUtils.openInTabClosesMenu && (tag == "menuitem" || tag == "menu"))
closeMenus(e.target);
if (e.button == 1 && !(tag == "menuitem" || tag == "menu")) this.onCommand(e);
};
Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");
proto._onMouseUp = function (e) {
if (e.button == 2 || PlacesUIUtils.openInTabClosesMenu) return;
let target = e.originalTarget;
if (target.tagName != "menuitem") return;
let modifKey = AppConstants.platform === "macosx" ? e.metaKey : e.ctrlKey;
if (modifKey || e.button == 1) {
target.setAttribute("closemenu", "none");
let menupopup = target.parentNode;
menupopup.addEventListener("popuphidden", () => target.removeAttribute("closemenu"), {
once: true,
});
} else target.removeAttribute("closemenu");
};
let popup = document.getElementById("historyMenuPopup");
popup.setAttribute("onclick", `this.parentNode._placesView._onClick(event);`);
popup.setAttribute("onmouseup", `this.parentNode._placesView._onMouseUp(event);`);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (window.PlacesPanelview) {
let proto = PlacesPanelview.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
eval(
`proto._onCommand = function ` +
proto._onCommand
.toSource()
.replace(/_onCommand/, "")
.replace(
/(button\.parentNode\.id == \"panelMenu_bookmarksMenu\")/,
`$1 || button.parentNode.id == "appMenu_historyMenu"`
)
.replace(
/(button\.parentNode\.id != \"panelMenu_bookmarksMenu\")/,
`($1 && button.parentNode.id != "appMenu_historyMenu")`
)
);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (window.SyncedTabsPanelList) {
let proto = SyncedTabsPanelList.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
eval(
`proto._createSyncedTabElement = function ` +
proto._createSyncedTabElement
.toSource()
.replace(/_createSyncedTabElement/, "")
.replace(/document\.defaultView\.whereToOpenLink\(e\)/, "preWhere")
.replace(
/document\.defaultView\.openUILink\(tabInfo\.url, e,/,
`let where = document.defaultView.whereToOpenLink(e, false, true);\n let preWhere = where;\n if (document.defaultView.PlacesUIUtils.loadBookmarksInTabs) {\n if (where == "current") where = "tab";\n if (where == "tab" && document.defaultView.gBrowser.selectedTab.isEmpty) where = "current";\n }\n document.defaultView.openUILinkIn(tabInfo.url, where,`
)
);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (location.href === `chrome://browser/content/syncedtabs/sidebar.xhtml`) {
let proto = syncedTabsDeckComponent.tabListComponent._View.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
proto.onOpenSelected = function (url, e) {
let browserWindow = this._window.browsingContext.topChromeWindow;
let where = browserWindow.whereToOpenLink(e, false, true);
if (browserWindow.PlacesUIUtils.loadBookmarksInTabs) {
if (where == "current") where = "tab";
if (where == "tab" && browserWindow.gBrowser.selectedTab.isEmpty) where = "current";
}
this.props.onOpenTab(url, where, {});
};
proto._hasBeenModifiedForOBHNT = true;
}
}
}
if (
location.href !== `chrome://browser/content/browser.xhtml` ||
gBrowserInit.delayedStartupFinished
)
init();
else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
init();
}
};
Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");
}
})();
18 changes: 14 additions & 4 deletions resources/in-content/site-wiki.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ Again, I recommend leaving this disabled and just using the Vector 2022 theme an
@-moz-document domain("wikipedia.org"), domain("wikimedia.org") {
:root {
--wiki-dummy-variable: initial;
--overlay-scrollbar-thumb-color: hsla(0, 0%, 45%, 0.75);
}

:root:not([data-darkreader-scheme="dark"]) {
--overlay-scrollbar-thumb-color: hsla(0, 0%, 52%, 0.6);
/* dynamically adjust overlay scrollbar color for dark mode. we need to do this because the value of
the scrollbar thumb color determines whether it will get lighter or darker on :hover or :active.
in dark mode, we want it to get lighter so that it will have more contrast with the background.
but in light mode, we want it to get darker, again for the same reason. the darker the base color is,
past a certain threshold, Firefox will decide to make it lighter on hover/active. so basically,
we need to generate a dark value when dark reader is enabled, and a light value when it's not.
this isn't easy because wikipedia has no color-scheme rules. our only dark mode option is dark reader.
but we can't select for dark reader on the root element. but what dark reader does add are variables.
so we can use a variable to derive the scrollbar color, and add a fallback value that's substantially
different for the case when dark reader is not installed. basically we mix our dynamic color with
black to get our final value. the first color is either 83% lightness when dark reader is enabled, or
100% when it's disabled or uninstalled. so our mixed color is darker in dark mode than in light mode.
that's how we get on the low end of the threshold in dark mode and still on the high end in light mode. */
--overlay-scrollbar-thumb-color: color-mix(in srgb, var(--darkreader-neutral-text, hsl(0, 0%, 100%)), black 52%);
}

/* Article preview popups. Make them scrollable, remove settings button, etc. */
Expand Down
3 changes: 2 additions & 1 deletion uc-misc.css
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ as the mouse would then be on top of the tooltip popup not the main window. */
box-shadow: none !important;
background: none !important;
border: none !important;
font-size: 0.9em;
margin-top: 1px !important;
/* font-size: 0.9em; */
}

:is(#back-button-tooltip, #forward-button-tooltip):not([shadow-support])
Expand Down
1 change: 1 addition & 0 deletions uc-tabs.css
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@

.tab-label-container {
height: revert !important;
pointer-events: none !important;
}

.tab-label[attention]:not([selected="true"]) {
Expand Down

0 comments on commit a414802

Please sign in to comment.