-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
146 lines (125 loc) · 4.05 KB
/
Copy pathbackground.js
File metadata and controls
146 lines (125 loc) · 4.05 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Function to fetch games from GG Deals
async function fetchGames() {
try {
const response = await fetch(
"https://gg.deals/news/feed/?availability=1&type=6&utm_source=eshan"
);
const text = await response.text();
console.log("Fetched HTML content:", text);
// Find the first <item></item> tag
const itemMatch = text.match(/<item>([\s\S]*?)<\/item>/);
let title = "No title found";
let date = "No date found";
if (itemMatch) {
const item = itemMatch[0];
//console.log("First Item:", item);
// Extract title and pubDate
const titleMatch = item.match(/<title>(.*?)<\/title>/);
const pubDateMatch = item.match(/<pubDate>(.*?)<\/pubDate>/);
if (titleMatch) {
title = titleMatch[1];
}
if (pubDateMatch) {
date = pubDateMatch[1];
}
//console.log("First Item:");
//console.log(`Title: ${title}`);
//console.log(`Date: ${date}`);
} else {
//console.log("No <item></item> tags found.");
}
// const parser = new DOMParser();
// const doc = parser.parseFromString(text, "text/html");
let newGames = [{ title, date }];
//console.log("New Games:", newGames);
// doc.querySelectorAll("article").forEach((article) => {
// if (article.classList.contains("active")) {
// const link = article.querySelector("a.full-link");
// const image = article.querySelector("img");
// if (link && image) {
// newGames.push({
// headline: link.textContent.trim(),
// url: "https://gg.deals" + link.getAttribute("href"),
// imageUrl: image.src,
// });
// }
// }
// });
return newGames;
} catch (error) {
console.error("Error fetching games:", error);
return [];
}
}
// Function to set badge with red dot
function setBadge() {
//console.log("setting badge2");
chrome.action.setBadgeText({ text: "•" });
chrome.action.setBadgeBackgroundColor({ color: "#FF0000" });
}
// Function to clear the badge
function clearBadge() {
chrome.action.setBadgeText({ text: "" });
}
// Function to check for new games and update storage
async function checkForNewGames(flag) {
//console.log("Checking for new games...");
//console.log("flag", flag);
// if (flag == 0) {
// await setBadge();
// return;
// } else if (flag == 1) {
// console.log("setting badge");
// await setBadge();
// console.log("setting badge done");
// return;
// }
// console.log("flag", flag);
// if (flag === 0 || flag === 1) {
// console.log("Setting badge due to flag:", flag);
// //clearBadge();
// setBadge();
// return;
// }
const newGames = await fetchGames();
if (newGames.length === 0) return;
const latestGame = newGames[0];
//console.log("latestGame", latestGame);
chrome.storage.local.get("lastGame", (result) => {
const storedGame = result.lastGame;
//console.log("storedGame", storedGame);
//console.log("checking if game is present");
if (!storedGame || storedGame.title !== latestGame.title) {
//console.log("setting badge");
setBadge();
chrome.storage.local.set({ lastGame: latestGame });
} else {
//console.log("game already present");
}
});
}
// Setup alarm to check every 2 hours
chrome.alarms.create("checkForNewGames", { periodInMinutes: 120 });
// Listen for alarm
chrome.alarms.onAlarm.addListener((alarm) => {
//console.log("Alarm triggered:", alarm);
if (alarm.name === "checkForNewGames") {
checkForNewGames(1);
}
});
// Initial check when extension is installed or reloaded
chrome.runtime.onInstalled.addListener(() => {
checkForNewGames(1);
//clearBadge(); // Clear badge when the extension is first installed/reloaded
});
// Check for new games on Chrome startup
chrome.runtime.onStartup.addListener(() => {
checkForNewGames(0);
});
// Listener for action icon click to execute content script
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["content.js"],
});
});