-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
71 lines (62 loc) · 2.48 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
document.getElementById("scrape").addEventListener("click", async () => {
const output = document.getElementById("output");
output.innerHTML = "<p>Loading...</p>";
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab) {
output.innerHTML = "<p>No active tab detected.</p>";
return;
}
const titleResponse = await new Promise((resolve) => {
chrome.tabs.sendMessage(tab.id, { action: "getTitle" }, (response) => {
if (chrome.runtime.lastError) {
resolve({ title: null });
} else {
resolve(response);
}
});
});
if (!titleResponse?.title) {
output.innerHTML = "<p>No game title found on this page.</p>";
return;
}
console.log("Found game title:", titleResponse.title);
const response = await fetchGameRatings(titleResponse.title);
if (response.success) {
const { data: ratings } = response;
const userRating = ratings.rating ? `${ratings.rating.toFixed(1)}/100` : "Not Available";
const criticRating = ratings.aggregated_rating ?
`${ratings.aggregated_rating.toFixed(1)}/100` : "Not Available";
output.innerHTML = `
<h2>${titleResponse.title}</h2>
<p><strong>User Rating:</strong> ${userRating}</p>
<p><strong>Critic Rating:</strong> ${criticRating}</p>`;
} else {
output.innerHTML = `<p>Error: ${response.error || "Unknown error occurred."}</p>`;
}
} catch (error) {
console.error('Error:', error);
output.innerHTML = `<p>Error: ${error.message}</p>`;
}
});
async function fetchGameRatings(gameTitle) {
const newProxyUrl = "https://insertyourworkersurlhere.workers.dev/";
return new Promise((resolve) => {
chrome.runtime.sendMessage(
{
action: "fetchGameRatings",
payload: {
proxyUrl: newProxyUrl,
query: `search "${gameTitle}"; fields name,aggregated_rating,rating;`
},
},
(response) => {
if (chrome.runtime.lastError) {
resolve({ success: false, error: chrome.runtime.lastError.message });
} else {
resolve(response);
}
}
);
});
}