mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-06-08 08:08:01 -04:00
60 lines
2.1 KiB
JavaScript
60 lines
2.1 KiB
JavaScript
//
|
|
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
|
// listed below.
|
|
//
|
|
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
|
|
// vendor/assets/javascripts directory can be referenced here using a relative path.
|
|
//
|
|
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
|
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
|
//
|
|
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
|
// about supported directives.
|
|
//
|
|
//= require rails-ujs
|
|
//= require activestorage
|
|
//= require_tree .
|
|
|
|
function urlBase64ToUint8Array(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;
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const pushButton = document.getElementById('enable-push-notifications');
|
|
if (pushButton) {
|
|
pushButton.addEventListener('click', () => {
|
|
if ('serviceWorker' in navigator) {
|
|
navigator.serviceWorker.ready.then(registration => {
|
|
const vapidPublicKey = document.querySelector('meta[name="vapid-public-key"]').content;
|
|
const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey);
|
|
|
|
registration.pushManager.subscribe({
|
|
userVisibleOnly: true,
|
|
applicationServerKey: convertedVapidKey
|
|
}).then(subscription => {
|
|
fetch('/push_subscriptions', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
|
|
},
|
|
body: JSON.stringify({ subscription: subscription.toJSON() })
|
|
});
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|