Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/features/accesskit/disable_gifs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { keyToCss } from '../../utils/css_map.js';
import { dom } from '../../utils/dom.js';
import { buildStyle, postSelector } from '../../utils/interface.js';
import { getPreferences } from '../../utils/preferences.js';
import { memoize } from '../../utils/memoize.js';

const canvasClass = 'xkit-paused-gif-placeholder';
const pausedPosterAttribute = 'data-paused-gif-use-poster';
Expand Down Expand Up @@ -92,7 +93,30 @@ const addLabel = (element, inside = false) => {
}
};

const pauseGif = function (gifElement) {
/**
* Fetches the selected image and tests if it is animated. On older browsers without ImageDecoder
* support, GIF images are assumed to be animated and WebP images are assumed to not be animated.
*/
const isAnimated = memoize(async sourceUrl => {
const response = await fetch(sourceUrl, { headers: { Accept: 'image/webp,*/*' } });
const contentType = response.headers.get('Content-Type');

if (typeof ImageDecoder === 'function' && await ImageDecoder.isTypeSupported(contentType)) {
const decoder = new ImageDecoder({
type: contentType,
data: response.body,
preferAnimation: true
});
await decoder.decode();
return decoder.tracks.selectedTrack.animated;
} else {
return !sourceUrl.endsWith('.webp');
}
});

const pauseGif = async function (gifElement) {
if (gifElement.currentSrc.endsWith('.webp') && !(await isAnimated(gifElement.currentSrc))) return;

const image = new Image();
image.src = gifElement.currentSrc;
image.onload = () => {
Expand Down Expand Up @@ -186,7 +210,7 @@ export const main = async function () {
'topPost', // activity page top post
'takeoverBanner' // advertisement
)}
) img[srcset*=".gif"]:not(${keyToCss('poster')})
) img:is([srcset*=".gif"], [src*=".gif"], [srcset*=".webp"], [src*=".webp"]):not(${keyToCss('poster')})
`;
pageModifications.register(gifImage, processGifs);

Expand Down