Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix redgif host video and images #5481

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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