Skip to content

Commit 4d9e41e

Browse files
committed
Added auto fetching and last fetched time.
1 parent e3b7b6b commit 4d9e41e

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

background.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,50 @@ function applyCSSToTab(tab) {
4141
});
4242
}
4343

44+
let autoUpdateInterval;
45+
46+
function startAutoUpdate() {
47+
if (autoUpdateInterval) clearInterval(autoUpdateInterval);
48+
autoUpdateInterval = setInterval(refetchCSS, 2 * 60 * 60 * 1000);
49+
}
50+
51+
function stopAutoUpdate() {
52+
if (autoUpdateInterval) clearInterval(autoUpdateInterval);
53+
}
54+
55+
async function refetchCSS() {
56+
try {
57+
const response = await fetch(
58+
"https://sameerasw.github.io/my-internet/styles.json",
59+
{
60+
headers: { "Cache-Control": "no-cache" },
61+
}
62+
);
63+
if (!response.ok) throw new Error("Failed to fetch styles.json");
64+
const styles = await response.json();
65+
await browser.storage.local.set({ styles });
66+
await browser.storage.local.set({ lastFetchedTime: Date.now() });
67+
console.info("All styles refetched and updated from GitHub.");
68+
} catch (error) {
69+
console.error("Error refetching styles:", error);
70+
}
71+
}
72+
73+
browser.runtime.onMessage.addListener((message) => {
74+
if (message.action === "enableAutoUpdate") {
75+
startAutoUpdate();
76+
} else if (message.action === "disableAutoUpdate") {
77+
stopAutoUpdate();
78+
}
79+
});
80+
81+
// Initialize auto-update based on stored settings
82+
browser.storage.local.get("transparentZenSettings").then((settings) => {
83+
if (settings.transparentZenSettings?.autoUpdate) {
84+
startAutoUpdate();
85+
}
86+
});
87+
4488
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
4589
if (changeInfo.status === "complete") {
4690
applyCSSToTab(tab);

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "Zen Internet",
4-
"version": "1.1.3",
4+
"version": "1.2.0",
55
"description": "Inject custom css from my repository in real time",
66
"browser_specific_settings": {
77
"gecko": {

popup/popup.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,6 @@ input:checked + .slider:before {
404404
margin-right: 4px;
405405
}
406406

407-
#addon-version{
407+
#addon-version, #last-fetched-time{
408408
font-size: 0.75em;
409409
}

popup/popup.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,18 @@ <h2 class="section-title">Current Site Features</h2>
4949
<i class="fas fa-sync-alt"></i> Refetch latest styles
5050
</button>
5151
</div>
52+
53+
<div class="toggle-container">
54+
<label class="toggle-switch">
55+
<input type="checkbox" id="auto-update">
56+
<span class="slider round"></span>
57+
</label>
58+
<span class="toggle-label">Auto Update Styles</span>
59+
</div>
60+
<div id="last-fetched-time" class="last-fetched-time"></div>
61+
5262
</main>
63+
5364

5465
<footer class="app-footer">
5566
<a href="https://sameerasw.github.io/my-internet/" class="footer-link" target="_blank">

popup/popup.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ new (class ExtensionPopup {
66
websitesList = document.getElementById("websites-list");
77
currentSiteFeatures = document.getElementById("current-site-toggles");
88
currentSiteHostname = "";
9+
autoUpdateSwitch = document.getElementById("auto-update");
10+
lastFetchedTime = document.getElementById("last-fetched-time");
911

1012
constructor() {
1113
this.loadSettings().then((settings) => {
@@ -22,6 +24,12 @@ new (class ExtensionPopup {
2224
this.websitesList.classList.toggle("collapsed");
2325
});
2426

27+
this.autoUpdateSwitch.addEventListener(
28+
"change",
29+
this.saveSettings.bind(this)
30+
);
31+
this.setupAutoUpdate();
32+
this.displayLastFetchedTime();
2533
this.setupContentScriptInjection();
2634
this.displayAddonVersion();
2735
}
@@ -61,6 +69,9 @@ new (class ExtensionPopup {
6169
this.enableStylingSwitch.checked =
6270
this.browserStorageSettings.enableStyling;
6371
}
72+
if (this.browserStorageSettings.autoUpdate !== undefined) {
73+
this.autoUpdateSwitch.checked = this.browserStorageSettings.autoUpdate;
74+
}
6475
this.loadCurrentSiteFeatures();
6576
this.loadWebsitesList();
6677
}
@@ -74,6 +85,7 @@ new (class ExtensionPopup {
7485
saveSettings() {
7586
this.browserStorageSettings.enableStyling =
7687
this.enableStylingSwitch.checked;
88+
this.browserStorageSettings.autoUpdate = this.autoUpdateSwitch.checked;
7789

7890
const featureSettings = {};
7991
this.currentSiteFeatures
@@ -191,6 +203,7 @@ new (class ExtensionPopup {
191203
if (!response.ok) throw new Error("Failed to fetch styles.json");
192204
const styles = await response.json();
193205
await browser.storage.local.set({ styles });
206+
await browser.storage.local.set({ lastFetchedTime: Date.now() });
194207

195208
this.loadCurrentSiteFeatures();
196209
this.loadWebsitesList();
@@ -201,6 +214,7 @@ new (class ExtensionPopup {
201214
this.refetchCSSButton.textContent = "Refetch latest styles";
202215
}, 2000);
203216
console.info("All styles refetched and updated from GitHub." + styles);
217+
this.displayLastFetchedTime();
204218
} catch (error) {
205219
this.refetchCSSButton.textContent = "Error!";
206220
setTimeout(() => {
@@ -294,4 +308,22 @@ new (class ExtensionPopup {
294308
"addon-version"
295309
).textContent = `Version: ${version}`;
296310
}
311+
312+
setupAutoUpdate() {
313+
if (this.autoUpdateSwitch.checked) {
314+
browser.runtime.sendMessage({ action: "enableAutoUpdate" });
315+
} else {
316+
browser.runtime.sendMessage({ action: "disableAutoUpdate" });
317+
}
318+
}
319+
320+
displayLastFetchedTime() {
321+
browser.storage.local.get("lastFetchedTime").then((result) => {
322+
if (result.lastFetchedTime) {
323+
this.lastFetchedTime.textContent = `Last fetched: ${new Date(
324+
result.lastFetchedTime
325+
).toLocaleString()}`;
326+
}
327+
});
328+
}
297329
})();

0 commit comments

Comments
 (0)