Skip to content

Commit

Permalink
Add senscritique support (#44)
Browse files Browse the repository at this point in the history
* feat: add support for senscritique website

* docs: add senscritique to supported site list

* Add mediaType update on page location change

* Remove version upgrade from PR

---------

Co-authored-by: RemiRigal <[email protected]>
  • Loading branch information
RodolpheGeiler and RemiRigal authored Feb 19, 2024
1 parent 7ce2a62 commit 288881a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Supported media websites:
- [Trakt.tv](https://trakt.tv/)
- [Taste.io](https://www.taste.io/)
- [Allociné](https://www.allocine.fr/)
- [Senscritique](https://www.senscritique.com/)

## Install

Expand Down
15 changes: 15 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,20 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
chrome.runtime.openOptionsPage();
return true;
}

else if (request.contentScriptQuery === 'listenForUrlChange') {
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (
changeInfo.status === 'complete' &&
tab.status === 'complete' &&
tab.url &&
tab.url.startsWith('https://www.senscritique.com')
) {
chrome.tabs.sendMessage(tab.id, {
newUrl: tab.url
});
}
});
}
return false;
});
96 changes: 96 additions & 0 deletions js/content-scripts/senscritique.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
let overseerrContainer, senscritiqueId, currentSenscritiqueUrl, tmdbId, mediaType, mediaInfo;

containerOptions.anchorElement = 'div[data-testid="product-infos"]';
containerOptions.textClass = 'text-sm';
containerOptions.containerClass = 'oa-mt-2 oa-py-2';
containerOptions.plexButtonClass = 'oa-bg-gray-800';
containerOptions.badgeBackground = '#032541';

mediaType = document.location.pathname.startsWith('/film') ? 'movie' : 'tv';
const senscritiqueRegex = /\/(?:film|serie)\/\w*\/(\d+)/;

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.newUrl && message.newUrl !== currentSenscritiqueUrl) {
currentSenscritiqueUrl = message.newUrl;
setTimeout(() => {
checkForMedia(message.newUrl);
}, 500);
}
});

chrome.runtime.sendMessage({contentScriptQuery: 'listenForUrlChange'});

const checkForMedia = async (urlToCheck) => {
let matches = urlToCheck.match(senscritiqueRegex);
if (matches !== null && matches.length > 1) {
mediaType = document.location.pathname.startsWith('/film') ? 'movie' : 'tv';

const titleElement = document.querySelector('h1');
let title = titleElement.textContent;
let releaseYear = extractYear(document.querySelector('p[data-testid="creators"]').textContent);
let displayedYear = parseInt(titleElement.nextElementSibling.querySelector('p:not([opacity])').textContent);

initializeContainer();
insertSpinner();

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

try {
let json = await sendMessageToBackground({contentScriptQuery: 'search', title: title});
json.results = filterResults(json.results, releaseYear, displayedYear);
if (json.results.length === 0) {
removeSpinner();
insertStatusButton('Media not found', 0);
return;
}
const firstResult = json.results[0];
json = await sendMessageToBackground({contentScriptQuery: 'queryMedia', tmdbId: firstResult.id, mediaType: mediaType});
mediaInfo = json;
tmdbId = json.id;
console.log(`TMDB id: ${tmdbId}`);
removeSpinner();
fillContainer(json.mediaInfo);
} catch (error) {
console.error(error);
}
});
}
}

const extractYear = (creatorInformationsString) => {
let parts = creatorInformationsString.split('·');
if (parts.length >= 3) {
let yearPart = parts[2].trim();
let regex = /\b(\d{4})\b/;
let match = yearPart.match(regex);
if (match) {
let year = match[1];
return parseInt(year);
}
}
return false;
}

const sendMessageToBackground = (message) => {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(message, (response) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(response);
}
});
});
}

const filterResults = (results, releaseYear, displayedYear) => {
return results.filter((result) => {
return result.mediaType === mediaType &&
(!releaseYear || (result.releaseDate && [releaseYear, displayedYear].includes(parseInt(result.releaseDate.slice(0, 4)))));
});
}
4 changes: 4 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"matches": ["https://www.allocine.fr/film/*", "https://www.allocine.fr/series/*"],
"css": ["css/style.css"],
"js": ["js/lib/jquery-3.5.1.min.js", "js/storage.js", "js/overseerr-container.js", "js/content-scripts/allocine.js"]
}, {
"matches": ["https://www.senscritique.com/*"],
"css": ["css/style.css"],
"js": ["js/lib/jquery-3.5.1.min.js", "js/storage.js", "js/overseerr-container.js", "js/content-scripts/senscritique.js"]
},{
"matches": ["https://thetvdb.com/movies/*", "https://thetvdb.com/series/*"],
"css": ["css/style.css"],
Expand Down

0 comments on commit 288881a

Please sign in to comment.