Skip to content

Commit

Permalink
Pull Request honestbleeps#5481: fix redgif host video and images
Browse files Browse the repository at this point in the history
  • Loading branch information
uwx committed Oct 19, 2023
2 parents 6a952ad + a490799 commit 20fdec5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 34 deletions.
3 changes: 2 additions & 1 deletion chrome/beta/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
"https://redditenhancementsuite.com/oauth",
"https://accounts.google.com/signin/oauth",
"https://www.dropbox.com/oauth2/authorize",
"https://login.live.com/oauth20_authorize.srf"
"https://login.live.com/oauth20_authorize.srf",
"https://api.redgifs.com/v2/*"
],
"web_accessible_resources": [
"{{../../lib/environment/background/permissions/prompt.html}}",
Expand Down
3 changes: 2 additions & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
"https://redditenhancementsuite.com/oauth",
"https://accounts.google.com/signin/oauth",
"https://www.dropbox.com/oauth2/authorize",
"https://login.live.com/oauth20_authorize.srf"
"https://login.live.com/oauth20_authorize.srf",
"https://api.redgifs.com/v2/*"
],
"web_accessible_resources": [
"{{../lib/environment/background/permissions/prompt.html}}",
Expand Down
3 changes: 2 additions & 1 deletion firefox/beta/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
"https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/*",
"https://www.googleapis.com/drive/v3/*",
"https://*.redd.it/*",
"https://www.flickr.com/services/oembed"
"https://www.flickr.com/services/oembed",
"https://api.redgifs.com/v2/*"
],
"web_accessible_resources": [
"{{../../lib/environment/background/permissions/prompt.html}}",
Expand Down
3 changes: 2 additions & 1 deletion firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
"https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/*",
"https://www.googleapis.com/drive/v3/*",
"https://*.redd.it/*",
"https://www.flickr.com/services/oembed"
"https://www.flickr.com/services/oembed",
"https://api.redgifs.com/v2/*"
],
"web_accessible_resources": [
"{{../lib/environment/background/permissions/prompt.html}}",
Expand Down
86 changes: 56 additions & 30 deletions lib/modules/hosts/redgifs.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,74 @@
/* @flow */

import { Host } from '../../core/host';
import { DAY, string } from '../../utils';
import { DAY, MINUTE, string } from '../../utils';
import { ajax } from '../../environment';

export default new Host('redgifs', {
name: 'redgifs',
domains: ['redgifs.com'],
permissions: ['https://api.redgifs.com/v2/*'],
logo: 'https://redgifs.com/assets/favicon.ico',
detect: ({ pathname }) => (/^\/(?:(?:ifr|watch)\/)(\w+)/i).exec(pathname),
detect: ({ pathname }) => (/^\/(?:(?:ifr|watch|i)\/)(\w+)/i).exec(pathname),
async handleLink(href, [, id]) {
const embed = `https://redgifs.com/ifr/${id}`;
async function _getInfo(id, deleteCache) {
const authUrl = string.encode`https://api.redgifs.com/v2/auth/temporary`;
if (deleteCache === true) {
await ajax.invalidate(authUrl);
}
const token = (await ajax({
url: authUrl,
type: 'json',
cacheFor: MINUTE * 15,
})).token;

// Load video width/height to show a responsive embed
try {
const info = (await ajax({
url: string.encode`https://api.redgifs.com/v1/gfycats/${id}`,
return ajax({
url: string.encode`https://api.redgifs.com/v2/gifs/${id}`,
type: 'json',
cacheFor: DAY,
})).gfyItem;

let height = info.height;
let width = info.width;
const ratio = width / height;
const maxSize = 600;

if (height > width) {
height = Math.min(height, maxSize);
width = parseInt(ratio * height, 10);
} else {
width = Math.min(width, maxSize);
height = parseInt(width / ratio, 10);
headers: {
Authorization: `Bearer ${token}`,
},
});
}
try {
let info;
try {
info = await _getInfo(id);
} catch (e) {
info = await _getInfo(id, true);
}

return {
type: 'IFRAME',
embed: `${embed}?autoplay=0`,
embedAutoplay: embed,
fixedRatio: false,
width: `${width}px`,
height: `${height}px`,
muted: true,
};
const gif = info.gif;
if (gif.type === 2) {
return {
type: 'IMAGE',
src: gif.urls.hd,
href,
};
} else if (gif.type === 1) {
return {
type: 'VIDEO',
muted: !gif.hasAudio,
credits: gif.userName,
href,
poster: gif.urls.poster,
loop: true,
time: gif.duration,
sources: [
{
source: gif.urls.hd,
type: 'video/mp4',
},
{
source: gif.urls.sd,
type: 'video/mp4',
},
],
};
}
throw new Error(`Could not handle content type(${gif.type}), href: ${href}`);
} catch (error) { // Fallback to a fixedRatio embed
const embed = `https://redgifs.com/ifr/${id}`;
return {
type: 'IFRAME',
embed: `${embed}?autoplay=0`,
Expand Down

0 comments on commit 20fdec5

Please sign in to comment.