mirror of
https://github.com/migatu/astrololo.git
synced 2026-07-14 21:38:37 +00:00
Merge pull request #15 from migatu/fix/geo-secure-context
Geolokalizacja: jawny komunikat gdy brak secure context (http)
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
// „Tu i teraz": uzupełnia datę/godzinę/strefę z przeglądarki oraz — jeśli to
|
||||||
|
// możliwe — lokalizację (lat/lon).
|
||||||
|
//
|
||||||
|
// UWAGA: geolokalizacja przeglądarki działa tylko w "secure context"
|
||||||
|
// (https:// lub localhost). Na http://<ip> przeglądarka NIE pyta o zgodę,
|
||||||
|
// tylko po cichu odmawia — dlatego pokazujemy jawny komunikat w #geoNote.
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
const btn = document.getElementById('nowBtn');
|
||||||
|
if (!btn) return;
|
||||||
|
|
||||||
|
const note = document.getElementById('geoNote');
|
||||||
|
const say = msg => { if (note) note.textContent = msg; };
|
||||||
|
|
||||||
|
btn.addEventListener('click', function () {
|
||||||
|
const d = new Date();
|
||||||
|
const pad = n => String(n).padStart(2, '0');
|
||||||
|
document.querySelector('input[name=date]').value =
|
||||||
|
d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate());
|
||||||
|
document.querySelector('input[name=time]').value = pad(d.getHours()) + ':' + pad(d.getMinutes());
|
||||||
|
document.querySelector('input[name=tz_offset]').value = (-d.getTimezoneOffset() / 60);
|
||||||
|
|
||||||
|
if (!('geolocation' in navigator)) {
|
||||||
|
say('Ta przeglądarka nie udostępnia geolokalizacji — wpisz lat/lon ręcznie.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!window.isSecureContext) {
|
||||||
|
say('Lokalizacja z przeglądarki wymaga HTTPS lub localhost (otwarto przez http://) — wpisz lat/lon ręcznie.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
say('Pobieram lokalizację…');
|
||||||
|
navigator.geolocation.getCurrentPosition(
|
||||||
|
function (pos) {
|
||||||
|
document.querySelector('input[name=lat]').value = pos.coords.latitude.toFixed(4);
|
||||||
|
document.querySelector('input[name=lon]').value = pos.coords.longitude.toFixed(4);
|
||||||
|
say('Lokalizacja pobrana ✓');
|
||||||
|
},
|
||||||
|
function (err) {
|
||||||
|
say('Nie udało się pobrać lokalizacji: ' + (err && err.message ? err.message : 'odmowa dostępu'));
|
||||||
|
},
|
||||||
|
{ timeout: 8000 }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -36,6 +36,7 @@
|
|||||||
<div class="actions">
|
<div class="actions">
|
||||||
<button type="button" id="nowBtn" class="ghost">Tu i teraz</button>
|
<button type="button" id="nowBtn" class="ghost">Tu i teraz</button>
|
||||||
<button type="submit">Policz horoskop</button>
|
<button type="submit">Policz horoskop</button>
|
||||||
|
<span id="geoNote" class="muted small"></span>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
@@ -110,22 +111,5 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<script>
|
<script src="/static/now.js"></script>
|
||||||
// „Tu i teraz": uzupełnia datę/godzinę bieżącą i offset lokalny przeglądarki.
|
|
||||||
document.getElementById('nowBtn').addEventListener('click', function () {
|
|
||||||
const d = new Date();
|
|
||||||
const pad = n => String(n).padStart(2, '0');
|
|
||||||
document.querySelector('input[name=date]').value =
|
|
||||||
d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate());
|
|
||||||
document.querySelector('input[name=time]').value = pad(d.getHours()) + ':' + pad(d.getMinutes());
|
|
||||||
document.querySelector('input[name=tz_offset]').value = (-d.getTimezoneOffset() / 60);
|
|
||||||
// bajer: lokalizacja z przeglądarki (wymaga zgody; działa na https/localhost)
|
|
||||||
if (navigator.geolocation) {
|
|
||||||
navigator.geolocation.getCurrentPosition(function (pos) {
|
|
||||||
document.querySelector('input[name=lat]').value = pos.coords.latitude.toFixed(4);
|
|
||||||
document.querySelector('input[name=lon]').value = pos.coords.longitude.toFixed(4);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
<div class="actions">
|
<div class="actions">
|
||||||
<button type="button" id="nowBtn" class="ghost">Tu i teraz</button>
|
<button type="button" id="nowBtn" class="ghost">Tu i teraz</button>
|
||||||
<button type="submit">Szukaj interpretacji</button>
|
<button type="submit">Szukaj interpretacji</button>
|
||||||
|
<span id="geoNote" class="muted small"></span>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
@@ -82,21 +83,5 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<script>
|
<script src="/static/now.js"></script>
|
||||||
document.getElementById('nowBtn').addEventListener('click', function () {
|
|
||||||
const d = new Date();
|
|
||||||
const pad = n => String(n).padStart(2, '0');
|
|
||||||
document.querySelector('input[name=date]').value =
|
|
||||||
d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate());
|
|
||||||
document.querySelector('input[name=time]').value = pad(d.getHours()) + ':' + pad(d.getMinutes());
|
|
||||||
document.querySelector('input[name=tz_offset]').value = (-d.getTimezoneOffset() / 60);
|
|
||||||
// bajer: lokalizacja z przeglądarki (wymaga zgody; działa na https/localhost)
|
|
||||||
if (navigator.geolocation) {
|
|
||||||
navigator.geolocation.getCurrentPosition(function (pos) {
|
|
||||||
document.querySelector('input[name=lat]').value = pos.coords.latitude.toFixed(4);
|
|
||||||
document.querySelector('input[name=lon]').value = pos.coords.longitude.toFixed(4);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user