GoogleChromeLabs / web-push-codelab

Other
557 stars 293 forks source link

Missing function from tutorial #46

Open bohdaq opened 7 years ago

bohdaq commented 7 years ago

Tutorial does not have urlB64ToUint8Array function declared.

When user will try to make Push Notifications work, they will end up with non existing urlB64ToUint8Array method.

Please include sample into tutorial

gauntface commented 7 years ago

Where is urlBase64ToUint8Array mentioned in the tutorial?

tomasdev commented 6 years ago

By the way the function is defined at https://github.com/GoogleChrome/push-notifications/blob/master/app/scripts/main.js

atb00ker commented 6 years ago

(Considering this issue is still relevent)urlBase64ToUint8Array is mentioned on this page: https://developers.google.com/web/fundamentals/push-notifications/subscribing-a-user

Antoinebr commented 5 years ago

If needed here's the function


/**
 * urlBase64ToUint8Array
 * 
 * @param {string} base64String a public vavid key
 */
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;
}
riophae commented 4 years ago

I've also found this: https://www.npmjs.com/package/urlb64touint8array

Dygear commented 1 year ago

The real question. Why is this not built into browsers?