Skip to content

Commit

Permalink
add refresh interval
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanfrans committed Apr 12, 2024
1 parent fa85682 commit b3ddc82
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hardbulls/baseball-ticker-cxt",
"description": "Baseball Ticker - Chrome Extension",
"version": "1.0.0",
"version": "1.0.1",
"private": true,
"author": {
"name": "Arjan Frans",
Expand Down
48 changes: 46 additions & 2 deletions src/cxt/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { Local } from "./storage/Local";
import { parseValues } from "./content/parse-values";
import { DEFAULT_OPTIONS_STATE, DEFAULT_POPUP_STATE, DEFAULT_SCOREBOARD_STATE } from "../lib/state/DefaultState";

function IndicatorElement(): HTMLDivElement & { on: () => void; off: () => void } {
const indicator = document.createElement("div") as HTMLDivElement & { on: () => void; off: () => void };
interface IndicatorElementOptions {
on: () => void;
off: () => void;
hasStarted: (hasStarted: boolean) => void;
}

function IndicatorElement(): HTMLDivElement & IndicatorElementOptions {
const indicator = document.createElement("div") as HTMLDivElement & IndicatorElementOptions;

indicator.style.width = "200px";
indicator.style.pointerEvents = "none";
Expand All @@ -23,6 +29,14 @@ function IndicatorElement(): HTMLDivElement & { on: () => void; off: () => void
indicator.textContent = "Following Ticker: OFF";
};

indicator["hasStarted"] = (hasStarted: boolean) => {
if (hasStarted) {
indicator.textContent = "Following Ticker: OFF";
} else {
indicator.textContent = "No Ticker detected";
}
};

indicator.off();

return indicator;
Expand All @@ -44,8 +58,11 @@ function IndicatorElement(): HTMLDivElement & { on: () => void; off: () => void
...(await Local.getScoreboard()),
};
let followInterval: number | undefined;
let refreshInterval: number | undefined;

const bodyElement = document.querySelector("body") as HTMLBodyElement;
const indicatorElement = IndicatorElement();
let startedIndicator = document.querySelector(".box-score-top-bar");

bodyElement.appendChild(indicatorElement);

Expand All @@ -59,6 +76,19 @@ function IndicatorElement(): HTMLDivElement & { on: () => void; off: () => void
}, OPTIONS_STATE.tickerInterval);
}

function refreshCheck() {
return setInterval(async () => {
startedIndicator = document.querySelector(".box-score-top-bar");

if (startedIndicator && refreshInterval) {
indicatorElement.hasStarted(true);
clearInterval(refreshInterval);
} else {
window.location.reload();
}
}, OPTIONS_STATE.refreshInterval);
}

chrome.storage.onChanged.addListener((changes) => {
if (changes.popup) {
if (changes.popup.newValue.followTicker !== undefined) {
Expand All @@ -69,12 +99,21 @@ function IndicatorElement(): HTMLDivElement & { on: () => void; off: () => void

if (!followTicker) {
indicatorElement.off();

if (refreshInterval) {
clearInterval(refreshInterval);
}
}
}

if (followTicker) {
followInterval = follow();
indicatorElement.on();

if (!startedIndicator) {
indicatorElement.hasStarted(false);
refreshInterval = refreshCheck();
}
}
}
}
Expand All @@ -89,5 +128,10 @@ function IndicatorElement(): HTMLDivElement & { on: () => void; off: () => void
if (INITIAL_STATE.followTicker) {
followInterval = follow();
indicatorElement.on();

if (!startedIndicator) {
indicatorElement.hasStarted(false);
refreshInterval = refreshCheck();
}
}
})();
3 changes: 2 additions & 1 deletion src/lib/state/DefaultState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export const DEFAULT_OPTIONS_STATE: OptionsState = {
leagueLogoShadow: "#888888",
league: undefined,
style: DEFAULT_STYLE_STATE,
tickerInterval: 1000,
tickerInterval: 1000, // 1 second
refreshInterval: 60000, // 1 minute
remote: {
firebaseConfig: undefined,
username: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/lib/state/OptionsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export type OptionsState = {
style: StyleState;
remote: RemoteState;
tickerInterval: number;
refreshInterval: number;
};

0 comments on commit b3ddc82

Please sign in to comment.