Skip to content

Commit 88e0818

Browse files
committed
Create ability to pick location manually
1 parent e863caf commit 88e0818

File tree

12 files changed

+545
-6
lines changed

12 files changed

+545
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"build": "node build.js"
1313
},
1414
"dependencies": {
15+
"leaflet": "^1.9.4",
1516
"sortablejs": "^1.15.6"
1617
},
1718
"devDependencies": {

src/images/icons/marker_icon.png

1.43 KB
Loading

src/js/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ function faviconURL(u) {
1717
)
1818
}
1919

20+
class ManualPosition {
21+
constructor(lat, lon) {
22+
this.coords = {
23+
latitude: lat,
24+
longitude: lon
25+
}
26+
}
27+
}
28+
2029
function createBookmark(
2130
el, name, url,
2231
{
@@ -90,9 +99,14 @@ if (isExtension) {
9099
}
91100

92101
if (items.wkey) {
93-
navigator.geolocation.getCurrentPosition(function(position) {
102+
if (items.wManualLocation) {
103+
const position = new ManualPosition(items.wlat, items.wlon)
94104
getWeather(items, position, items.wkey, items.wlanguage)
95-
})
105+
} else {
106+
navigator.geolocation.getCurrentPosition((position) => {
107+
getWeather(items, position, items.wkey, items.wlanguage)
108+
})
109+
}
96110
}
97111

98112
const bookmarks = document.getElementById("bookmarks")

src/js/options.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as manifest from "../manifest.json"
22
import Sortable from "sortablejs"
3+
34
import { extensionSettings } from "./utils/settings.js"
45
import { isFirefox } from "./utils/browser.js"
6+
import { WorldMap } from "./utils/map.js"
57
import { HexClock } from "./utils/timeManager.js"
68
import { weatherLanguages, dateLocales } from "./utils/lists.js"
79

@@ -10,6 +12,8 @@ if (findVersion) {
1012
findVersion.textContent = manifest.version
1113
}
1214

15+
const userMap = new WorldMap()
16+
1317
function createAlert(message, css="") {
1418
const notification = document.getElementById("notification")
1519
const alert = document.createElement("div")
@@ -38,6 +42,9 @@ function saveOptions(message, css="") {
3842
customfont: document.getElementById("customfont").value,
3943
customfontgoogle: document.getElementById("customfontgoogle").checked,
4044
wkey: document.getElementById("wkey").value,
45+
wlat: userMap.marker ? userMap.marker.getLatLng().lat : 0,
46+
wlon: userMap.marker ? userMap.marker.getLatLng().lng : 0,
47+
wManualLocation: document.getElementById("wManualLocation").checked,
4148
hexbg: document.getElementById("hexbg").checked,
4249
temp_type: document.getElementById("temp_type").value,
4350
showSettings: document.getElementById("show-settings").checked,
@@ -51,6 +58,27 @@ function saveOptions(message, css="") {
5158
})
5259
}
5360

61+
function createMapInit(items) {
62+
const getMap = document.getElementById("map")
63+
getMap.style.display = "block"
64+
65+
try {
66+
userMap.createMap("map", items.wlat, items.wlon)
67+
} catch {
68+
return // Ignore error
69+
}
70+
71+
userMap.map.on("click", (e) => {
72+
const { lat, lng } = e.latlng
73+
userMap.setMarker(lat, lng)
74+
const wlat = document.getElementById("wlat")
75+
const wlon = document.getElementById("wlon")
76+
wlat.textContent = lat.toFixed(4)
77+
wlon.textContent = lng.toFixed(4)
78+
saveOptions(`Weather location set: Lat ${lat.toFixed(4)}, Lon ${lng.toFixed(4)}`, "change")
79+
})
80+
}
81+
5482
// Restores select box and checkbox state using the preferences
5583
// stored in chrome.storage.
5684
function restoreOptions() {
@@ -75,6 +103,27 @@ function restoreOptions() {
75103
wlanguage.value = items.wlanguage
76104
wlanguage.onchange = () => { saveOptions(`Weather language set: ${wlanguage.value || "default"}`, wlanguage.value ? "change" : "remove") }
77105

106+
const wManualLocation = document.getElementById("wManualLocation")
107+
wManualLocation.checked = items.wManualLocation
108+
wManualLocation.onchange = () => {
109+
saveOptions(`Weather manual location set: ${wManualLocation.checked}`, wManualLocation.checked ? "add" : "remove")
110+
111+
if (wManualLocation.checked) {
112+
createMapInit(items)
113+
} else {
114+
document.getElementById("map").style.display = "none"
115+
}
116+
}
117+
118+
if (items.wManualLocation) {
119+
createMapInit(items)
120+
}
121+
122+
const wlat = document.getElementById("wlat")
123+
const wlon = document.getElementById("wlon")
124+
wlat.textContent = items.wlat.toFixed(4) || "0.0000"
125+
wlon.textContent = items.wlon.toFixed(4) || "0.0000"
126+
78127
const fmtTime = document.getElementById("fmt_time")
79128
fmtTime.value = items.fmt_time
80129
fmtTime.onchange = () => { saveOptions(`Time format set: ${fmtTime.value || "default"}`, fmtTime.value ? "change" : "remove") }

src/js/utils/map.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import leaflet from "leaflet"
2+
3+
export class WorldMap {
4+
constructor() {
5+
// Default values
6+
this.lat = 20
7+
this.lon = 0
8+
9+
this.map = null
10+
this.marker = null
11+
}
12+
13+
createMap(targetId, lat, lon) {
14+
this.lat = lat
15+
this.lon = lon
16+
17+
this.map = leaflet.map(
18+
targetId,
19+
{
20+
preferCanvas: true,
21+
attribution: "&copy; <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>",
22+
maxBounds: [[-90, -180], [90, 180]],
23+
maxBoundsViscosity: 1.0
24+
}
25+
)
26+
this.map.setView([this.lat, this.lon], 3)
27+
leaflet.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(this.map)
28+
this.marker = leaflet.marker([this.lat, this.lon], {
29+
icon: leaflet.icon({
30+
iconUrl: "images/icons/marker_icon.png",
31+
iconSize: [25, 41],
32+
iconAnchor: [12, 41]
33+
})
34+
}).addTo(this.map)
35+
}
36+
37+
setMarker(lat, lon) {
38+
this.lat = lat
39+
this.lon = lon
40+
this.marker.setLatLng([lat, lon])
41+
}
42+
43+
getCoordinates() {
44+
return { lat: this.lat, lon: this.lon }
45+
}
46+
}

src/js/utils/settings.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export const extensionSettings = {
1616
bookmarksTopSitesEnabled: false,
1717
bookmarksTopSitesAmount: 5,
1818
wkey: "",
19+
wlat: 0,
20+
wlon: 0,
21+
wManualLocation: false,
1922
temp_type: "celcius",
2023
hexbg: false,
2124
showSettings: true,

src/manifest.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "AlexFlipnote/Homepage",
55
"description": "A website that gives you a clean homepage.",
66
"homepage_url": "https://alexflipnote.dev/homepage/",
7-
"version": "2.3.0",
7+
"version": "2.3.1",
88

99
"chrome_url_overrides": {
1010
"newtab": "index.html"
@@ -44,6 +44,7 @@
4444
],
4545

4646
"host_permissions": [
47-
"https://api.openweathermap.org/*"
47+
"https://api.openweathermap.org/*",
48+
"https://*.tile.openstreetmap.org/*"
4849
]
4950
}

src/options.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ <h1 class="no-margin">24h HEX Background</h1>
209209
</div>
210210

211211
<div class="box-container">
212-
<h1 class="no-margin">OpenWeather key</h1>
212+
<h1 class="no-margin">Weather (From OpenWeatherMap)</h1>
213213
<p>
214214
To get your own weather key, go to <a href="https://home.openweathermap.org/api_keys" target="_blank">https://home.openweathermap.org/api_keys</a> to recive one.
215215
</p>
@@ -230,6 +230,14 @@ <h1 class="no-margin">OpenWeather key</h1>
230230
<option value="fahrenheit">Fahrenheit (°F)</option>
231231
</select>
232232
</label>
233+
<label>
234+
<input type="checkbox" id="wManualLocation"> Pick a location manually instead
235+
</label>
236+
<div id="map">
237+
<div class="map-coords">
238+
<span id="wlat"></span>, <span id="wlon"></span>
239+
</div>
240+
</div>
233241
</div>
234242

235243
<div class="box-container">

src/privacy.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ <h2>Permissions Used</h2>
3131
Your location data is never stored, it's used temporarily by your browser to fetch the
3232
weather from <a href="https://openweathermap.org/" class="emerald-text">OpenWeatherMap</a> with an API key
3333
and your coordinates are not transmitted to any other party.
34-
If weather is turned off, geolocation is not used.
34+
If weather is turned off or you have enabled manual location, geolocation is not used.
3535
</li>
3636
<li><b>topSites</b>: Used to fetch your most visited sites for displaying bookmarks if you enable that feature. If disabled, this permission is not used.</li>
3737
<li><b>favicon</b>: Used to fetch the favicon of your most visited sites for displaying bookmarks. If disabled, this permission is not used.</li>

0 commit comments

Comments
 (0)