diff --git a/chrome/badge.js b/chrome/badge.js index 67e220a8..376787b5 100644 --- a/chrome/badge.js +++ b/chrome/badge.js @@ -39,7 +39,7 @@ async function setBadgeText(text, tabId = null) { } // receive message from content script with the badgeText and set it in the badge -chrome.runtime.onMessage.addListener(function (message, sender) { +chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { chrome.storage.local.get("Badges", function (result) { Badges = result.Badges; if (Badges === undefined) { @@ -54,6 +54,13 @@ chrome.runtime.onMessage.addListener(function (message, sender) { chrome.storage.local.set({ Badges }); chrome.action.setBadgeText({ text: "", tabId: sender.tab.id }); } + // else if (message.url) { + // fetch(message.url) + // .then((response) => response.json()) + // .then((data) => sendResponse(data)) + // .catch((error) => console.error(error)); + // return true; // Indicates that sendResponse will be called asynchronously + // } }); }); diff --git a/chrome/manifest.json b/chrome/manifest.json index ad08a483..abb9b436 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -24,12 +24,14 @@ "*://*.netflix.ca/*", "*://*.netflix.com.au/*", "*://*.disneyplus.com/*", - "*://*.hotstar.com/*" + "*://*.hotstar.com/*", + "https://apis.justwatch.com/*" ], "js": ["skipper.js"] } ], - "permissions": ["storage", "*://apis.justwatch.com/*"], + "permissions": ["storage"], + "host_permissions": ["https://apis.justwatch.com/*"], "action": { "default_icon": { "16": "icons/NetflixAmazon Auto-Skip--16.png", diff --git a/chrome/skipper.js b/chrome/skipper.js index 88254598..a41b7f3e 100644 --- a/chrome/skipper.js +++ b/chrome/skipper.js @@ -144,6 +144,8 @@ if (isPrimeVideo || isNetflix || isDisney || isHotstar) { if (oldValue === undefined || settings.Statistics.SegmentsSkipped === 0) { resetBadge(); } + } else if (key == "DBCache") { + DBCache = newValue; } } }); @@ -197,23 +199,28 @@ if (isPrimeVideo || isNetflix || isDisney || isHotstar) { async function getMovieInfo(movieTitle, locale = "en_US") { // console.log("getMovieInfo", movieTitle); const url = `https://apis.justwatch.com/content/titles/${locale}/popular?language=en&body={"page_size":1,"page":1,"query":"${movieTitle}","content_types":["show","movie"]}`; - const response = await fetch(encodeURI(url)); - const data = await response.json(); - if (data) { - // "https://www.justwatch.com" + data.items[0].full_path; - const justWatchURL = data?.items?.[0]?.full_path; - // flatrate = free with subscription (netflix, amazon prime, disney+) - let offers = data?.items?.[0].offers?.filter((x) => x.monetization_type == "flatrate" && (x.package_short_name == "amp" || x.package_short_name == "nfx" || x.package_short_name == "dnp")); - // get the first offer of each provider - offers = offers?.filter((x, i) => offers.findIndex((y) => y.provider_id == x.provider_id) == i); - // map offers to only package_short_name, country and standard_web url - offers = offers?.map((x) => ({ country: x.country, package_short_name: x.package_short_name, url: x.urls.standard_web })); - return { - jWURL: justWatchURL, - score: data?.items?.[0]?.scoring?.filter((x) => x.provider_type == "imdb:score")?.[0]?.value, - streamLinks: offers, - }; - } + chrome.runtime.sendMessage({ url }).then( + (data) => { + console.log(data); + + if (data) { + // "https://www.justwatch.com" + data.items[0].full_path; + const justWatchURL = data?.items?.[0]?.full_path; + // flatrate = free with subscription (netflix, amazon prime, disney+) + let offers = data?.items?.[0].offers?.filter((x) => x.monetization_type == "flatrate" && (x.package_short_name == "amp" || x.package_short_name == "nfx" || x.package_short_name == "dnp")); + // get the first offer of each provider + offers = offers?.filter((x, i) => offers.findIndex((y) => y.provider_id == x.provider_id) == i); + // map offers to only package_short_name, country and standard_web url + offers = offers?.map((x) => ({ country: x.country, package_short_name: x.package_short_name, url: x.urls.standard_web })); + return { + jWURL: justWatchURL, + score: data?.items?.[0]?.scoring?.filter((x) => x.provider_type == "imdb:score")?.[0]?.value, + streamLinks: offers, + }; + } + }, + { timeout: 5000 } + ); return null; } @@ -227,24 +234,25 @@ if (isPrimeVideo || isNetflix || isDisney || isHotstar) { if (isNetflix) titleCards = document.querySelectorAll(".title-card .boxart-container:not(.imdb)"); else titleCards = document.querySelectorAll("li:not(.imdb) [data-card-title]"); titleCards.forEach((card) => { - // let card = document.querySelector(".title-card .boxart-container"); + // let card = document.querySelector("li:not(.imdb) [data-card-title]"); + let card = document.querySelector(".title-card .boxart-container:not(.imdb)"); let title; - if (isNetflix) title = card.children?.[1]?.firstChild?.textContent; + if (isNetflix) title = card?.children?.[1]?.firstChild?.textContent; // remove everything after - in the title else title = card.getAttribute("data-card-title").split(" - ")[0].split(" – ")[0]; //Amazon if (title && !title.includes("Netflix") && !title.includes("Prime Video")) { if (!DBCache[title]) { getMovieInfo(title).then((data) => { if (data) DBCache[title] = data; - setRatingOnCard(card, data); + setRatingOnCard(card, data, title); }); } else { - setRatingOnCard(card, DBCache[title]); + setRatingOnCard(card, DBCache[title], title); } } }); } - async function setRatingOnCard(card, data) { + async function setRatingOnCard(card, data, title) { if (isNetflix) card.classList.add("imdb"); else card.parentElement.classList.add("imdb"); diff --git a/firefox/skipper.js b/firefox/skipper.js index 4bbb8433..bac8bc85 100644 --- a/firefox/skipper.js +++ b/firefox/skipper.js @@ -144,6 +144,8 @@ if (isPrimeVideo || isNetflix || isDisney || isHotstar) { if (oldValue === undefined || settings.Statistics.SegmentsSkipped === 0) { resetBadge(); } + } else if (key == "DBCache") { + DBCache = newValue; } } }); @@ -227,19 +229,20 @@ if (isPrimeVideo || isNetflix || isDisney || isHotstar) { if (isNetflix) titleCards = document.querySelectorAll(".title-card .boxart-container:not(.imdb)"); else titleCards = document.querySelectorAll("li:not(.imdb) [data-card-title]"); titleCards.forEach((card) => { - // let card = document.querySelector(".title-card .boxart-container"); + // let card = document.querySelector("li:not(.imdb) [data-card-title]"); + let card = document.querySelector(".title-card .boxart-container:not(.imdb)"); let title; - if (isNetflix) title = card.children?.[1]?.firstChild?.textContent; + if (isNetflix) title = card?.children?.[1]?.firstChild?.textContent; // remove everything after - in the title else title = card.getAttribute("data-card-title").split(" - ")[0].split(" – ")[0]; //Amazon if (title && !title.includes("Netflix") && !title.includes("Prime Video")) { if (!DBCache[title]) { getMovieInfo(title).then((data) => { if (data) DBCache[title] = data; - setRatingOnCard(card, data); + setRatingOnCard(card, data, title); }); } else { - setRatingOnCard(card, DBCache[title]); + setRatingOnCard(card, DBCache[title], title); } } });