Closed KwanEsq closed 5 years ago
webp support is configurable via the mage.webp.enabled
preference at about:config
, so I would rather see feature detection than browser sniffing.
You could add a check for webp here: https://github.com/Rob--W/open-in-browser/blob/26df4dbaa2fb82a2d24ddc0d7ca30300adee2538/extension/content-handlers-firefox.js#L108
and then perform feature detection, for example:
var canvas = document.createElement('canvas');
canvas.width = canvas.height = 1;
canvas.toDataURL('image/webp').startsWith('data:image/webp'); // true if supported, false otherwise
In theory the pref can repeatedly be toggled at runtime; in practice that is probably so unlikely that we can probably cache the result of feature detection in a global variable (initially undefined
, change to boolean after performing feature detection).
Out of curiosity, what result does that snippet give you in 65+? Because I did actually try that earlier (though without the dimension setting) when I was trying to figure out if it was sniff-able, but I seem to get PNG even in WebP supporting Firefoxen.
Oh it doesn't work because Firefox doesn't support webp for toDataURL yet - https://bugzilla.mozilla.org/show_bug.cgi?id=1505383
You will have to load a webp image, and use img.onerror
/ img.onload
+ check for img.naturalWidth
to check whether webp images can be loaded. This check is asynchronous, and you certainly have to cache the result to avoid performing the check over and over again.
Okay new version with checking via an <img>
, cribbed heavily from the PDF check code (which I need to file an issue against...)
Checking img.naturalWidth doesn't seem to gain anything? I get the expected results while toggling the pref as is, what would that add?
Test image is from https://hg.mozilla.org/mozilla-central/file/476293c6700f/image/test/gtest/green.webp.
Thanks for the patch and providing the source of the test image!
@Rob--W can you push a new version with this fix in please?
@Rob--W can you push a new version with this fix in please?
This please, it's an annoying bug.
Hmm usually @Rob--W is quick to respond to requests like this... I'll upload a new version soon.
2.9 is released on AMO. Seee also https://github.com/Rob--W/open-in-browser/pull/59
Oops. I wanted to look into the PDF detection, but didn't get to that and forgot to publish an update with this fix. Thanks @est31 for publishing the update.
Support shipped in Firefox 65. Fixes #51.
Not sure if this is the best way to do it, but certainly fixes the problem for me.