Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions JS/privateTabs.uc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name Private Tabs
// @version 1.4.1
// @version 1.4.2
// @author aminomancer
// @homepage https://github.com/aminomancer
// @description An fx-autoconfig port of [Private Tab](https://github.com/xiaoxiaoflood/firefox-scripts/blob/master/chrome/privateTab.uc.js) by xiaoxiaoflood. Adds buttons and menu items allowing you to open a "private tab" in nearly any circumstance in which you'd be able to open a normal tab. Instead of opening a link in a private window, you can open it in a private tab instead. This will use a special container and prevent history storage, depending on user configuration. You can also toggle tabs back and forth between private and normal mode. This script adds two hotkeys: Ctrl+Alt+P to open a new private tab, and Ctrl+Alt+T to toggle private mode for the active tab. These hotkeys can be configured along with several other options at the top of the script file.
Expand Down Expand Up @@ -61,9 +61,8 @@ class PrivateTabManager {
// to addTrustedTab. so we need to make our own function, which requires us
// to access some private objects.
// eslint-disable-next-line mozilla/use-chromeutils-import
let { SessionStoreInternal, TAB_CUSTOM_VALUES } = Cu.import(
"resource:///modules/sessionstore/SessionStore.jsm"
);
let { SessionStoreInternal, TAB_CUSTOM_VALUES } =
ChromeUtils.importESModule("resource:///modules/sessionstore/SessionStore.sys.mjs");
Comment on lines +64 to +65
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work. Neither of these properties are exported, they're hidden in the code and not available to outside callers, so they can't be imported by importESModule. Like I said in the other thread, you'd need to change the duplicateTab code so it uses SessionStore.duplicateTab, like xiaoxiaoflood's version. Then your code doesn't rely on SessionStoreInternal or TAB_CUSTOM_VALUES. But that has trade-offs.

this.SSI = SessionStoreInternal;
this.TAB_CUSTOM_VALUES = TAB_CUSTOM_VALUES;
ChromeUtils.defineESModuleGetters(this, {
Expand Down Expand Up @@ -411,15 +410,12 @@ class PrivateTabManager {
? aWindow
: lazy.BrowserWindowTracker.getTopWindow();
}
let openTabsetString = PlacesUIUtils.openTabset.toString();
eval(
`PlacesUIUtils.openTabset = ${
openTabsetString.startsWith("function") ? "" : "function "
}${openTabsetString.replace(
/(\s+)(inBackground: loadInBackground,)/,
"$1$2$1userContextId: aEvent.userContextId || 0,"
)}`
);
let originalOpenTabset = PlacesUtils.openTabset;
lazy.PlacesUtils.openTabset = function(aURIs, aOptions) {
aOptions = aOptions || {};
aOptions.userContextId = aOptions.userContextId || 0;
originalOpenTabset.call(this, aURIs, aOptions);
};
Comment on lines +414 to +418
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's PlacesUIUtils, not PlacesUtils. there is no PlacesUtils.openTabset, it doesn't exist. so this is doing nothing. therefore, opening multiple private tabs is not working.

also, eval is better. it means if minor changes are made to the function, they get forwarded to your modified version.

as far as I can tell, there's no reason the original version on the left wouldn't work. unless you have eval blocked because you don't have security.allow_unsafe_dangerous_privileged_evil_eval set to true.

}

const { WebExtensionPolicy } = Cu.getGlobalForObject(Services);
Expand Down Expand Up @@ -527,16 +523,25 @@ class PrivateTabManager {
return newTab;
}

togglePrivate(tab = gBrowser.selectedTab) {
tab.isToggling = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was the only way I found to get the script working again😅
I think that the removed part of the code is what makes it possible to change a normal tab to a private one, and that feature is still functional with the changes made despite that part of the code being removed.
I don't understand why it would be wrong to remove that part of the code if the script is functional. I think that's better than having a broken script.🤷‍♂️

let shouldSelect = tab == gBrowser.selectedTab;
this.duplicateTab(tab, {
index: shouldSelect ? tab._tPos + 1 : tab._tPos,
inBackground: !shouldSelect,
togglePrivate(tab = gBrowser.selectedTab) {
let isCurrentlyPrivate = this.isPrivate(tab);
let targetUserContextId = isCurrentlyPrivate ? 0 : this.container.userContextId;

let urlToOpen = tab.linkedBrowser.currentURI.spec;

let newTab = openTrustedLinkIn(urlToOpen, "tab", {
userContextId: targetUserContextId,
index: tab._tPos + 1,
inBackground: tab != gBrowser.selectedTab,
Comment on lines +532 to +535
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't restore the tab state. You'll lose tab history. You should be using SessionStore.duplicateTab, like in xiaoxiaoflood's version.

});
if (shouldSelect && gURLBar.focused) gURLBar.focus();

if (tab == gBrowser.selectedTab) {
gBrowser.selectedTab = newTab;
if (gURLBar.focused) gURLBar.focus();
}
Comment on lines +538 to +541
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should save this before you open/duplicate the tab. Like at line 531. wasSelected = tab === gBrowser.selectedTab. Then check wasSelected here. Because tab selection changes after opening the new tab.


gBrowser.removeTab(tab, { animate: false, closeWindowWithLastTab: false });
}
}

toggleMask() {
let privateMask = document.querySelector(
Expand Down