Skip to content

Commit

Permalink
remove graphql requests
Browse files Browse the repository at this point in the history
  • Loading branch information
combatwombat committed Sep 10, 2023
1 parent bee2109 commit d21b5be
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 498 deletions.
Binary file modified dist/chrome/lb-imdb.zip
Binary file not shown.
94 changes: 2 additions & 92 deletions dist/chrome/src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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":
Expand Down
74 changes: 0 additions & 74 deletions dist/chrome/src/js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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");
Expand Down
Binary file modified dist/firefox/lb-imdb.zip
Binary file not shown.
94 changes: 2 additions & 92 deletions dist/firefox/src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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":
Expand Down
74 changes: 0 additions & 74 deletions dist/firefox/src/js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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");
Expand Down
Loading

0 comments on commit d21b5be

Please sign in to comment.