-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
122 lines (106 loc) · 3.57 KB
/
background.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
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
const CHECK_INTERVAL = 5; // Interval in minutes
let monitoredURL = ""; // Default RSS feed URL
let lastChecked = 0; // Last time the feed was checked
// Function to fetch RSS feed and check for build errors
async function checkRSSFeed() {
if (!monitoredURL) return;
console.log("Checking RSS feed...");
try {
const response = await fetch(monitoredURL);
const text = await response.text();
console.log("Fetched RSS feed:");
// Parse the RSS feed as text
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, "application/xml");
// Extract all item titles and descriptions
const items = xmlDoc.querySelectorAll("item");
for (const item of items) {
const dateString = item.querySelector("pubDate")?.textContent || "";
const pubDate = Date.parse(dateString);
if (lastChecked && pubDate <= lastChecked) {
continue;
}
const title = item.querySelector("title")?.textContent || "";
const guid = item.querySelector("guid")?.textContent || "";
const author = item.querySelector("author")?.textContent || "";
const link = item.querySelector("link")?.textContent || "";
if (title.endsWith("(failed)")) {
console.log("Found failed build at:", link);
const description = `Author: ${author}\nLink: ${link}`;
showNotification(title, description);
// markBuildAsRead(guid);
break;
}
}
} catch (error) {
console.error("Error fetching RSS feed:", error);
}
lastChecked = Date.now();
}
// Display a notification
function showNotification(title, description) {
console.log("Showing notification:", title, description);
// new Notification(title, {body: description});
browser.notifications.create({
type: "basic",
iconUrl: "icon-48px.png",
title: "Build error on Sourcehut",
message: `${title}\n${description}`
});
}
// // Mark a build as read
// function markBuildAsRead(guid) {
// // Save the GUID to storage
// browser.storage.local.get(["readBuilds"], (data) => {
// const readBuilds = data.readBuilds || {};
// readBuilds[guid] = true;
//
// browser.storage.local.set({readBuilds});
// console.log("Duilds marked as read:", readBuilds);
// });
// }
// Load settings from storage
function loadSettings() {
console.log("Loading settings...");
browser.storage.local.get(["userId"], (data) => {
if (data.userId) {
// Example: https://builds.sr.ht/~sfermigier/rss.xml
monitoredURL = `https://builds.sr.ht/~${data.userId}/rss.xml`;
} else {
monitoredURL = "";
}
});
checkRSSFeed().then(
() => console.log("Initial RSS feed check complete"),
(error) => console.error("Error checking RSS feed:", error)
)
}
// // Media Query Listener for viewport size changes
// function setupViewportListener() {
// const mediaQuery = window.matchMedia("(max-width: 600px)");
//
// function handleViewportChange(event) {
// if (event.matches) {
// console.log("Viewport is 600px or smaller");
// } else {
// console.log("Viewport is larger than 600px");
// }
// }
//
// if (mediaQuery.addEventListener) {
// // Modern browsers
// mediaQuery.addEventListener("change", handleViewportChange);
// }
// }
// Set up periodic alarms
browser.alarms.create("checkFeed", {periodInMinutes: CHECK_INTERVAL});
browser.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "checkFeed") {
checkRSSFeed();
}
});
// Listen for storage changes
browser.storage.onChanged.addListener(loadSettings);
// Initial load
loadSettings();
// setupViewportListener();