UNPKG

astro-mail-obfuscation

Version:

Protect email addresses, phone numbers and other sensitive data from bots scraping the source code of your Astro app.

1 lines 3.53 kB
class AstroMailObfuscation{decryptionPerformed=false;constructor(){const init=()=>{if(!this.decryptionPerformed){this.deobfuscateContent();this.decryptionPerformed=true}};document.addEventListener("DOMContentLoaded",init);document.addEventListener("astro:page-load",(()=>{this.decryptionPerformed=false;init()}))}deobfuscateContent(){const elements=document.querySelectorAll("[data-7391]");if(!elements.length)return;const{key:key,salt:salt}=this.getKeyAndSalt();if(!key)return;elements.forEach((el=>{const encryptedHref=el.getAttribute("data-5647");const encryptedContent=el.getAttribute("data-7391");const type=el.getAttribute("data-obfuscation");if(!encryptedContent||!type)return;const decrypt=this.decryptionMethods[type];if(!decrypt)return;const content=decrypt(encryptedContent,key,salt);const href=encryptedHref?decrypt(encryptedHref,key,salt):null;if(!content)return;if(href){el.setAttribute("href",href)}const noscriptEl=el.querySelector("noscript");if(noscriptEl){noscriptEl.remove()}el.innerHTML=content}))}getKeyAndSalt(){const meta=document.querySelector('meta[name="4758"]');if(!meta)return{};const content=meta.getAttribute("content");if(!content)return{};const saltLength=8;const key=content.slice(0,-saltLength);const salt=content.slice(-saltLength);return{key:key,salt:salt}}decryptionMethods={1:(data,key)=>{try{const dataBytes=this.hexStringToUint8Array(data);const resultBytes=new Uint8Array(dataBytes.length);for(let i=0;i<dataBytes.length;i++){resultBytes[i]=dataBytes[i]^key.charCodeAt(i%key.length)}const decoder=new TextDecoder;return decoder.decode(resultBytes)}catch(err){console.error("Astro-Mail-Obfuscation: Decryption error in method 1.",err);return null}},2:(data,key)=>{try{const shouldReverse=key.charCodeAt(key.length-1)>key.charCodeAt(key.length-2);const base64=shouldReverse?data.split("").reverse().join(""):data;const dataBytes=Uint8Array.from(atob(base64),(c=>c.charCodeAt(0)));const resultBytes=new Uint8Array(dataBytes.length);for(let i=0;i<dataBytes.length;i++){resultBytes[i]=dataBytes[i]^key.charCodeAt(i%key.length)}const decoder=new TextDecoder;return decoder.decode(resultBytes)}catch(err){console.error("Astro-Mail-Obfuscation: Decryption error in method 2.",err);return null}},3:(data,key,salt)=>{if(!salt){console.error("Astro-Mail-Obfuscation: Salt is required for method 3.");return null}try{const dataBytes=this.base62Decode(data);const resultBytes=new Uint8Array(dataBytes.length);for(let i=0;i<dataBytes.length;i++){resultBytes[i]=dataBytes[i]^key.charCodeAt(i%key.length)^salt.charCodeAt(i%salt.length)}const decoder=new TextDecoder;return decoder.decode(resultBytes)}catch(err){console.error("Astro-Mail-Obfuscation: Decryption error in method 3.",err);return null}}};hexStringToUint8Array(hexString){const length=hexString.length/2;const array=new Uint8Array(length);for(let i=0;i<length;i++){array[i]=parseInt(hexString.substring(i*2,i*2+2),16)}return array}base62Decode(str){const chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";const charMap=new Map;for(let i=0;i<chars.length;i++){charMap.set(chars[i],i)}let value=BigInt(0);const base=BigInt(62);for(const char of str){const index=charMap.get(char);if(index===undefined)throw new Error("Invalid character in Base62 string");value=value*base+BigInt(index)}let hex=value.toString(16);if(hex.length%2)hex="0"+hex;const length=hex.length/2;const bytes=new Uint8Array(length);for(let i=0;i<length;i++){bytes[i]=parseInt(hex.substring(i*2,i*2+2),16)}return bytes}}new AstroMailObfuscation;export{};