Squashed 'themes/tabi-lean/' content from commit efb4246
git-subtree-dir: themes/tabi-lean git-subtree-split: efb42461fde7ee5b0c140d27f2789fdf9c98b100
This commit is contained in:
commit
8be91ee3d9
336 changed files with 25227 additions and 0 deletions
44
static/js/decodeMail.js
Normal file
44
static/js/decodeMail.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// Utility function: Base64 Decoding.
|
||||
function decodeBase64(encodedString) {
|
||||
try {
|
||||
// Can't use atob() directly because it doesn't support non-ascii characters.
|
||||
// And non-ascii characters are allowed in email addresses and domains.
|
||||
// See https://en.wikipedia.org/wiki/Email_address#Internationalization
|
||||
// Code below adapted from Jackie Han: https://stackoverflow.com/a/64752311
|
||||
const byteString = atob(encodedString);
|
||||
|
||||
// Convert byteString to an array of char codes.
|
||||
const charCodes = [...byteString].map((char) => char.charCodeAt(0));
|
||||
|
||||
// Use TypedArray.prototype.set() to copy the char codes into a Uint8Array.
|
||||
const bytes = new Uint8Array(charCodes.length);
|
||||
bytes.set(charCodes);
|
||||
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
return decoder.decode(bytes);
|
||||
} catch (e) {
|
||||
console.error('Failed to decode Base64 string: ', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Utility function: Update href of an element with a decoded email.
|
||||
function updateEmailHref(element) {
|
||||
const encodedEmail = element.getAttribute('data-encoded-email');
|
||||
const decodedEmail = decodeBase64(encodedEmail);
|
||||
|
||||
if (decodedEmail) {
|
||||
element.setAttribute('href', `mailto:${decodedEmail}`);
|
||||
} else {
|
||||
// If the decoding fails, hide the email link.
|
||||
element.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch and process email elements with the "data-encoded-email" attribute.
|
||||
const encodedEmailElements = document.querySelectorAll('[data-encoded-email]');
|
||||
encodedEmailElements.forEach(updateEmailHref);
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue