Skip to content

Commit

Permalink
In extension, intercept ratings requests, and submit data to API (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaldengeki authored Aug 25, 2024
1 parent 05fdacb commit c3635af
Showing 1 changed file with 73 additions and 6 deletions.
79 changes: 73 additions & 6 deletions ark_nova_stats/extension/detectivekookaburra.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
let pattern = "https://boardgamearena.com/*";
let apiEndpoint = "https://api.arknova.ouguo.us/graphql";

function handleRequest(requestDetails) {
if (!requestDetails.url.includes("logs.html")) {
return;
}

function handleLogsRequest(requestDetails) {
let filter = browser.webRequest.filterResponseData(requestDetails.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
Expand Down Expand Up @@ -55,7 +51,7 @@ function handleRequest(requestDetails) {
});
let apiPromise = fetch(apiRequest).then(
data => {
console.log(`Made API request: ${data.json()}`)
console.log(`Made game logs API request: ${data.json()}`)
}
)

Expand All @@ -68,6 +64,77 @@ function handleRequest(requestDetails) {
return {};
}

function handleRatingsRequest(requestDetails) {
const url = new URL(requestDetails.url);
const tableId = parseInt(url.searchParams.get("id"), 10);

if (tableId === null) {
console.log("No table ID found in URL, bailing.");
return {}
}

let filter = browser.webRequest.filterResponseData(requestDetails.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();

const data = [];

filter.ondata = (event) => {
data.push(event.data);
}

filter.onstop = (event) => {
let str = "";

// Decode all the pushed data and assemble a string representing the entire response.
if (data.length === 1) {
str = decoder.decode(data[0]);
} else {
for (let i = 0; i < data.length; i++) {
const stream = i !== data.length - 1;
const decodedChunk = decoder.decode(data[i], { stream });
str += decodedChunk;
}
}

let apiHeaders = new Headers();
apiHeaders.append("Content-Type", "application/json");

let apiRequest = new Request(apiEndpoint, {
method: "POST",
headers: apiHeaders,
body: JSON.stringify({
query: "mutation SubmitGameRatings($ratings:String!,$tableId:Int!) {\n submitGameRatings(ratings:$ratings,tableId:$tableId) {\n id\n }\n}",
variables: {
ratings: str,
tableId: tableId,
},
operationName: "SubmitGameRatings",
})
});
let apiPromise = fetch(apiRequest).then(
data => {
console.log(`Made game ratings API request: ${data.json()}`)
}
)

filter.write(encoder.encode(str));
filter.disconnect();

return apiPromise;
}

return {};
}

function handleRequest(requestDetails) {
if (requestDetails.url.includes("logs.html")) {
return handleLogsRequest(requestDetails);
} else if (requestDetails.url.includes("tableratingsupdate.html")) {
return handleRatingsRequest(requestDetails);
}
}

browser.webRequest.onBeforeRequest.addListener(
handleRequest,
{ urls: [pattern] },
Expand Down

0 comments on commit c3635af

Please sign in to comment.