Skip to content

Add other GIF providers #60

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/components/giphy-toolbar-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export default (
>
<div class="select-menu-header d-flex">
<span class="select-menu-title flex-auto">Select a GIF</span>
<span class="ghg-powered-by-giphy" />
<select class="ghg-provider-selector">
<option value="tenor">Tenor</option>
<option value="giphy">Giphy</option>
</select>
</div>
<tab-list>
<div class="select-menu-filters">
Expand Down
File renamed without changes.
64 changes: 64 additions & 0 deletions src/lib/providers/tenor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export default class Tenor {
constructor(apiToken) {
if (!apiToken) {
throw new Error('[Tenor] API Token required')
}
this.apiToken = apiToken
}

async search(q, offset = 0) {
const params = new URLSearchParams({
q: q,
key: this.apiToken,
client_key: "my_test_app",
limit: 50,
pos: offset,
})
const response = await fetch(`https://tenor.googleapis.com/v2/search?${params.toString()}`)
const decodedResponse = await response.json()

return decodedResponse.results.map(Tenor.convertToGiphyObj)
}

async getTrending(offset = 0) {
const params = new URLSearchParams({
key: this.apiToken,
client_key: "my_test_app",
limit: 50,
})

if (offset > 0) {
params.pos = offset
}

const response = await fetch(`https://tenor.googleapis.com/v2/featured?${params.toString()}`)
const decodedResponse = await response.json()

return decodedResponse.results.map(Tenor.convertToGiphyObj)
}

static convertToGiphyObj(item) {
return {
images: {
original: {
size: item.media_formats.gif.size,
url: item.media_formats.gif.url,
},
downsized_medium: {
size: item.media_formats.mediumgif.size,
url: item.media_formats.mediumgif.url,
},
fixed_width: {
size: item.media_formats.tinygif.size,
url: item.media_formats.tinygif.url,
height: item.media_formats.tinygif.dims[1],
width: item.media_formats.tinygif.dims[0],
},
fixed_width_downsampled: {
size: item.media_formats.tinygif.size,
url: item.media_formats.tinygif.url,
},
}
}
}
}
30 changes: 22 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ import select from 'select-dom'
import observeEl from './lib/simplified-element-observer.js'
import LoadingIndicator from './components/loading-indicator.js'
import GiphyToolbarItem from './components/giphy-toolbar-item.js'
import Giphy from './lib/giphy.js'
import Giphy from './lib/providers/giphy.js'
import Tenor from './lib/providers/tenor.js'

import observe from './lib/selector-observer.js'

// Create a new Giphy Client
const giphyClient = new Giphy('Mpy5mv1k9JRY2rt7YBME2eFRGNs7EGvQ')
const providers = {
tenor: new Tenor('AIzaSyDPNP-ivCtCACDvIV-M0i86TgKbZv5a-0Q'),
giphy: new Giphy('Mpy5mv1k9JRY2rt7YBME2eFRGNs7EGvQ')
};

let provider = providers.tenor;

/**
* Responds to the GIPHY modal being opened or closed.
*/
Expand All @@ -41,7 +47,7 @@ async function watchGiphyModals(element) {
resultsContainer.append(<div>{LoadingIndicator}</div>)

// Fetch the trending gifs
const gifs = await giphyClient.getTrending()
const gifs = await provider.getTrending()

// Clear the loading indicator
resultsContainer.innerHTML = ''
Expand Down Expand Up @@ -164,8 +170,8 @@ async function performSearch(event) {

// If there is no search query, get the trending gifs
const gifs = await (searchQuery === ''
? giphyClient.getTrending()
: giphyClient.search(searchQuery))
? provider.getTrending()
: provider.search(searchQuery))

// Clear any previous results
resultsContainer.innerHTML = ''
Expand Down Expand Up @@ -324,14 +330,21 @@ function handleInfiniteScroll(event) {
resultsContainer.dataset.offset = offset

const gifs = await (searchQuery
? giphyClient.search(searchQuery, offset)
: giphyClient.getTrending(offset))
? provider.search(searchQuery, offset)
: provider.getTrending(offset))

appendResults(resultsContainer, gifs)
}, 250)
}
}

/**
* Updates the GIF provider
*/
function changeProvider(e) {
provider = providers[e.target.value]
}

/**
* Defines the event listeners
*/
Expand All @@ -354,6 +367,7 @@ function listen() {
// What comes after <summary> is the dropdown
watchGiphyModals(event.delegateTarget)
})
delegate('.ghg-provider-selector', 'change', changeProvider)
}

// Ensure we only bind events to elements once
Expand Down
3 changes: 2 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"permissions": [
"contextMenus",
"activeTab",
"https://api.giphy.com/*"
"https://api.giphy.com/*",
"https://tenor.googleapis.com/*"
],
"optional_permissions": [
"http://*/*",
Expand Down