diff --git a/files/assets/js/bottom.js b/files/assets/js/bottom.js
index 5478e276e..95670b74a 100644
--- a/files/assets/js/bottom.js
+++ b/files/assets/js/bottom.js
@@ -234,3 +234,10 @@ for (const input of inputs) {
if (parseInt(input.value) > parseInt(input.max)) input.value = input.max;
};
}
+
+if (!('serviceWorker' in navigator && 'PushManager' in window)) {
+ let e = document.getElementById("enable-push-nav-item").style = "display: none";
+ if (e) {
+ e.style = "display: none"
+ }
+}
\ No newline at end of file
diff --git a/files/assets/js/core.js b/files/assets/js/core.js
index 963c345dd..b5a04be2a 100644
--- a/files/assets/js/core.js
+++ b/files/assets/js/core.js
@@ -605,3 +605,51 @@ document.querySelectorAll('form').forEach(form => {
form.classList.add('is-submitting');
});
});
+
+function urlB64ToUint8Array(base64String) {
+ const padding = '='.repeat((4 - base64String.length % 4) % 4);
+ const base64 = (base64String + padding)
+ .replace(/\-/g, '+')
+ .replace(/_/g, '/');
+
+ const rawData = window.atob(base64);
+ const outputArray = new Uint8Array(rawData.length);
+
+ for (let i = 0; i < rawData.length; ++i) {
+ outputArray[i] = rawData.charCodeAt(i);
+ }
+ return outputArray;
+}
+
+function updateSubscriptionOnServer(subscription, apiEndpoint) {
+ const formData = new FormData();
+ formData.append("subscription_json", JSON.stringify(subscription));
+
+ const xhr = createXhrWithFormKey(
+ apiEndpoint,
+ 'POST',
+ formData
+ );
+
+ xhr[0].send(xhr[1]);
+}
+
+function enablePushNotifications() {
+ if (!('serviceWorker' in navigator && 'PushManager' in window)) return;
+ let publicKeyElement = document.getElementById('VAPID_PUBLIC_KEY');
+ if (!publicKeyElement) return;
+
+ let publicKey = urlB64ToUint8Array(publicKeyElement.value);
+ navigator.serviceWorker.getRegistration("/assets/js/service_worker.js").then((reg) => {
+ return reg.pushManager.subscribe({
+ userVisibleOnly: true,
+ applicationServerKey: publicKey,
+ })
+ }).then((subscription) => {
+ updateSubscriptionOnServer(subscription, "/push_subscribe")
+ alert("Push notifications are enabled!")
+ }).catch((e) => {
+ alert("Failed to enable push notifications :marseyill:")
+ console.log(e)
+ })
+}
\ No newline at end of file
diff --git a/files/assets/js/register_service_worker.js b/files/assets/js/register_service_worker.js
index 95db4c377..29eb56ee2 100644
--- a/files/assets/js/register_service_worker.js
+++ b/files/assets/js/register_service_worker.js
@@ -1,76 +1,11 @@
'use strict';
-function urlB64ToUint8Array(base64String) {
- const padding = '='.repeat((4 - base64String.length % 4) % 4);
- const base64 = (base64String + padding)
- .replace(/\-/g, '+')
- .replace(/_/g, '/');
-
- const rawData = window.atob(base64);
- const outputArray = new Uint8Array(rawData.length);
-
- for (let i = 0; i < rawData.length; ++i) {
- outputArray[i] = rawData.charCodeAt(i);
- }
- return outputArray;
-}
-
-function updateSubscriptionOnServer(subscription, apiEndpoint) {
- const formData = new FormData();
- formData.append("subscription_json", JSON.stringify(subscription));
-
- const xhr = createXhrWithFormKey(
- apiEndpoint,
- 'POST',
- formData
- );
-
- xhr[0].send(xhr[1]);
-}
-
-function subscribeUser(swRegistration, applicationServerPublicKey, apiEndpoint) {
- const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
- swRegistration.pushManager.subscribe({
- userVisibleOnly: true,
- applicationServerKey: applicationServerKey
- })
- .then(function(subscription) {
- return updateSubscriptionOnServer(subscription, apiEndpoint);
-
- })
- .then(function(response) {
- if (!response.ok) {
- throw new Error('Bad status code from server.');
- }
- return response.json();
- })
- .then(function(responseData) {
- if (responseData.status!=="success") {
- throw new Error('Bad response from server.');
- }
- })
- .catch(function() {
- });
-}
-
-function registerServiceWorker(serviceWorkerUrl, applicationServerPublicKey, apiEndpoint){
- let swRegistration = null;
+function registerServiceWorker(serviceWorkerUrl){
if ('serviceWorker' in navigator && 'PushManager' in window) {
- navigator.serviceWorker.register(serviceWorkerUrl)
- .then(function(swReg) {
- subscribeUser(swReg, applicationServerPublicKey, apiEndpoint);
-
- swRegistration = swReg;
- })
- .catch(function() {
- });
- } else {
+ navigator.serviceWorker.register(serviceWorkerUrl);
}
- return swRegistration;
}
registerServiceWorker(
- "/assets/js/service_worker.js",
- document.getElementById('VAPID_PUBLIC_KEY').value,
- "/push_subscribe"
+ "/assets/js/service_worker.js"
)
diff --git a/files/templates/header.html b/files/templates/header.html
index 7ca82f876..675f0be6d 100644
--- a/files/templates/header.html
+++ b/files/templates/header.html
@@ -254,7 +254,9 @@
My profile
Settings
- Mobile app
+ {% if g.browser != 'webview' %}
+ Mobile app
+ {% endif %}
Donate
@@ -310,7 +312,12 @@
Settings
- Mobile app
+
+ {% if g.browser == 'webview' %}
+ Mobile app
+ {% endif %}
+
+ Enable push notifications
Donate
diff --git a/files/templates/home.html b/files/templates/home.html
index 41a84f799..c5bbafb17 100644
--- a/files/templates/home.html
+++ b/files/templates/home.html
@@ -160,7 +160,10 @@
{% if request.path == '/' and v %}
-
+{% endif %}
+
+{% if v %}
+
{% endif %}
{% if False and request.path == '/' and g.browser != 'webview' and time.time() > session.get('tooltip_dismissed',0)+86400*30 %}