From 3196f6dbe3a3413b1fe8a9f10a2abe563853c99d Mon Sep 17 00:00:00 2001 From: Ephellon Date: Thu, 10 May 2018 03:33:37 -0500 Subject: [PATCH] Add files via upload --- src/flenix.js | 18 ++++++++++---- src/letterboxd.css | 4 +++ src/letterboxd.js | 11 +++++++-- src/manifest.json | 3 ++- src/manifest_firefox.json | 3 ++- src/utils.js | 51 +++++++++++++++++++++------------------ 6 files changed, 58 insertions(+), 32 deletions(-) diff --git a/src/flenix.js b/src/flenix.js index 695e1a1..94eb5be 100644 --- a/src/flenix.js +++ b/src/flenix.js @@ -8,22 +8,23 @@ function isMoviePageReady() { return !!document.querySelector('iframe[title="Disqus"]'); } -async function init() { +function init() { if (isMoviePage()) if (isMoviePageReady()) - await initPlexThingy(); + initPlexThingy(); else // This almost never happens, but sometimes the page is too slow so we need to wait a bit. setTimeout(init, 1000); } -parseOptions().then(async() => { +parseOptions().then(() => { window.addEventListener('popstate', init); window.addEventListener('pushstate-changed', init); - await init(); + init(); }); async function initPlexThingy() { + let $button = renderPlexButton(); if (!$button) return; @@ -39,9 +40,15 @@ async function initPlexThingy() { ), null; + let meta = { + method: 'POST', + headers: {'Content-Type': 'application/x-www-form-urlencoded'}, + mode: 'no-cors' + }; + let title = $title.innerText.trim(), year = parseInt($date.innerText), - IMDbID = await getIDs({ title, year, type: 'imdb' }); + IMDbID = await getIDs({ title, year, type: 'imdb', meta }); findPlexMedia({ title, year, button: $button, IMDbID, type: 'movie', remote: '/engine/ajax/get.php', locale: 'flenix' }); } @@ -77,6 +84,7 @@ function renderPlexButton() { el.title = 'Loading...'; el.classList.add((li? 'flatButton': 'roundButton'), 'web-to-plex-button'); e.appendChild(li? (pa.appendChild(el), pa): el); + return els.push(el); }); diff --git a/src/letterboxd.css b/src/letterboxd.css index 83b02c6..b5d4906 100644 --- a/src/letterboxd.css +++ b/src/letterboxd.css @@ -5,6 +5,10 @@ line-height: 1.66666667; } +.web-to-plex-button:hover { + color: #f67e56!important; +} + .web-to-plex-notification { cursor: pointer; display: block; diff --git a/src/letterboxd.js b/src/letterboxd.js index 21be22b..d7661a8 100644 --- a/src/letterboxd.js +++ b/src/letterboxd.js @@ -34,13 +34,20 @@ function renderPlexButton() { return; let parentEl = document.createElement('p'), - el = document.createElement('a'); + el = document.createElement('a'), + ch = document.createElement('span'); + + ch.classList.add('icon', '-web-to-plex'); + ch.setAttribute('style', `background: url("${ chrome.extension.getURL('img/16.png') }") no-repeat !important`); - parentEl.appendChild(el); el.textContent = 'Web to Plex+'; el.title = 'Loading...'; el.classList.add('label', 'web-to-plex-button'); + + parentEl.appendChild(ch); + parentEl.appendChild(el); $actions.appendChild(parentEl); + return el; } diff --git a/src/manifest.json b/src/manifest.json index 7b6c2ee..ba821dc 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -65,5 +65,6 @@ "optional_permissions": [ "http://*/", "https://*/" - ] + ], + "web_accessible_resources": ["img/16.png", "img/48.png"] } diff --git a/src/manifest_firefox.json b/src/manifest_firefox.json index a7d3cbe..e80f85b 100644 --- a/src/manifest_firefox.json +++ b/src/manifest_firefox.json @@ -58,5 +58,6 @@ "storage", "http://*/", "https://*/" - ] + ], + "web_accessible_resources": ["img/16.png", "img/48.png"] } diff --git a/src/utils.js b/src/utils.js index d416c00..003519d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -101,25 +101,28 @@ function parseOptions() { ); } -async function getIDs({ title, year, type, IMDbID, APIID }) { +async function getIDs({ title, year, type, IMDbID, APIID, meta }) { let json = {}, data = {}, - promise; + promise, + api = { + tvdb: 'bcb95f026f9a01ffa707fcff71900e94', + }; type = type || '*'; let url = (type === 'imdb' || (type === '*' && !IMDbID && title && year))? - `https://www.theimdbapi.org/api/find/movie?title=${ title }&year=${ year }`: + `https://api.themoviedb.org/3/search/movie?api_key=${ api.tvdb }&query=${ encodeURI(title) }&year=${ year }`: (type === 'thetvdb' || APIID || (type === '*' && (IMDbID || (title && year))))? (IMDbID)? - `https://api.themoviedb.org/3/find/${IMDbID}?api_key=bcb95f026f9a01ffa707fcff71900e94&external_source=imdb_id`: - `https://api.tvmaze.com/${ (APIID)? `shows/${APIID}`: `search/shows?q=${ title }`}`: + `https://api.themoviedb.org/3/find/${ IMDbID }?api_key=${ api.tvdb }&external_source=imdb_id`: + `https://api.tvmaze.com/${ (APIID)? `shows/${ APIID }`: `search/shows?q=${ encodeURI(title) }`}`: null; if(url === null) return 0; - await fetch(`${url}`) + await(meta? fetch(url/*, meta*/): fetch(url)) .then(response => { return response.json(); }) @@ -130,11 +133,9 @@ async function getIDs({ title, year, type, IMDbID, APIID }) { return json = objects; }); - json = ('movie_results' in json)? - json.movie_results: - ('tv_results' in json)? - json.tv_results: - json; + if('results' in json) { + json = json.results; + } if(json instanceof Array) { let crush = (string) => string.toLowerCase().replace(/\&/g, 'and').replace(/\W+/g, ''); @@ -148,7 +149,7 @@ async function getIDs({ title, year, type, IMDbID, APIID }) { found = (IMDbID === $data.show.externals.imdb || ($data.show.name === title && year === $data.show.premiered.slice(0, 4)))? $data: found; - //api.themoviedb.org/ + //api.themoviedb.org/ < local else if('movie_results' in $data || 'tv_results' in $data) found = (DATA => { for(var i = 0, f = !1, o = DATA.movie_results, l = o.length | 0; i < l; i++) @@ -159,9 +160,9 @@ async function getIDs({ title, year, type, IMDbID, APIID }) { return f? o: f; })($data); - //theimdbapi.org/ + //api.themoviedb.org/ < remote else - found = (IMDbID === $data.imdb_id || $data.title === title && year === $data.year)? + found = ($data.title === title && year == $data.release_date.slice(0, 4))? $data: found; } @@ -175,7 +176,7 @@ async function getIDs({ title, year, type, IMDbID, APIID }) { found = (crush($data.show.name) == crush(title))? $data: found; - //api.themoviedb.org/ + //api.themoviedb.org/ < local else if('movie_results' in $data || 'tv_results' in $data) found = (DATA => { for(var i = 0, f = !1, o = DATA.movie_results, l = o.length | 0; i < l; i++) @@ -186,7 +187,7 @@ async function getIDs({ title, year, type, IMDbID, APIID }) { return f? o: f; })($data); - //theimdbapi.org/ + //api.themoviedb.org/ < remote else found = (crush($data.title) == crush(title))? $data: @@ -196,6 +197,8 @@ async function getIDs({ title, year, type, IMDbID, APIID }) { json = found; } + console.log(json); + if('show' in json) data = json.show.externals; else if('imdb_id' in json) @@ -222,6 +225,8 @@ function showNotification(state, text, timeout, callback) { lastNotification = null; } + callback = callback? callback: () => {}; + let existingEl = document.querySelector('.web-to-plex-notification'); if (existingEl) { document.body.removeChild(existingEl); @@ -231,7 +236,7 @@ function showNotification(state, text, timeout, callback) { el.classList.add('web-to-plex-notification'); el.onclick = () => { el.remove(); - callback && callback(); + callback(); }; if (state === 'warning') { @@ -328,10 +333,10 @@ function pushRadarrRequest(options) { return showNotification('warning', 'Could not add to Radarr: ' + response.error), console.error('Error adding to Radarr:', response.error, response.location); } else if (response && response.success) { - let title = options.title.replace(/\&/g, 'and').replace(/\s+/g, '-').toLowerCase(), + let title = options.title.replace(/\&/g, 'and').replace(/\s+/g, '-').replace(/\W+/g, '').toLowerCase(), TVDbID = options.TVDbID; - showNotification('info', 'Added movie to Radarr', 0, () => window.open(`${config.radarrURL}movies/${title}-${TVDbID}`, '_blank')); + showNotification('info', 'Added movie to Radarr', 0, () => window.open(`${config.radarrURL}movies/${TVDbID? `${title}-${TVDbID}`: '' }`, '_blank')); } else { showNotification('warning', 'Could not add to Radarr: Unknown Error'); } @@ -363,7 +368,7 @@ function pushSonarrRequest(options) { return showNotification('warning', 'Could not add to Sonarr: ' + response.error), console.error('Error adding to Sonarr:', response.error, response.location); } else if (response && response.success) { - let title = options.title.replace(/\&/g, 'and').replace(/\s+/g, '-').toLowerCase(); + let title = options.title.replace(/\&/g, 'and').replace(/\s+/g, '-').replace(/\W+/g, '').toLowerCase(); showNotification('info', 'Added series to Sonarr', 0, () => window.open(`${config.sonarrURL}series/${title}`, '_blank')); } else { @@ -429,7 +434,7 @@ function modifyPlexButton(el, action, title, options) { el.hrefs = dum; el.download = `${options.title} (${options.year})`; el.href = el.hrefs.slice(0, el.index = 1); - tl = (el.href.replace(/.*(?:\.(\w+))$/, '$1') || 'mp3').toUpperCase(); + tl = (el.href.replace(/.*(?:\.(\w+))?$/, '$1') || 'mp3').toUpperCase(); el.textContent = el.textContent.replace(/\d+\/.+?$/, `${el.index}/${el.hrefs.length} (${tl})`); el.hrefs = el.hrefs.join(delimeter); }; @@ -449,7 +454,7 @@ function modifyPlexButton(el, action, title, options) { } el.href = hs.slice(el.index, 1); - tl = (el.href.replace(/.*(?:\.(\w+))$/, '$1') || 'mp3').toUpperCase(); + tl = (el.href.replace(/.*(?:\.(\w+))?$/, '$1') || 'mp3').toUpperCase(); el.textContent = el.textContent.replace(/\d+\/.+?$/, `${++el.index}/${hs.length} (${tl})`); }); break; @@ -479,7 +484,7 @@ function modifyPlexButton(el, action, title, options) { xhr.send(data); } else { el.href = '#'; - el.textContent = 'Download ' + ty; + el.textContent = 'Get ' + ty; el.classList.add('web-to-plex-button--downloader'); el.addEventListener('click', e => { e.preventDefault();