From 066e8ee3d785f5080082d129478146b4f6661625 Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Thu, 15 Dec 2016 12:03:54 +0100 Subject: [PATCH] Make it even easier to add multiple providers Ref #6 --- src/imdb.css | 10 +++++----- src/imdb.js | 43 ++----------------------------------------- src/movieo.js | 42 ++---------------------------------------- src/utils.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 86 deletions(-) diff --git a/src/imdb.css b/src/imdb.css index 368f037..24e03b4 100644 --- a/src/imdb.css +++ b/src/imdb.css @@ -1,4 +1,4 @@ -.imdb-to-plex-button { +.movieo-to-plex-button { margin-top: 10px; display: inline-block; background: #727272; @@ -9,19 +9,19 @@ text-transform: uppercase; } -.imdb-to-plex-button--couchpotato { +.movieo-to-plex-button--couchpotato { background: #f45a26; } -.imdb-to-plex-button--couchpotato:hover { +.movieo-to-plex-button--couchpotato:hover { background: #f67e56; } -.imdb-to-plex-button--found { +.movieo-to-plex-button--found { background: #e5a00d; } -.imdb-to-plex-button--found:hover { +.movieo-to-plex-button--found:hover { background: #f9be03; } diff --git a/src/imdb.js b/src/imdb.js index c0cc137..b5a0e8c 100644 --- a/src/imdb.js +++ b/src/imdb.js @@ -17,37 +17,11 @@ function renderPlexButton() { return; } const el = document.createElement('a'); - el.classList.add('imdb-to-plex-button'); + el.classList.add('movieo-to-plex-button'); $plotSummary.appendChild(el); return el; } -function modifyPlexButton(el, action, title, key) { - if (action === 'found') { - el.href = getPlexMediaUrl(config.plexMachineId, key); - el.textContent = 'On Plex'; - el.classList.add('imdb-to-plex-button--found'); - } - if (action === 'error') { - el.removeAttribute('href'); - el.textContent = 'Not on Plex'; - el.classList.remove('imdb-to-plex-button--found'); - } - if (action === 'couchpotato') { - el.href = '#'; - el.textContent = 'Download'; - el.classList.add('imdb-to-plex-button--couchpotato'); - el.addEventListener('click', (e) => { - e.preventDefault(); - addToCouchpotato(config, imdbId); - }); - } - - if (title) { - el.title = title; - } -} - function initPlexThingy() { $button = renderPlexButton(); if (!$button) { @@ -60,20 +34,7 @@ function initPlexThingy() { // The year element contains `()`, so we need to strip it out. const year = $year.textContent.trim().replace(/\(|\)/g, ''); - plexRequest({ url: config.plexUrl, token: config.plexToken, title, year }) - .then(({ size, key }) => { - if (size) { - modifyPlexButton($button, 'found', 'Found on Plex', key); - } else { - const action = config.couchpotatoUrl ? 'couchpotato' : 'error'; - const title = config.couchpotatoUrl ? 'Could not find, add on Couchpotato?' : 'Could not find on Plex'; - modifyPlexButton($button, action, title); - } - }) - .catch((err) => { - modifyPlexButton($button, 'error', 'Request to Plex failed'); - console.error('Request to Plex failed', err); - }); + handlePlex(config, { title, year, button: $button, imdbId }); } let config; diff --git a/src/movieo.js b/src/movieo.js index cf414ee..6701ba9 100644 --- a/src/movieo.js +++ b/src/movieo.js @@ -52,21 +52,9 @@ function initPlexThingy() { } const title = $title.dataset.title.trim(); const year = parseInt($date.content.slice(0, 4)); + const imdbId = getImdbId(); - plexRequest({ url: config.plexUrl, token: config.plexToken, title, year }) - .then(({ size, key }) => { - if (size) { - modifyPlexButton($button, 'found', 'Found on Plex', key); - } else { - const action = config.couchpotatoUrl ? 'couchpotato' : 'error'; - const title = config.couchpotatoUrl ? 'Could not find, add on Couchpotato?' : 'Could not find on Plex'; - modifyPlexButton($button, action, title); - } - }) - .catch((err) => { - modifyPlexButton($button, 'error', 'Request to Plex failed'); - console.error('Request to Plex failed', err); - }); + handlePlex(config, { title, year, button: $button, imdbId }); } function renderPlexButton() { @@ -88,32 +76,6 @@ function renderPlexButton() { return el; } -function modifyPlexButton(el, action, title, key) { - if (action === 'found') { - el.href = getPlexMediaUrl(config.plexMachineId, key); - el.textContent = 'On Plex'; - el.classList.add('movieo-to-plex-button--found'); - } - if (action === 'error') { - el.removeAttribute('href'); - el.textContent = 'Not on Plex'; - el.classList.remove('movieo-to-plex-button--found'); - } - if (action === 'couchpotato') { - el.href = '#'; - el.textContent = 'Download'; - el.classList.add('movieo-to-plex-button--couchpotato'); - el.addEventListener('click', (e) => { - e.preventDefault(); - addToCouchpotato(config, getImdbId()); - }); - } - - if (title) { - el.title = title; - } -} - function getImdbId() { const $link = document.querySelector('.tt-parent[href^="http://www.imdb.com/title/tt"]'); if ($link) { diff --git a/src/utils.js b/src/utils.js index f923992..59736a8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -132,3 +132,46 @@ function addToCouchPotatoRequest(imdbId) { } }); } + +function modifyPlexButton(el, action, title, key) { + if (action === 'found') { + el.href = getPlexMediaUrl(config.plexMachineId, key); + el.textContent = 'On Plex'; + el.classList.add('movieo-to-plex-button--found'); + } + if (action === 'error') { + el.removeAttribute('href'); + el.textContent = 'Not on Plex'; + el.classList.remove('movieo-to-plex-button--found'); + } + if (action === 'couchpotato') { + el.href = '#'; + el.textContent = 'Download'; + el.classList.add('movieo-to-plex-button--couchpotato'); + el.addEventListener('click', (e) => { + e.preventDefault(); + addToCouchpotato(config, key); + }); + } + + if (title) { + el.title = title; + } +} + +function handlePlex(config, options) { + plexRequest({ url: config.plexUrl, token: config.plexToken, title: options.title, year: options.year }) + .then(({ size, key }) => { + if (size) { + modifyPlexButton(options.button, 'found', 'Found on Plex', key); + } else { + const action = config.couchpotatoUrl ? 'couchpotato' : 'error'; + const title = config.couchpotatoUrl ? 'Could not find, add on Couchpotato?' : 'Could not find on Plex'; + modifyPlexButton(options.button, action, title, options.imdbId); + } + }) + .catch((err) => { + modifyPlexButton(options.button, 'error', 'Request to Plex failed'); + console.error('Request to Plex failed', err); + }); +}