Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In extension, intercept ratings requests, and submit data to API #417

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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