Skip to content

Commit

Permalink
Fix broadcasts on homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
aopell committed Apr 17, 2024
1 parent 69a7c2a commit eb93392
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 86 deletions.
6 changes: 3 additions & 3 deletions src/scripts/pages/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function load() {
loadBetaFeatures();
updateDefaultDomainSetting();
pageModifications();
handleNewVersion();
await handleNewVersion();
loadCourseIcons();
activateEasterEgg();
addDarkThemeToggleButton();
Expand Down Expand Up @@ -304,8 +304,8 @@ async function handleNewVersion() {
],
} as IziToastSettings);

await versionSpecificFirstLaunch(currentVersion, newVersion);
Setting.setValue("newVersion", chrome.runtime.getManifest().version);
versionSpecificFirstLaunch(currentVersion, newVersion);
await Setting.setValue("newVersion", chrome.runtime.getManifest().version);
}
}

Expand Down
92 changes: 38 additions & 54 deletions src/scripts/pages/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { trackEvent } from "../utils/analytics";
import { fetchApiJson, getUserId } from "../utils/api";
import { Broadcast } from "../utils/broadcast";
import { EXTENSION_NAME, EXTENSION_WEBSITE } from "../utils/constants";
import { createButton, createElement, createSvgLogo } from "../utils/dom";
import { createButton, createElement, createSvgLogo, waitForElement } from "../utils/dom";
import { Logger } from "../utils/logger";
import Modal from "../utils/modal";
import { SIDEBAR_SECTIONS, SIDEBAR_SECTIONS_MAP, Setting } from "../utils/settings";
Expand All @@ -30,67 +30,51 @@ function loadBroadcasts() {

if (homeFeedContainer && Setting.getValue("broadcasts") !== "disabled") {
(async function () {
let observer = new MutationObserver(async function (mutations) {
if (mutations.length == 0) {
return;
}
await waitForElement("#home-feed-container #edge-filters");

// we Should only be observing changes to style on homeFeedContainer
// 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
// we Should only be observing changes to style on homeFeedContainer
// 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: Broadcast[] = Setting.getValue("unreadBroadcasts") || [];
let onlineBroadcasts: Broadcast[] = [];
let unreadBroadcasts: Broadcast[] = Setting.getValue("unreadBroadcasts") || [];
let onlineBroadcasts: Broadcast[] = [];

try {
onlineBroadcasts = await (
await fetch(`${EXTENSION_WEBSITE}/alert.json`)
).json();

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

onlineBroadcasts = onlineBroadcasts.filter(
b =>
!parsedReadBroadcasts.includes(b.id) &&
!unreadBroadcasts.map(u => u.id).includes(b.id)
);
try {
onlineBroadcasts = await (await fetch(`${EXTENSION_WEBSITE}/alert.json`)).json();

for (let onlineBroadcast of onlineBroadcasts) {
onlineBroadcast.title = DOMPurify.sanitize(onlineBroadcast.title);
onlineBroadcast.message = DOMPurify.sanitize(onlineBroadcast.message);
}
} catch (err) {
// Ignore
}
let readBroadcasts = localStorage.getItem("splus-readBroadcasts");
let parsedReadBroadcasts: string[] =
readBroadcasts === null ? [] : JSON.parse(readBroadcasts);

let unexpiredBroadcasts: Broadcast[] = [];
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);
}
}
onlineBroadcasts = onlineBroadcasts.filter(
b =>
!parsedReadBroadcasts.includes(b.id) &&
!unreadBroadcasts.map(u => u.id).includes(b.id)
);

// remove expired broadcasts
Setting.setValue("unreadBroadcasts", unexpiredBroadcasts);
for (let onlineBroadcast of onlineBroadcasts) {
onlineBroadcast.title = DOMPurify.sanitize(onlineBroadcast.title);
onlineBroadcast.message = DOMPurify.sanitize(onlineBroadcast.message);
}
} catch (err) {
// Ignore
}

// then disconnect
observer.disconnect();
});
let unexpiredBroadcasts: Broadcast[] = [];
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);
}
}

observer.observe(homeFeedContainer, {
attributes: true,
attributeFilter: ["style"],
});
// remove expired broadcasts
Setting.setValue("unreadBroadcasts", unexpiredBroadcasts);
})();
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/scripts/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,15 @@ export function createSvgLogo(...classes: string[]) {

return svg;
}

export function waitForElement(selector: string, check_interval = 50): Promise<HTMLElement> {
return new Promise(resolve => {
let interval = setInterval(() => {
let elem = document.querySelector(selector);
if (elem) {
clearInterval(interval);
resolve(elem as HTMLElement);
}
}, check_interval);
});
}
58 changes: 29 additions & 29 deletions src/scripts/utils/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,48 +63,48 @@ const migrationsTo: {
`${EXTENSION_NAME} has a Discord server where you can offer feature suggestions, report bugs, get support, or just talk with other ${EXTENSION_NAME} users. <a href="${DISCORD_URL}" id="announcement-discord-link" class="splus-track-clicks">Click here</a> to join!`,
new Date(2019, 1 /* February - don't you just love JavaScript */, 14)
),
]);
]).then(() => {
if (getBrowser() !== "Firefox") {
let analyticsModalExistsInterval = setInterval(function () {
if (
document.readyState === "complete" &&
document.getElementById("analytics-modal") &&
!document.querySelector(".splus-modal-open")
) {
clearInterval(analyticsModalExistsInterval);
Modal.openModal("analytics-modal");
}
}, 50);
}

if (getBrowser() !== "Firefox") {
let analyticsModalExistsInterval = setInterval(function () {
let chooseThemeModalExistsInterval = setInterval(function () {
if (
document.readyState === "complete" &&
document.getElementById("analytics-modal") &&
document.getElementById("choose-theme-modal") &&
!document.querySelector(".splus-modal-open")
) {
clearInterval(analyticsModalExistsInterval);
Modal.openModal("analytics-modal");
clearInterval(chooseThemeModalExistsInterval);
Modal.openModal("choose-theme-modal");
}
}, 50);
}

let chooseThemeModalExistsInterval = setInterval(function () {
if (
document.readyState === "complete" &&
document.getElementById("choose-theme-modal") &&
!document.querySelector(".splus-modal-open")
) {
clearInterval(chooseThemeModalExistsInterval);
Modal.openModal("choose-theme-modal");
}
}, 50);

var accessToAccountInterval = setInterval(function () {
if (
document.readyState === "complete" &&
!document.querySelector(".splus-modal-open")
) {
clearInterval(accessToAccountInterval);
if (!Setting.getValue("apistatus")) {
location.pathname = "/api";
var accessToAccountInterval = setInterval(function () {
if (
document.readyState === "complete" &&
!document.querySelector(".splus-modal-open")
) {
clearInterval(accessToAccountInterval);
if (!Setting.getValue("apistatus")) {
location.pathname = "/api";
}
}
}
}, 50);
}, 50);
});
},
8.0: function (currentVersion, previousVersion) {},
};

export async function versionSpecificFirstLaunch(currentVersion: string, previousVersion?: string) {
export function versionSpecificFirstLaunch(currentVersion: string, previousVersion?: string) {
Logger.log(
"[Updater] First launch after update, updating to ",
currentVersion,
Expand Down

0 comments on commit eb93392

Please sign in to comment.