diff --git a/dist/chrome/lb-imdb.zip b/dist/chrome/lb-imdb.zip index 10012e7..b8b8838 100644 Binary files a/dist/chrome/lb-imdb.zip and b/dist/chrome/lb-imdb.zip differ diff --git a/dist/chrome/src/js/background.js b/dist/chrome/src/js/background.js index c069f05..b7daf74 100644 --- a/dist/chrome/src/js/background.js +++ b/dist/chrome/src/js/background.js @@ -31,8 +31,8 @@ browser.runtime.onInstalled.addListener(() => { }); -// For Firefox Manifest v2 -if (typeof browser.webRequest.onHeadersReceived !== "undefined") { +// strip headers, for Firefox Manifest v2 +if (typeof browser.webRequest !== "undefined" && typeof browser.webRequest.onHeadersReceived !== "undefined") { function stripHeaders(response) { console.log("stripping headers. respone: ", response); let headers = response.responseHeaders.filter(header => { @@ -51,96 +51,6 @@ if (typeof browser.webRequest.onHeadersReceived !== "undefined") { } - -browser.runtime.onMessage.addListener( - function(request, sender, sendResponse) { - if (typeof request.imdbCode !== 'undefined') { - getIMDBData(request.imdbCode, sendResponse); - return true; - } - } -); - - - -async function getIMDBData(imdbCode, callback) { - - let nonSpoilerItems = await imdbRequest(imdbCode, false) - let spoilerItems = await imdbRequest(imdbCode, true) - - let data = { - imdbCode: imdbCode, - nonSpoilerItems: nonSpoilerItems, - spoilerItems: spoilerItems - } - - callback(data); -} - -async function imdbRequest(imdbCode, spoilers = false, pagePointer = null) { - - let triviaItems = []; - - var variables = { - // "after" : "pointer_to_next_page", - "const": imdbCode, - "filter":{ - // "categories":["uncategorized"] to limit to categories - // "spoilers":"EXCLUDE_SPOILERS" or "SPOILERS_ONLY" - }, - "first":50, - "isAutoTranslationEnabled": false, - "locale":"en-US", - "originalTitleText":false - } - - if (spoilers) { - variables.filter.spoilers = "SPOILERS_ONLY"; - } else { - variables.filter.spoilers = "EXCLUDE_SPOILERS"; - } - - if (pagePointer) { - variables.after = pagePointer; - } - - const extensions = { - "persistedQuery":{ - "sha256Hash": "101651d4fef546dfc31d45f2bf864f588bf4951c8e4720de555d1aa2d9050df2", - "version":1 - } - } - - let url = 'https://caching.graphql.imdb.com/?operationName=TitleTriviaPagination'; - - url += '&variables=' + encodeURIComponent(JSON.stringify(variables)); - url += '&extensions=' + encodeURIComponent(JSON.stringify(extensions)); - - const response = await fetch(url, { - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/graphql+json, application/json' - } - }); - - - if (response.ok) { - const data = await response.json(); - - if (typeof data.data !== "undefined") { - triviaItems.push(...data.data.title.trivia.edges); - - if (data.data.title.trivia.pageInfo.hasNextPage) { - let nextPage = await imdbRequest(imdbCode, spoilers, data.data.title.trivia.pageInfo.endCursor); - triviaItems.push(...nextPage); - } - } - - } - - return triviaItems; -} - browser.runtime.onMessage.addListener(function(message) { switch (message.action) { case "openOptionsPage": diff --git a/dist/chrome/src/js/content.js b/dist/chrome/src/js/content.js index e503b30..79b1026 100644 --- a/dist/chrome/src/js/content.js +++ b/dist/chrome/src/js/content.js @@ -22,10 +22,7 @@ function init() { var arr = rx.exec(imdbLink); imdbCode = arr[1]; if (typeof imdbCode !== 'undefined') { - insertIframe(imdbCode); - - //browser.runtime.sendMessage({imdbCode: imdbCode}, gotIMDBData); } } } @@ -56,77 +53,6 @@ window.addEventListener('message', function(event) { }); -function gotIMDBData(data) { - - if (data.nonSpoilerItems.length > 0 || data.spoilerItems.length > 0) { - var triviaCategories = getTriviaCategoriesFromData(data); - if (triviaCategories.length > 0) { - insertTriviaCategories(triviaCategories); - } - } else { - insertFallback(data.imdbCode); - } -} - - -/** - * Get array of trivia html, grouped by category, with spoilers and non-spoilers - * [ - * { - * "category": "Uncategorized", - * "spoilerItems": [html, html, html, ...] - * "nonSpoilerItems": [html, html, html, ...] - * }, - * { - * "category": "Cameo", - * "spoilerItems": [html, html, html, ...] - * "nonSpoilerItems": [html, html, html, ...] - * }, - * ... - * ] - * @param {string} imdbCode - * @param {function} done - */ -function getTriviaCategoriesFromData(data) { - let categories = []; - - processItems(data.nonSpoilerItems, 'nonSpoilerItems', categories); - processItems(data.spoilerItems, 'spoilerItems', categories); - - return categories; -} - -/** - * Extract trivia and categories from data - * @param items - * @param itemType - * @param categories - */ -function processItems(items, itemType, categories) { - items.forEach(function(item) { - let category = item.node.category.text; - - let triviaItem = { - 'html' : item.node.displayableArticle.body.plaidHtml, - 'usersVoted': item.node.interestScore.usersVoted, - 'usersInterested': item.node.interestScore.usersInterested - } - - let existingCategory = categories.find(cat => cat.category === category); - if (existingCategory) { - if (!Array.isArray(existingCategory[itemType])) { - existingCategory[itemType] = []; - } - existingCategory[itemType].push(triviaItem); - } else { - categories.push({ - 'category': category, - [itemType]: [triviaItem] - }); - } - }); -} - // thx to stackbykumbi https://stackoverflow.com/a/69361046/1191375 function nullthrows(v) { if (v == null) throw new Error("it's a null"); diff --git a/dist/firefox/lb-imdb.zip b/dist/firefox/lb-imdb.zip index 89aff0a..2e0f663 100644 Binary files a/dist/firefox/lb-imdb.zip and b/dist/firefox/lb-imdb.zip differ diff --git a/dist/firefox/src/js/background.js b/dist/firefox/src/js/background.js index c069f05..b7daf74 100644 --- a/dist/firefox/src/js/background.js +++ b/dist/firefox/src/js/background.js @@ -31,8 +31,8 @@ browser.runtime.onInstalled.addListener(() => { }); -// For Firefox Manifest v2 -if (typeof browser.webRequest.onHeadersReceived !== "undefined") { +// strip headers, for Firefox Manifest v2 +if (typeof browser.webRequest !== "undefined" && typeof browser.webRequest.onHeadersReceived !== "undefined") { function stripHeaders(response) { console.log("stripping headers. respone: ", response); let headers = response.responseHeaders.filter(header => { @@ -51,96 +51,6 @@ if (typeof browser.webRequest.onHeadersReceived !== "undefined") { } - -browser.runtime.onMessage.addListener( - function(request, sender, sendResponse) { - if (typeof request.imdbCode !== 'undefined') { - getIMDBData(request.imdbCode, sendResponse); - return true; - } - } -); - - - -async function getIMDBData(imdbCode, callback) { - - let nonSpoilerItems = await imdbRequest(imdbCode, false) - let spoilerItems = await imdbRequest(imdbCode, true) - - let data = { - imdbCode: imdbCode, - nonSpoilerItems: nonSpoilerItems, - spoilerItems: spoilerItems - } - - callback(data); -} - -async function imdbRequest(imdbCode, spoilers = false, pagePointer = null) { - - let triviaItems = []; - - var variables = { - // "after" : "pointer_to_next_page", - "const": imdbCode, - "filter":{ - // "categories":["uncategorized"] to limit to categories - // "spoilers":"EXCLUDE_SPOILERS" or "SPOILERS_ONLY" - }, - "first":50, - "isAutoTranslationEnabled": false, - "locale":"en-US", - "originalTitleText":false - } - - if (spoilers) { - variables.filter.spoilers = "SPOILERS_ONLY"; - } else { - variables.filter.spoilers = "EXCLUDE_SPOILERS"; - } - - if (pagePointer) { - variables.after = pagePointer; - } - - const extensions = { - "persistedQuery":{ - "sha256Hash": "101651d4fef546dfc31d45f2bf864f588bf4951c8e4720de555d1aa2d9050df2", - "version":1 - } - } - - let url = 'https://caching.graphql.imdb.com/?operationName=TitleTriviaPagination'; - - url += '&variables=' + encodeURIComponent(JSON.stringify(variables)); - url += '&extensions=' + encodeURIComponent(JSON.stringify(extensions)); - - const response = await fetch(url, { - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/graphql+json, application/json' - } - }); - - - if (response.ok) { - const data = await response.json(); - - if (typeof data.data !== "undefined") { - triviaItems.push(...data.data.title.trivia.edges); - - if (data.data.title.trivia.pageInfo.hasNextPage) { - let nextPage = await imdbRequest(imdbCode, spoilers, data.data.title.trivia.pageInfo.endCursor); - triviaItems.push(...nextPage); - } - } - - } - - return triviaItems; -} - browser.runtime.onMessage.addListener(function(message) { switch (message.action) { case "openOptionsPage": diff --git a/dist/firefox/src/js/content.js b/dist/firefox/src/js/content.js index e503b30..79b1026 100644 --- a/dist/firefox/src/js/content.js +++ b/dist/firefox/src/js/content.js @@ -22,10 +22,7 @@ function init() { var arr = rx.exec(imdbLink); imdbCode = arr[1]; if (typeof imdbCode !== 'undefined') { - insertIframe(imdbCode); - - //browser.runtime.sendMessage({imdbCode: imdbCode}, gotIMDBData); } } } @@ -56,77 +53,6 @@ window.addEventListener('message', function(event) { }); -function gotIMDBData(data) { - - if (data.nonSpoilerItems.length > 0 || data.spoilerItems.length > 0) { - var triviaCategories = getTriviaCategoriesFromData(data); - if (triviaCategories.length > 0) { - insertTriviaCategories(triviaCategories); - } - } else { - insertFallback(data.imdbCode); - } -} - - -/** - * Get array of trivia html, grouped by category, with spoilers and non-spoilers - * [ - * { - * "category": "Uncategorized", - * "spoilerItems": [html, html, html, ...] - * "nonSpoilerItems": [html, html, html, ...] - * }, - * { - * "category": "Cameo", - * "spoilerItems": [html, html, html, ...] - * "nonSpoilerItems": [html, html, html, ...] - * }, - * ... - * ] - * @param {string} imdbCode - * @param {function} done - */ -function getTriviaCategoriesFromData(data) { - let categories = []; - - processItems(data.nonSpoilerItems, 'nonSpoilerItems', categories); - processItems(data.spoilerItems, 'spoilerItems', categories); - - return categories; -} - -/** - * Extract trivia and categories from data - * @param items - * @param itemType - * @param categories - */ -function processItems(items, itemType, categories) { - items.forEach(function(item) { - let category = item.node.category.text; - - let triviaItem = { - 'html' : item.node.displayableArticle.body.plaidHtml, - 'usersVoted': item.node.interestScore.usersVoted, - 'usersInterested': item.node.interestScore.usersInterested - } - - let existingCategory = categories.find(cat => cat.category === category); - if (existingCategory) { - if (!Array.isArray(existingCategory[itemType])) { - existingCategory[itemType] = []; - } - existingCategory[itemType].push(triviaItem); - } else { - categories.push({ - 'category': category, - [itemType]: [triviaItem] - }); - } - }); -} - // thx to stackbykumbi https://stackoverflow.com/a/69361046/1191375 function nullthrows(v) { if (v == null) throw new Error("it's a null"); diff --git a/src/shared/js/background.js b/src/shared/js/background.js index c069f05..b7daf74 100644 --- a/src/shared/js/background.js +++ b/src/shared/js/background.js @@ -31,8 +31,8 @@ browser.runtime.onInstalled.addListener(() => { }); -// For Firefox Manifest v2 -if (typeof browser.webRequest.onHeadersReceived !== "undefined") { +// strip headers, for Firefox Manifest v2 +if (typeof browser.webRequest !== "undefined" && typeof browser.webRequest.onHeadersReceived !== "undefined") { function stripHeaders(response) { console.log("stripping headers. respone: ", response); let headers = response.responseHeaders.filter(header => { @@ -51,96 +51,6 @@ if (typeof browser.webRequest.onHeadersReceived !== "undefined") { } - -browser.runtime.onMessage.addListener( - function(request, sender, sendResponse) { - if (typeof request.imdbCode !== 'undefined') { - getIMDBData(request.imdbCode, sendResponse); - return true; - } - } -); - - - -async function getIMDBData(imdbCode, callback) { - - let nonSpoilerItems = await imdbRequest(imdbCode, false) - let spoilerItems = await imdbRequest(imdbCode, true) - - let data = { - imdbCode: imdbCode, - nonSpoilerItems: nonSpoilerItems, - spoilerItems: spoilerItems - } - - callback(data); -} - -async function imdbRequest(imdbCode, spoilers = false, pagePointer = null) { - - let triviaItems = []; - - var variables = { - // "after" : "pointer_to_next_page", - "const": imdbCode, - "filter":{ - // "categories":["uncategorized"] to limit to categories - // "spoilers":"EXCLUDE_SPOILERS" or "SPOILERS_ONLY" - }, - "first":50, - "isAutoTranslationEnabled": false, - "locale":"en-US", - "originalTitleText":false - } - - if (spoilers) { - variables.filter.spoilers = "SPOILERS_ONLY"; - } else { - variables.filter.spoilers = "EXCLUDE_SPOILERS"; - } - - if (pagePointer) { - variables.after = pagePointer; - } - - const extensions = { - "persistedQuery":{ - "sha256Hash": "101651d4fef546dfc31d45f2bf864f588bf4951c8e4720de555d1aa2d9050df2", - "version":1 - } - } - - let url = 'https://caching.graphql.imdb.com/?operationName=TitleTriviaPagination'; - - url += '&variables=' + encodeURIComponent(JSON.stringify(variables)); - url += '&extensions=' + encodeURIComponent(JSON.stringify(extensions)); - - const response = await fetch(url, { - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/graphql+json, application/json' - } - }); - - - if (response.ok) { - const data = await response.json(); - - if (typeof data.data !== "undefined") { - triviaItems.push(...data.data.title.trivia.edges); - - if (data.data.title.trivia.pageInfo.hasNextPage) { - let nextPage = await imdbRequest(imdbCode, spoilers, data.data.title.trivia.pageInfo.endCursor); - triviaItems.push(...nextPage); - } - } - - } - - return triviaItems; -} - browser.runtime.onMessage.addListener(function(message) { switch (message.action) { case "openOptionsPage": diff --git a/src/shared/js/content.js b/src/shared/js/content.js index e503b30..79b1026 100644 --- a/src/shared/js/content.js +++ b/src/shared/js/content.js @@ -22,10 +22,7 @@ function init() { var arr = rx.exec(imdbLink); imdbCode = arr[1]; if (typeof imdbCode !== 'undefined') { - insertIframe(imdbCode); - - //browser.runtime.sendMessage({imdbCode: imdbCode}, gotIMDBData); } } } @@ -56,77 +53,6 @@ window.addEventListener('message', function(event) { }); -function gotIMDBData(data) { - - if (data.nonSpoilerItems.length > 0 || data.spoilerItems.length > 0) { - var triviaCategories = getTriviaCategoriesFromData(data); - if (triviaCategories.length > 0) { - insertTriviaCategories(triviaCategories); - } - } else { - insertFallback(data.imdbCode); - } -} - - -/** - * Get array of trivia html, grouped by category, with spoilers and non-spoilers - * [ - * { - * "category": "Uncategorized", - * "spoilerItems": [html, html, html, ...] - * "nonSpoilerItems": [html, html, html, ...] - * }, - * { - * "category": "Cameo", - * "spoilerItems": [html, html, html, ...] - * "nonSpoilerItems": [html, html, html, ...] - * }, - * ... - * ] - * @param {string} imdbCode - * @param {function} done - */ -function getTriviaCategoriesFromData(data) { - let categories = []; - - processItems(data.nonSpoilerItems, 'nonSpoilerItems', categories); - processItems(data.spoilerItems, 'spoilerItems', categories); - - return categories; -} - -/** - * Extract trivia and categories from data - * @param items - * @param itemType - * @param categories - */ -function processItems(items, itemType, categories) { - items.forEach(function(item) { - let category = item.node.category.text; - - let triviaItem = { - 'html' : item.node.displayableArticle.body.plaidHtml, - 'usersVoted': item.node.interestScore.usersVoted, - 'usersInterested': item.node.interestScore.usersInterested - } - - let existingCategory = categories.find(cat => cat.category === category); - if (existingCategory) { - if (!Array.isArray(existingCategory[itemType])) { - existingCategory[itemType] = []; - } - existingCategory[itemType].push(triviaItem); - } else { - categories.push({ - 'category': category, - [itemType]: [triviaItem] - }); - } - }); -} - // thx to stackbykumbi https://stackoverflow.com/a/69361046/1191375 function nullthrows(v) { if (v == null) throw new Error("it's a null");