Merge pull request #3 from Andrii256/fix_correction

Fix correction
This commit is contained in:
Andrii Hrushetskyi 2025-03-27 21:30:06 +01:00 committed by GitHub
commit c3e806a3e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 101 additions and 79 deletions

View file

@ -1,8 +1,6 @@
# Smart DayNight Switcher
## What does this plugin do?
This plugin calculates daily sunrise and sunset times for your location and automatically switches your Obsidian theme: light mode when the sun rises ☀️ and dark mode when it sets 🌒.
Calculates daily sunrise and sunset times for your location and automatically switches your Obsidian theme: light mode when the sun rises ☀️ and dark mode when it sets 🌒.
No more manual theme switching!

166
main.ts
View file

@ -1,8 +1,7 @@
// Import the SunCalc library to calculate sun position and light phases
import SunCalc from "suncalc"; // Documentation: https://www.npmjs.com/package/suncalc
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";
import { debounce } from "ts-debounce";
import { App, Plugin, PluginSettingTab, Setting, debounce } from "obsidian";
/**
* SDNS: Smart DayNight Switcher
@ -18,8 +17,6 @@ const DEFAULT_SETTINGS: SDNSPluginSettings = {
longitude: "-0.127758",
};
const DYNAMIC_DIV_ID = "a440b9a8-80d9-4b4b-b7a3-0265c8964997"; // unique ID to make sure it will not clash with other plugins
export default class SDNSPlugin extends Plugin {
private timeout: ReturnType<typeof setTimeout> | null;
settings: SDNSPluginSettings;
@ -53,51 +50,6 @@ export default class SDNSPlugin extends Plugin {
async saveSettings() {
await this.saveData(this.settings);
this.checkAndSwitchColorScheme();
this.updateScheduleHTML();
}
updateScheduleHTML() {
const container = document.getElementById(DYNAMIC_DIV_ID);
if (container) {
container[byPassAnnoyingBot("inner")] = this.generateScheduleHTML();
}
}
generateScheduleHTML() {
const date = new Date();
const rows = [];
for (let i = 0; i <= 30; i++) {
const { dawn, sunsetStart: dusk } = SunCalc.getTimes(
date,
+this.settings.latitude,
+this.settings.longitude
);
const dateStr = `${date.getDate()} ${date.toLocaleString("en-US", {
month: "short",
})} ${date.getFullYear()}`; // date 'DD MMM YYYY'
const dawnStr = `${String(dawn.getHours()).padStart(
2,
"0"
)}:${String(dawn.getMinutes()).padStart(2, "0")}`; // dawn 'HH:MM'
const duskStr = `${String(dusk.getHours()).padStart(
2,
"0"
)}:${String(dusk.getMinutes()).padStart(2, "0")}`; // dusk 'HH:MM'
rows.push(
`<tr><td>${dateStr}</td><td>${dawnStr}</td><td>${duskStr}</td></tr>`
);
date.setDate(date.getDate() + 1); // to increase date before next call
}
const html = `<div id="a440b9a8-80d9-4b4b-b7a3-0265c8964997"><h4 style=" border-top: 1px solid var(--background-modifier-border); margin: 1em auto 1em; padding-top: 1em;">Schedule:</h4><table style="width: 100%;text-align: center;"> <tbody> <tr> <th>Date</th> <th>Dawn<br><small>(Light mode will be enabled)</small></th> <th>Dusk<br><small>(Dark mode will be enabled)</small></th> ${rows.join(
""
)}</tbody></table>`;
return html;
}
checkAndSwitchColorScheme() {
@ -165,16 +117,6 @@ export default class SDNSPlugin extends Plugin {
}
}
function byPassAnnoyingBot(inner: string): string {
const h = "h";
const l = "l";
const arr = [inner.toLowerCase(), h.toUpperCase()];
arr.push("T");
arr.push("M");
return arr.join("") + l.toUpperCase();
}
class SDNSPluginSettingTab extends PluginSettingTab {
plugin: SDNSPlugin;
@ -187,11 +129,9 @@ class SDNSPluginSettingTab extends PluginSettingTab {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h3", { text: "Starting Point Coordinates" });
containerEl.createDiv()[
byPassAnnoyingBot("inner")
] = `<p>Please enter the latitude and longitude of your approximate location (coords of any city within ±200 km)</p>`;
containerEl.createEl("p", {
text: "Please enter the latitude and longitude of your approximate location (coords of any city within ±200 km)",
});
new Setting(containerEl)
.setName("Latitude")
@ -227,13 +167,97 @@ class SDNSPluginSettingTab extends PluginSettingTab {
)
);
containerEl.createDiv()[
byPassAnnoyingBot("inner")
] = `<p><small >To easily find your latitude and longitude, you can use any simple online service, such as <a href="https://www.latlong.net/">latlong.net</a>, <a href="https://www.gps-coordinates.net/">gps-coordinates.net</a>, or any other similar tools available on the web.</small ><br/><br /><small >* Obsidian does not provide developers with access to geolocation, so this plugin cannot automatically determine your coordinates.</small > <br /> <small style="display: inline-block; margin-top: 0.4em;" >&nbsp;However, <strong>to accurately calculate sunrise and sunset times in your location</strong>, the formula needs an approximate location (within ±200 km) of where you are.</small ></p>`;
const addAdditionalDescription = () => {
const additionalDescription = containerEl.createEl("p");
const dynamicContent = containerEl.createDiv();
dynamicContent.id = DYNAMIC_DIV_ID;
dynamicContent[byPassAnnoyingBot("inner")] =
this.plugin.generateScheduleHTML();
additionalDescription.createEl("small", {
text: "To easily find your latitude and longitude, you can use any simple online service, such as ",
});
additionalDescription.createEl("small").createEl("a", {
href: "https://www.latlong.net/",
text: "latlong.net",
});
additionalDescription.createEl("small", { text: ", " });
additionalDescription.createEl("small").createEl("a", {
href: "https://www.gps-coordinates.net/",
text: "gps-coordinates.net",
});
additionalDescription.createEl("small", {
text: ", or any other similar tools available on the web.",
});
additionalDescription.createEl("br");
additionalDescription.createEl("br");
additionalDescription.createEl("small", {
text: "* Obsidian does not provide developers with access to geolocation, so this plugin cannot automatically determine your coordinates.",
});
additionalDescription.createEl("br");
additionalDescription.createEl("small", { text: "However, " });
additionalDescription.createEl("small").createEl("strong", {
text: "to accurately calculate sunrise and sunset times in your location",
});
additionalDescription.createEl("small", {
text: ", the formula needs an approximate location (within ±200 km) of where you are.",
});
};
addAdditionalDescription();
const addSchedule = () => {
const dynamicContent = containerEl.createEl("div");
const scheduleHeader = dynamicContent.createEl("h4", {
text: "Schedule:",
});
scheduleHeader.setCssStyles({
borderTop: "1px solid var(--background-modifier-border)",
margin: "1em auto 1em",
paddingTop: "1em",
});
const table = dynamicContent.createEl("table", {
attr: { style: "width: 100%; text-align: center;" },
});
const tbody = table.createEl("tbody");
const headerRow = tbody.createEl("tr");
headerRow.createEl("th", { text: "Date" });
const dawnTh = headerRow.createEl("th");
dawnTh.createEl("span", { text: "Dawn" });
dawnTh.createEl("br");
dawnTh.createEl("small", { text: "(Light mode will be enabled)" });
const duskTh = headerRow.createEl("th");
duskTh.createEl("span", { text: "Dusk" });
duskTh.createEl("br");
duskTh.createEl("small", { text: "(Dark mode will be enabled)" });
const date = new Date();
for (let i = 0; i <= 30; i++) {
const { dawn, sunsetStart: dusk } = SunCalc.getTimes(
date,
+this.plugin.settings.latitude,
+this.plugin.settings.longitude
);
const dateStr = `${date.getDate()} ${date.toLocaleString(
"en-US",
{
month: "short",
}
)} ${date.getFullYear()}`;
const dawnStr = `${String(dawn.getHours()).padStart(
2,
"0"
)}:${String(dawn.getMinutes()).padStart(2, "0")}`;
const duskStr = `${String(dusk.getHours()).padStart(
2,
"0"
)}:${String(dusk.getMinutes()).padStart(2, "0")}`;
const row = tbody.createEl("tr");
row.createEl("td", { text: dateStr });
row.createEl("td", { text: dawnStr });
row.createEl("td", { text: duskStr });
date.setDate(date.getDate() + 1);
}
};
addSchedule();
}
}

View file

@ -1,11 +1,11 @@
{
"id": "smart-day-night-switcher",
"name": "Smart DayNight switcher",
"version": "1.0.2",
"version": "1.0.3",
"minAppVersion": "1.8.9",
"description": "This plugin intelligently determines sunrise and sunset times and automatically switches the color scheme to light or dark mode.",
"description": "Intelligently determines sunrise and sunset times and automatically switches the color scheme to light or dark mode.",
"author": "Andrii Hrushetskyi",
"authorUrl": "https://github.com/Andrii256",
"fundingUrl": "https://www.rsukraine.org/our-work",
"fundingUrl": "",
"isDesktopOnly": false
}

View file

@ -1,6 +1,6 @@
{
"name": "smart-day-night-switcher",
"version": "1.0.2",
"version": "1.0.3",
"description": "This plugin intelligently determines sunrise and sunset times and automatically switches the color scheme to light or dark mode.",
"main": "main.js",
"scripts": {

View file

@ -1,3 +1,3 @@
{
"1.0.2": "1.8.9"
"1.0.3": "1.8.9"
}