Rob--W / open-in-browser

A browser extension that offers the ability to open files directly in the browser instead of downloading them.
Other
88 stars 16 forks source link

Conditionally add image/webp as displayable inline. #52

Closed KwanEsq closed 5 years ago

KwanEsq commented 5 years ago

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.

Rob--W commented 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).

KwanEsq commented 5 years ago

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.

Rob--W commented 5 years ago

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.

KwanEsq commented 5 years ago

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.

Rob--W commented 5 years ago

Thanks for the patch and providing the source of the test image!

OrangeDog commented 5 years ago

@Rob--W can you push a new version with this fix in please?

lol768 commented 5 years ago

@Rob--W can you push a new version with this fix in please?

This please, it's an annoying bug.

est31 commented 5 years ago

Hmm usually @Rob--W is quick to respond to requests like this... I'll upload a new version soon.

est31 commented 5 years ago

2.9 is released on AMO. Seee also https://github.com/Rob--W/open-in-browser/pull/59

Rob--W commented 5 years ago

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.