Skip to content

Commit

Permalink
Improved Plex Discover support (RemiRigal#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Rigal committed Apr 15, 2022
1 parent 4bbd3b5 commit 7081c4e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 38 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
[![Chrome Web Store Downloads](https://img.shields.io/chrome-web-store/d/hopnjiadheaagfhpipecoamoegijhnij.svg?style=flat-square)](https://chrome.google.com/webstore/detail/hopnjiadheaagfhpipecoamoegijhnij/reviews)
[![Chrome Web Store Rating](https://img.shields.io/chrome-web-store/stars/hopnjiadheaagfhpipecoamoegijhnij.svg?style=flat-square)](https://chrome.google.com/webstore/detail/hopnjiadheaagfhpipecoamoegijhnij/reviews)

Overseerr Assistant is a browser extension for [Overseerr](https://github.com/sct/overseerr). It provides an integration for TMDB and IMDb websites.
Overseerr Assistant is a browser extension for [Overseerr](https://github.com/sct/overseerr). It provides an integration for most popular media websites.

Features:
- One-click Overseerr requests directly from TMDB and IMDb websites
- One-click Overseerr requests directly from media websites
- One-click access to available media on Plex
- Monitor requests status
- Support for both movies and TV shows

Supported media websites:
- [TMDB](https://www.themoviedb.org/)
- [IMDb](https://www.imdb.com/)
- [TVDB](https://thetvdb.com/)
- [Plex Discover](https://app.plex.tv/desktop/#!/media/tv.plex.provider.discover?source=home)
- [Letterboxd](https://letterboxd.com/)
- [Allociné](https://www.allocine.fr/)

## Install

**[Available on the Chrome Web Store](https://chrome.google.com/webstore/detail/hopnjiadheaagfhpipecoamoegijhnij)**
Expand Down
13 changes: 13 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,21 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
return true;
}

else if (request.contentScriptQuery === 'plexQueryMedia') {
let mediaKey = encodeURIComponentSafe(request.mediaKey);
let plexToken = encodeURIComponentSafe(request.plexToken);
console.log(`Requesting Plex media '${mediaKey}'`);
const options = {headers: {'Accept': 'application/json'}};
fetch(`https://metadata.provider.plex.tv/library/metadata/${mediaKey}?X-Plex-Token=${plexToken}`, options)
.then(response => response.json())
.then(json => sendResponse(json))
.catch(error => console.error(error));
return true;
}

else if (request.contentScriptQuery === 'openOptionsPage') {
chrome.runtime.openOptionsPage();
return true;
}
return false;
});
88 changes: 52 additions & 36 deletions js/content-scripts/plex.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ containerOptions.anchorElement = 'div.PrePlayActionBar-container-US01pp';
containerOptions.containerClass = 'mb-3 py-2';
containerOptions.badgeBackground = '#00000099';

const titleElementSelector = 'div.PrePlayLeftTitle-leftTitle-ewTpwH';
const dateElementSelector = 'div.PrePlaySecondaryTitle-secondaryTitle-BA3QVn';


function isHashValid(hash) {
return hash.startsWith('#!/provider/');
Expand Down Expand Up @@ -41,48 +38,67 @@ function arrangeMargins() {
});
}

function getPlexToken() {
let imageElements = $('img').filter(function() {
return $(this).attr('src').includes('X-Plex-Token');
});
if (imageElements.length > 0) {
let pattern = /.X-Plex-Token=(\w+)/;
let matches = imageElements.attr('src').match(pattern);
if (matches !== null && matches.length > 1) {
return matches[1];
}
}
return null;
}

function getMediaKey() {
let pattern = /key=%2Flibrary%2Fmetadata%2F(\w+)/;
let matches = document.location.hash.match(pattern);
if (matches !== null && matches.length > 1) {
return matches[1];
}
return null;
}

function processPage() {
if (overseerrContainer) overseerrContainer.remove();

waitForElm(titleElementSelector).then((titleElement) => {
let title = titleElement.textContent;
let releaseYear = parseInt($(dateElementSelector).text()) || null;
console.log(title, releaseYear);
waitForElm('div.PrePlayLeftTitle-leftTitle-ewTpwH').then(() => {
waitForElm('div.ImagePoster-flex-Ry0HC5 > img').then(() => {
initializeContainer();
insertSpinner();
arrangeMargins();

pullStoredData(function () {
if (!userId) {
removeSpinner();
insertNotLoggedInButton();
return;
}

initializeContainer();
insertSpinner();
arrangeMargins();
let plexToken = getPlexToken();
let mediaKey = getMediaKey();

pullStoredData(function () {
if (!userId) {
removeSpinner();
insertNotLoggedInButton();
return;
}

chrome.runtime.sendMessage({contentScriptQuery: 'search', title: title}, json => {
json.results = json.results
.filter((result) => result.mediaType === 'movie' || result.mediaType === 'tv')
.filter((result) => {
if(!releaseYear) {
return true;
}
let date = result.releaseDate || result.firstAirDate || null;
return date && parseInt(date.slice(0, 4)) === releaseYear;
});
if (json.results.length === 0) {
if (plexToken === null || mediaKey === null) {
removeSpinner();
insertStatusButton('Media not found', 0);
return;
}
const firstResult = json.results[0];
mediaType = firstResult.mediaType;
chrome.runtime.sendMessage({contentScriptQuery: 'queryMedia', tmdbId: firstResult.id, mediaType: mediaType}, json => {
mediaInfo = json;
tmdbId = json.id;
console.log(`TMDB id: ${tmdbId}`);
removeSpinner();
fillContainer(json.mediaInfo);

chrome.runtime.sendMessage({contentScriptQuery: 'plexQueryMedia', plexToken: plexToken, mediaKey: mediaKey}, json => {
let guids = json.MediaContainer.Metadata[0].Guid.filter(guid => guid.id.startsWith('tmdb'));
if (guids.length > 0) {
tmdbId = parseInt(guids[0].id.replace('tmdb://', ''));
mediaType = json.MediaContainer.Metadata[0].type === 'movie' ? 'movie' : 'tv';
console.log(`TMDB id: ${tmdbId}`);
chrome.runtime.sendMessage({contentScriptQuery: 'queryMedia', tmdbId: tmdbId, mediaType: mediaType}, json => {
mediaInfo = json;
console.log(json.name);
removeSpinner();
fillContainer(json.mediaInfo);
});
}
});
});
});
Expand Down

0 comments on commit 7081c4e

Please sign in to comment.