jsmnbom / back2close

Firefox web extension. Use the back button to close newly opened tabs.
https://addons.mozilla.org/en-US/firefox/addon/back-to-close-we/
MIT License
16 stars 4 forks source link

Firefox Portable #13

Open dima6526 opened 2 years ago

dima6526 commented 2 years ago

Stopped working on FF Portable since some time ago, changing settings, updating FF didn't help.

Hvalfisk commented 2 years ago

It has stopped working for me. I am using Firefox 99.0b6

Hvalfisk commented 2 years ago

It works again after disabling Tampermonkey

dima6526 commented 2 years ago

Indeed, thanks for pointing it out. Hope the conflict gets resolved in future versions.

krompus commented 2 years ago

@Hvalfisk Thanks for updating this thread with the fix; I wasn't using Tampermonkey much anyways, and now Back to Close works again. :)

ElhemEnohpi commented 1 year ago

This isn't unique to Firefox Portable, it also happens on the Mac version, etc. It's been reported in the Tampermonkey issues here: https://github.com/Tampermonkey/tampermonkey/issues/1566

Not working on Firefox 106 Mac, or Firefox Developer Edition 107.0b5, with TamperMonkey 4.18.0, or Beta 4.19.6171 installed, when doing "open link in a new tab". The "back" button in the new tab doesn't show any history. It still works if you open a new tab and enter a URL. Tampermonkey needs only to be installed, without any scripts active. Unchecking "Enabled" from the Tampermonkey menu doesn't help - you have to disable it completely with the addons manager. The following lines appear in the browser console when you do "open link in new tab" with Tampermonkey installed, which are not there when it isn't:

Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”). moz-extension:58:146
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”). moz-extension:58:361
CennoxX commented 7 months ago

Tampermonkey injects a history entry on its own. So this incompatibility with Tampermonkey could be fixed, by adding one to the required length where not a new tab

code: source.replace('__PUSH_STATE__', tabs[tab.id].push).replace('__REQUIRED_LENGTH__', tabs[tab.id].newtab ? 2 : 1),

making it

code: source.replace('__PUSH_STATE__', tabs[tab.id].push).replace('__REQUIRED_LENGTH__', tabs[tab.id].newtab ? 2 : 2),

on line 58 of background.js, but only if Tampermonkey version 5+ is used.

To check the installed Tampermonkey version the following code could be used, with the permission management in the manifest:

browser.management.getAll().then((addons) => {
    var tampermonkey = addons.find(i => i.id == "firefox@tampermonkey.net" && i.name == "Tampermonkey");
    console.log("uses new tampermonkey: " + (tampermonkey != null && tampermonkey.enabled && tampermonkey.version.split(".")[0] > 4)); 
})

I don't know whether it makes sense to include a special check for this. An additional option would also be sufficient.

ElhemEnohpi commented 7 months ago

@CennoxX - thanks for this! I tested it by changing the one line ending from 1 to 2 as you said, and loading the extension in the latest Firefox Developer Edition on macOS, along with Tampermonkey. It does restore the "back to close" function for tabs opened from the context menu "open link in new tab", which had been broken as I described above. Unfortunately, it's inconsistent in some cases. For example, doing "open link in new tab" with this png link does work without the modification, but with it, sometimes it functions with the back button closing it, but sometimes doesn't have an active "back" button.

Some links that automatically open in a new tab with target="_blank" are not broken by Tampermonkey without the modification, and the back to close function still works. For example, single-clicking on links in posts or comments on Reddit. With this modification, they now are broken. From a bit of testing with the W3Schools Tryit Editor, it seems to be only broken if the link is to an external site. There's also some further inconsistency, for example, links from Reddit to YouTube become broken by Tampermonkey, and remain broken with this modification.

My Javascript skills aren't good enough to fix it myself. It looks like the developer, @jsmnbom hasn't been around for a long time, and I guess may not respond. Hopefully someone else can take it on!

CennoxX commented 6 months ago

@ElhemEnohpi Could you test this new fix, that should work with and without Tampermonkey? Instead of https://github.com/jsmnbom/back2close/blob/aedc7074a14881502c7378852d68f2103f7ead65/src/script.js#L34 check all times for both history.length = 1 and 2

    if (__PUSH_STATE__ && (history.length === 1 || history.length === 2)) {

This seems to work for me...

Edit: One negative side effect seems to be, that an extra "Close Tab" history entry gets added to the history on restored tabs (compare An extra Close Tab entry gets added to the history on restored tabs #2). For the example in #2 this time it would be in the order: Close Tab -> Close Tab -> Site 1 -> Site 2 -> Site 3 ... so that shouldn't be more than an aesthetic imperfection, if you take a look at your history by right clicking on the navigation arrow.

ElhemEnohpi commented 6 months ago

@CennoxX Unfortunately that doesn't seem to work for me at all. A tab opened with "Open Link in New Tab" has no active back button or history entry.

I'm not sure if you meant this fix to be in addition to the other one in background.js, or as a replacement. I tried it both ways, but neither works.