Skip to content

Commit

Permalink
DBCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamlinerm committed Sep 15, 2023
1 parent 63f47de commit c5eab25
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
9 changes: 8 additions & 1 deletion chrome/badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
// }
});
});

Expand Down
6 changes: 4 additions & 2 deletions chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
52 changes: 30 additions & 22 deletions chrome/skipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ if (isPrimeVideo || isNetflix || isDisney || isHotstar) {
if (oldValue === undefined || settings.Statistics.SegmentsSkipped === 0) {
resetBadge();
}
} else if (key == "DBCache") {
DBCache = newValue;
}
}
});
Expand Down Expand Up @@ -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;
}

Expand All @@ -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");

Expand Down
11 changes: 7 additions & 4 deletions firefox/skipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ if (isPrimeVideo || isNetflix || isDisney || isHotstar) {
if (oldValue === undefined || settings.Statistics.SegmentsSkipped === 0) {
resetBadge();
}
} else if (key == "DBCache") {
DBCache = newValue;
}
}
});
Expand Down Expand Up @@ -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);
}
}
});
Expand Down

0 comments on commit c5eab25

Please sign in to comment.