Skip to content

Commit

Permalink
Update broadcasts and add version filter
Browse files Browse the repository at this point in the history
  • Loading branch information
aopell committed Apr 7, 2023
1 parent 420d068 commit 39e99be
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions js/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,7 @@ function formatDateAsString(date) {

if (homeFeedContainer && Setting.getValue("broadcasts") !== "disabled") {
(async function () {
try {
let onlineBroadcasts = await (await fetch("https://schoologypl.us/alert.json")).json();

let readBroadcasts = localStorage.getItem("splus-readBroadcasts");
readBroadcasts = readBroadcasts === null ? [] : JSON.parse(readBroadcasts);

saveBroadcasts(onlineBroadcasts.filter(b => !readBroadcasts.includes(b.id)));
} catch (err) {
// Ignore
}

let observer = new MutationObserver(function (mutations) {
let observer = new MutationObserver(async function (mutations) {
if (mutations.length == 0) {
return;
}
Expand All @@ -121,10 +110,26 @@ if (homeFeedContainer && Setting.getValue("broadcasts") !== "disabled") {
// style is set on homeFeedContainer whenever Schoology decides to unhide it (static CSS sets display: none), i.e. when it's finished loading
// once this happens, we can do our thing

let unreadBroadcasts = Setting.getValue("unreadBroadcasts");
let unreadBroadcasts = Setting.getValue("unreadBroadcasts") || [];
let onlineBroadcasts = [];

try {
onlineBroadcasts = await (await fetch("https://schoologypl.us/alert.json")).json();

let readBroadcasts = localStorage.getItem("splus-readBroadcasts");
readBroadcasts = readBroadcasts === null ? [] : JSON.parse(readBroadcasts);

onlineBroadcasts = onlineBroadcasts.filter(b => !readBroadcasts.includes(b.id) && !unreadBroadcasts.map(u => u.id).includes(b.id));
} catch (err) {
// Ignore
}

let unexpiredBroadcasts = [];
for (let broadcast of unreadBroadcasts || []) {
if (!broadcast.expires || broadcast.expires > Date.now()) {
for (let broadcast of [...unreadBroadcasts, ...onlineBroadcasts]) {
if (
(!broadcast.expires || broadcast.expires > Date.now())
&& (!broadcast.version || compareVersions(chrome.runtime.getManifest().version, broadcast.version) >= 0)
) {
feed.insertAdjacentElement("afterbegin", postFromBroadcast(broadcast));
unexpiredBroadcasts.push(broadcast);
}
Expand Down

0 comments on commit 39e99be

Please sign in to comment.