From 03f10cdec56562f9e8f30952b1fd30fc34b806a8 Mon Sep 17 00:00:00 2001 From: yereter Date: Thu, 19 Oct 2023 13:48:23 +0300 Subject: [PATCH 1/2] fix redgif host video and images --- chrome/beta/manifest.json | 3 +- chrome/manifest.json | 3 +- firefox/beta/manifest.json | 3 +- firefox/manifest.json | 3 +- lib/modules/hosts/redgifs.js | 86 +++++++++++++++++++++++------------- 5 files changed, 64 insertions(+), 34 deletions(-) diff --git a/chrome/beta/manifest.json b/chrome/beta/manifest.json index e430d45cb6..6708c97f70 100644 --- a/chrome/beta/manifest.json +++ b/chrome/beta/manifest.json @@ -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://*.redgifs.com/*" ], "web_accessible_resources": [ "{{../../lib/environment/background/permissions/prompt.html}}", diff --git a/chrome/manifest.json b/chrome/manifest.json index f8af9c42cd..76442ade27 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -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://*.redgifs.com/*" ], "web_accessible_resources": [ "{{../lib/environment/background/permissions/prompt.html}}", diff --git a/firefox/beta/manifest.json b/firefox/beta/manifest.json index de9b58fd62..2930d5af05 100644 --- a/firefox/beta/manifest.json +++ b/firefox/beta/manifest.json @@ -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://*.redgifs.com/*" ], "web_accessible_resources": [ "{{../../lib/environment/background/permissions/prompt.html}}", diff --git a/firefox/manifest.json b/firefox/manifest.json index a5d9d24147..7b0e5b280f 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -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://*.redgifs.com/*" ], "web_accessible_resources": [ "{{../lib/environment/background/permissions/prompt.html}}", diff --git a/lib/modules/hosts/redgifs.js b/lib/modules/hosts/redgifs.js index abd22d4049..3f6f2ad666 100644 --- a/lib/modules/hosts/redgifs.js +++ b/lib/modules/hosts/redgifs.js @@ -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://*.redgifs.com/*'], 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`, From a490799aadbf0206d1296cddbaa4376b54102c91 Mon Sep 17 00:00:00 2001 From: yereter Date: Thu, 19 Oct 2023 14:21:42 +0300 Subject: [PATCH 2/2] make redgif permission more strict --- chrome/beta/manifest.json | 2 +- chrome/manifest.json | 2 +- firefox/beta/manifest.json | 2 +- firefox/manifest.json | 2 +- lib/modules/hosts/redgifs.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/chrome/beta/manifest.json b/chrome/beta/manifest.json index 6708c97f70..9d8a4d8b51 100644 --- a/chrome/beta/manifest.json +++ b/chrome/beta/manifest.json @@ -87,7 +87,7 @@ "https://accounts.google.com/signin/oauth", "https://www.dropbox.com/oauth2/authorize", "https://login.live.com/oauth20_authorize.srf", - "https://*.redgifs.com/*" + "https://api.redgifs.com/v2/*" ], "web_accessible_resources": [ "{{../../lib/environment/background/permissions/prompt.html}}", diff --git a/chrome/manifest.json b/chrome/manifest.json index 76442ade27..79c3bd9449 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -87,7 +87,7 @@ "https://accounts.google.com/signin/oauth", "https://www.dropbox.com/oauth2/authorize", "https://login.live.com/oauth20_authorize.srf", - "https://*.redgifs.com/*" + "https://api.redgifs.com/v2/*" ], "web_accessible_resources": [ "{{../lib/environment/background/permissions/prompt.html}}", diff --git a/firefox/beta/manifest.json b/firefox/beta/manifest.json index 2930d5af05..b6f10ab446 100644 --- a/firefox/beta/manifest.json +++ b/firefox/beta/manifest.json @@ -88,7 +88,7 @@ "https://www.googleapis.com/drive/v3/*", "https://*.redd.it/*", "https://www.flickr.com/services/oembed", - "https://*.redgifs.com/*" + "https://api.redgifs.com/v2/*" ], "web_accessible_resources": [ "{{../../lib/environment/background/permissions/prompt.html}}", diff --git a/firefox/manifest.json b/firefox/manifest.json index 7b0e5b280f..3d4a4cef41 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -88,7 +88,7 @@ "https://www.googleapis.com/drive/v3/*", "https://*.redd.it/*", "https://www.flickr.com/services/oembed", - "https://*.redgifs.com/*" + "https://api.redgifs.com/v2/*" ], "web_accessible_resources": [ "{{../lib/environment/background/permissions/prompt.html}}", diff --git a/lib/modules/hosts/redgifs.js b/lib/modules/hosts/redgifs.js index 3f6f2ad666..514078f449 100644 --- a/lib/modules/hosts/redgifs.js +++ b/lib/modules/hosts/redgifs.js @@ -7,7 +7,7 @@ import { ajax } from '../../environment'; export default new Host('redgifs', { name: 'redgifs', domains: ['redgifs.com'], - permissions: ['https://*.redgifs.com/*'], + permissions: ['https://api.redgifs.com/v2/*'], logo: 'https://redgifs.com/assets/favicon.ico', detect: ({ pathname }) => (/^\/(?:(?:ifr|watch|i)\/)(\w+)/i).exec(pathname), async handleLink(href, [, id]) {