Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent or automatically close leftover redirector tabs for any site. #552

Open
BillyJoeJimBob opened this issue Jan 10, 2022 · 3 comments

Comments

@BillyJoeJimBob
Copy link

BillyJoeJimBob commented Jan 10, 2022

I'm trying to find the best means of handling sites that natively always open links in new tabs, for example any of the article links in https://news.google.com or https://www.fark.com.

If I use isolation by navigation set to "Different from tab domain & subdomains", clicking an article link in a tab in container 1 opens a new redirector tab within container 1, from this redirector tab Temporary Containers then opens a new third tab which has the actual target domain page in new container 2. This results in a leftover redirector tab for every clicked external link.

I can avoid the redirector tab in these specific sites by using isolation by left mouse click set to "Always" instead of navigation as described above. In this case, clicking an article link in a tab in container 1 opens a new redirector tab within new container 2 which then redirects to the target domain within that redirector tab, no third tab is created. However, left mouse click "Always" means that every single clicked internal link, page or comment within sites like news.google.com causes a new container tab to open, which is also results in tab clutter. This is how I have it configured:

"domain": [
{
"pattern": "www.fark.com",
"always": {
"action": "disabled",
"allowedInPermanent": false,
"allowedInTemporary": false
},
"navigation": {
"action": "never"
},
"mouseClick": {
"middle": {
"action": "global"
},
"ctrlleft": {
"action": "global"
},
"left": {
"action": "always"
}
},
"excluded": {}
},

My question is it possible to effectively narrow the domain pattern to include a path such as: news.google.com/articles/* or www.fark.com/goto/* to avoid causing every click on the site opening a new container tab? I've tried entering them in RegEx format but I have not been able to make this work, likely because I'm not doing it correctly.

Alternatively, is there another way to avoid or automatically remove the intermediate tabs? In Temporary Containers preferences: ADVANCED -> General -> Isolation, there is an option: Instead of creating a new tab replace the current tab in case of Isolation, which can only be applied globally, not individual sites.

There is another setting In Temporary Containers preferences: ADVANCED -> General -> Isolation: Automatically close leftover redirector tabs after 2 seconds, but this appears to be limited to Twitter, AMO and Slack.

Thank you.

@BillyJoeJimBob BillyJoeJimBob changed the title How to handle intermediate redirect tabs How to prevent or automatically close leftover redirector tabs for any site. Jan 14, 2022
@KerfuffleV2
Copy link

KerfuffleV2 commented Mar 4, 2022

@BillyJoeJimBob @EdJoPaTo #503

It's actually possible to do this using the scripting function Temporary Containers, although it's definitely a pain and there's a tradeoff as well.

You can add scripts in the TC settings under AdvancedScripts. Here's an example for cleaning up the DuckDuckGo and fark redirectors:

Domain Pattern

Set to /^https?:.*/ — This is a regular expression that matches all http:// and https:// URLs.

Run At

Doesn't really matter. Adjusting the setting to document_begin may clean up tabs faster, but it may also nuke them before they get a chance to redirect.

Code

const REDIRS = [ 'https://duckduckgo.com/l/', 'https://www.fark.com/goto/', ];
const ORIGURL = document.URL;
if (REDIRS.find(r => ORIGURL.startsWith(r))) {
setTimeout(
  () => {if (document.URL === ORIGURL) window.close();}, 500);
}

The REDIRS constant here is just a list of strings that are prefixes for the URLs of redirectors you want to clean up.

When the script hits an URL that matches the list of redirectors, it saves the current URL and waits 500ms (half second) and checks to see if it's still the redirector and if so, it closes the window or tab. This is basically how TC's own redirector cleanup function works (although it waits about 2 seconds, I think.)

If you're comfortable making regexps then the matching logic could be removed from the script and controlled by the Domain Pattern setting instead.


This works well for cleaning up redirectors that opened a new tab, but unfortunately scripts are not normally allowed to close windows that they did not create. That means if you navigate to a redirector link within the same tab the script won't have permission to close it. There is a workaround, though: In about:config there is a setting dom.allow_scripts_to_close_windows which can be toggled to true. When this is enabled, the TC script above will be able to clean up both types of left of redirectors.

This also means any script on any webpage can close its own window, which can be abused. This is something to be aware of.

Disclaimer: The TC scripts page warns not to add scripts from untrusted sources. That's me!

Also, it may be possible to end up in a situation where all tabs get immediately closed if the domain pattern and script matching are too broad. I'd suggest trying this out in a new, empty browser profile. TC also won't show any indication if there are errors in the script, so if nothing happens then there's probably a typo somewhere.

Hope this is helpful!

@BillyJoeJimBob
Copy link
Author

BillyJoeJimBob commented Mar 5, 2022

@KerfuffleV2

Thank you! Your script solved the issue for the me. In the case of news.google.com/articles/, 250ms sometimes was not enough time for the site to redirect, increasing the time solved that.

@stoically
Copy link
Owner

Would definitely be useful to have the ability to either allow custom entries for the leftover redirector tabs for the advanced preference, or have it be part of #397, where a rule could also have a "redirector toggle".

@KerfuffleV2 Creative use of TC scripts, nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants