Skip to content

Instantly share code, notes, and snippets.

@Fordi
Created May 31, 2017 18:42
Show Gist options
  • Save Fordi/f0a1cdf109bb7d21a7f0a94e1f2b43df to your computer and use it in GitHub Desktop.
Save Fordi/f0a1cdf109bb7d21a7f0a94e1f2b43df to your computer and use it in GitHub Desktop.
DOM btoa / atob UTF-8 suppport
((win, btoa, atob) => Object.assign(win, {
btoa: (str) => btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(parseInt(p1, 16)))),
atob: (str) => decodeURIComponent(atob(str).split('').map((c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''))
}))(this, btoa, atob);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment