Skip to content

Instantly share code, notes, and snippets.

@Klerith
Last active April 7, 2024 14:16
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Klerith/80abd742d726dd587f4bd5d6a0ab26b6 to your computer and use it in GitHub Desktop.
Save Klerith/80abd742d726dd587f4bd5d6a0ab26b6 to your computer and use it in GitHub Desktop.
web-push: urlBase64ToUint8array
// Web-Push
// Public base64 to Uint
function urlBase64ToUint8Array(base64String) {
var padding = '='.repeat((4 - base64String.length % 4) % 4);
var base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
var rawData = window.atob(base64);
var outputArray = new Uint8Array(rawData.length);
for (var i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment