simonlindholm/toggle-gifs

Port to WebExtensions

Opened this issue ยท 46 comments

Mozilla will be dropping support for XUL extensions in Firefox 57, so this needs to be ported to the WebExtensions APIs before then if you want to keep it working. (And I'd really appreciate if that were the case)

If it can't be accomplished using WebExtensions APIs, you need to get onto Bugzilla A.S.A.P. to start a discussion on how to extend the APIs.

... yeah. :( I hope to have time to do this during the summer. It requires pretty much a complete rewrite and getting API's for GIF manipulation into Firefox.

According to a discussion I had in #firefox, the shut-off is tied to the version number, so you only have until August 7th if you want to avoid disruption for users on the Aurora/DeveloperEdition channel. (another 6 weeks sooner for nightlies if their deadlines don't slip)

That's why I said A.S.A.P. about getting on Bugzilla. You need to get them designing and writing that API pronto.

EDIT: And please mention the bug here when you do open it, so I can follow the discussion.

Thanks for the information about timing. I wonder when in the Nightly cycle it will land... Still, seems like I should have time in June and July if not before.

My (naive?) hope is that the API is small enough that I can just design and land it myself, without a very long discussion phase. (And FWIW if for whatever reason I don't manage to get APIs accepted into Firefox, I'll make them available as a webextension experiment so I can continue using the extension myself.)

Assuming no slipped deadlines, it'll land in Nightly on June 12th.

(I've been doing some serious research into this whole "XUL is going away" thing.)

Thanks. I'm now following both of those.

suy commented

Hi. I see that https://bugzilla.mozilla.org/show_bug.cgi?id=1364972 is resolved, and some new API is added for FF 57. This doesn't seem as complete as fully controlling everything, but, can a pause/play button be implemented with that?

Kind of - I suspect changing the setting only affects future page loads though. I'm currently experimenting with whether it's possible to exploit Firefox bugs/quirks to get the behavior I want. (In particular, image animation state is shared between images with the same source even across frames, and window.stop() stops image animation along with its other effects.)

I rather missed the deadline here, but, anyway. Here's a possible plan for a port:

  • Hook all web requests of type image and maybe main_frame (for image documents). (Will need some benchmarking to see what the perf implications of this are.)
  • Check for each response that appears whether it has content-type: image/gif, and if so whether it is an animated GIF (APNG's will have to come at a later state; GIFV's I already have heuristic detection code for). Record each anim-gif-URL in a set, with LRU cache eviction or something.
  • For each hovered image, and on load/loadstart events, check whether the URL is in the set. If so, mark it as animated and show control buttons and stuff.
  • Using the API in https://bugzilla.mozilla.org/show_bug.cgi?id=1364972 we know the animation state of each frame -- namely, the global state at the time the frame loaded. Use this to figure out whether each individual image is animated. (This breaks down in obscure cases, but hey.)
  • To pause an image, set the global animation state to "not animating", add a hidden iframe to the page, then set it back to "animating". This way the added frame has animations disabled. (While, assuming no race conditions, no other frames do.) Then do iframe.contentDocument.body.appendChild(image.cloneNode()). The added image clone will a) be served from the image cache, not incurring any web requests, b) be paused, because it's in a frame with animations disabled, and, somewhat subtly, c) also cause the original image to pause, since animations are keyed by URL. Then remove the image clone after it has had time to paint.
  • To unpause an image, same thing.
  • To reset an image's animation, set image.src = image.src. The HTML spec guarantees this to work without networky side-effects.
  • For Ctrl+M, iterate over all images and pause/unpause them, and then maybe do something with load events to affect later loaded images...
  • (Bonus!) Maybe add an option for stopping GIF network loads after the first frame, now that I'm hooking network requests and have a GIF parser lying around. Might break Connection: keep-alive and actually be bad for performance if I do it naively, though, and it will need some GIF parser surgery.

Hopefully I'll have time for all this at some point... In the mean time, the addon continues to work on Nightly, about:config's image.animation_mode = none still works, and with image.animation_mode = normal https://addons.mozilla.org/en-US/firefox/addon/superstop/ might be an alternative.

Hope you find a way. :) I miss this plugin so hard.

Looking forward to it!
Thanks in advance.

Sorry for filling your comments... Just wanted to let you know that more people are awaiting WebExt version.
Good luck.

Thanks for the encouragement, all. I have some work-in-progress on the web-ext branch; current status is that it's buggy. Hopefully it's fixable, if not I guess I could do something more like https://github.com/buzzfeed/libgif-js or https://github.com/adamfur/GifJam. Still haven't tested performance.

I noticed a bug with Firefox 57, it doesn't want to play animated gifs and I tried removing Toggle Animated Gifs (even though its still a legacy add-on) and it didn't help. My guess is that something got corrupted in Firefox when updating to version 57. Also good luck with updating TAG to a Web Extension.

I suspect that's because the addon sets the image.animation_mode pref in about:config to none, and doesn't revert it on uninstall because Firefox 57 doesn't run uninstall code for legacy addons. You should be able to fix it by going to about:config and setting image.animation_mode to normal. Sorry for the inconvenience.

@simonlindholm Thanks, But I already got some help last night from the Mozilla forums about this.:)

@simonlindholm hi, what is the status of the port? Do you need help?

Thanks for asking. Life caught up with me, and I haven't had any time to look at this for the last two months... If you're interested in helping out, there's some work in progress in the 'web-ext' branch, and I could try giving some detail on how the code works. Status is that most of the big comment above is implemented, and sometimes it even works. However, it's buggy in a few ways:

  • it doesn't follow redirects correctly (not hard to fix, just needs a bunch of code)
  • it's somewhat broken with <imageset>s, which wikipedia uses
  • generally, sometimes images just decide to revert to their original animation mode for no discernible reason. This might be because I was testing with <imageset>s, but either way it's worrying
  • there are some issues with resetting images after they've changed animation mode
  • hovering an image adds a funky-looking iframe to the page to aid with debugging, which would need to be marked invisible before the addon is suitable for daily use

So the way I'd continue this if I had time (likely won't for the next 1-2 months either) would be to first fix the redirect thing (to avoid false positives when testing), and do further testing of how stable the method is. Depending on that, either tweak things like resetting to work reliably and then ship an update, or re-think the approach and either implement my own custom GIF rendering or look into standization.

Thanks for the response. I'll check it out after my vacation. :)

Hey guys! Any news regarding the port? I don't have much time to help you code, but I could help you test if you need some testers. Thanks for your work

Not much has happened since the last update. I may start to get time for this addon soon, though.

Hey, any chances project will move on any time soon?
You have a bunch of fans, and your plugin has no replacement for Firefox 53 onwards.

On a related note, is anyone aware of an alternative with feature-parity? I don't want to disable GIF and HTML video autoplay entirely, only be default and play the files at will.

Even only implementing GIF-support would be very helpful, I occasionally come across older forums where every post seems to have animated GIFs (avatars, signatures, the message content). As far as I've understood implementing support for HTML video is delaying development.

@simonlindholm, godspeed from still snow-covered Moscow.

Curious on what is current status on port. :)

Hej. Life got in the way. I had an idea of making a small gif only extension which only stops/starts a gif-image. But didn't found the time.

@simonlindholm
Have you some required API missing, to continue developing this lovely addon?
Do you defined missing APIs in bugzilla.mozilla.org?
If you have, i will be glad to support it by adding my vote in them.

Really missing this add-on. Willing to donate if it helps.

any news ? I love the addon and is a big miss on quantum

I wish I knew enough to make one. I recently made an addon, cool but its very simple compared to this one.

Can the suggestions in the following work?
https://stackoverflow.com/questions/3688460/stopping-gif-animation-programmatically/4276742
https://stackoverflow.com/questions/3062229/animated-gif-in-html5-canvas/6017468#6017468
https://github.com/ctrl-freaks/freezeframe.js

For a simple addon this links are good. But this addon especially aims to stop the gif in place. So hit stop/pause and play again where you stopped the gif.

At least the addon won't have problems with cross-domain policy, which actually saves a ton of headache. There are libs which can parse GIFs in JS (eg https://github.com/buzzfeed/libgif-js or https://github.com/benwiley4000/gif-frames), all we need is to actually replace all the gifs into canvases, get the gif frames, get the gif animation speed and then just playback manually. But that solution isn't very performant.
There is another way - to convert the gif into a video, and instead rendering it to canvas, just render it to a video tag - and we get play/pause out of the box. There is even libs which can do such a conversion (aka https://github.com/Kagami/webm.js), which I guess could be tweaked for the lossless encoding (as we don't really care about the resulting file size, and lossless is faster), but that would need some kind of caching and/or background and/or lazy loading. That sure gives better results, but that also kind of changes semantics - aka a video tag is not an img tag, so it can break some things as well - also won't probably work with background-image CSS tag gifs (although animated gifs in background - is that even a thing?)

There are ways, but they sure are messy than this one which basically could "hack" into the Firefox gif playing engine and set the state directly for the gifs themselves. Too bad Mozilla dropped XUL support =(

This is the user script I've made and been using for some time: Animate GIFs.user.js.

This is the user script I've made and been using for some time: Animate GIFs.user.js.

Sorry I cant code (LOL) where should i put this code in to use on firefox?

Install TamperMonkey and be happy

This is the user script I've made and been using for some time: Animate GIFs.user.js

Doesn't work for me. I see an icon on the GIF but clicking it does nothing

lonix1 commented

I doubt that script works, it hadn't been updated in years. Doesn't work for me anyway.

The real solution is a webextension, like this one. But seems like it's abandoned.

Doesn't work for me. I see an icon on the GIF but clicking it does nothing

It actually does, i think it starts the gif back from the begining when you click on it. Still useless

I did seem to degrade the quality of the GIF. Oh well.

And userscripts can do most of what webextensions do.

there's a webapp in chrome called gif scrubber. I think it worked for some time (but is dead now)
i think this should be an integrated function in firefox tbh. i dont get why software companies keep trying to push things nobody ever wanted, while fail to provide basic common sense services (specially microsoft and google). I got pretty mad for firefox ending XUL but i'm back after 5 years

yeah I hate it when control is taken away. I hate how youtube just autoplays and same for gifs. This was awesome till it stopped working.

yeah I hate it when control is taken away. I hate how youtube just autoplays and same for gifs. This was awesome till it stopped working.

As long as I middle-click instead of left-clicking when changing videos, and toggle auto-advance off in the YouTube player widget (it does a pretty good job of staying off until I restart the browser), I'm pretty comfortable with the level of non-autoplay I get out of YouTube without breaking particularly indirect play-button-to-video-element connections on other sites.

Here are the relevant about:config keys I currently have set to non-default values:

media.autoplay.ask-permission	true
media.autoplay.default	1	
media.autoplay.enabled	false	
media.autoplay.enabled.user-gestures-needed	true

The more annoying part was that I had to write a CSS Userstyle to force the issue on YouTube's insistence on constantly turning CC on. (.captions-text { display: none !important; })

I will always want captions on myself :D but otherwise thanks.

I've pushed the current version of my script here.