diff --git a/README.md b/README.md index e756311..d054765 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/main.ts b/main.ts index 257d8d6..44e2778 100644 --- a/main.ts +++ b/main.ts @@ -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 | 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( - `${dateStr}${dawnStr}${duskStr}` - ); - - date.setDate(date.getDate() + 1); // to increase date before next call - } - - const html = `

Schedule:

${rows.join( - "" - )}
Date Dawn
(Light mode will be enabled)
Dusk
(Dark mode will be enabled)
`; - - 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") - ] = `

Please enter the latitude and longitude of your approximate location (coords of any city within ±200 km)

`; + 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") - ] = `

To easily find your latitude and longitude, you can use any simple online service, such as latlong.net, gps-coordinates.net, or any other similar tools available on the web.

* Obsidian does not provide developers with access to geolocation, so this plugin cannot automatically determine your coordinates.
 However, to accurately calculate sunrise and sunset times in your location, the formula needs an approximate location (within ±200 km) of where you are.

`; + 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(); } } diff --git a/manifest.json b/manifest.json index 4956dc7..472317d 100644 --- a/manifest.json +++ b/manifest.json @@ -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 } diff --git a/package.json b/package.json index 0171e60..0c9de18 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/versions.json b/versions.json index c9bd7e7..7dc4ad1 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "1.0.2": "1.8.9" + "1.0.3": "1.8.9" }