1 line
6.4 KiB
JavaScript
1 line
6.4 KiB
JavaScript
"use strict";var V="0.3.1",MAILTO="mailto:",mailgoCSS=document.createElement("link");mailgoCSS.rel="stylesheet",mailgoCSS.type="text/css",mailgoCSS.href="https://unpkg.com/mailgo@"+V+"/dist/mailgo.min.css",document.head.appendChild(mailgoCSS);var mailgoInit=function(){var e=document.createElement("div");e.id="mailgo",e.classList.add("mailgo-modal"),e.style.display="none";var t=document.createElement("div");t.className="mailgo-modal-background",e.appendChild(t);var a=document.createElement("div");a.className="mailgo-modal-content",e.appendChild(a);var o=document.createElement("strong");o.id="mailgo-title",o.className="mailgo-title",a.appendChild(o);var d=document.createElement("div");d.id="mailgo-details",d.className="mailgo-details";var n=document.createElement("p");n.id="mailgo-cc";var l=document.createElement("span");l.className="mailgo-weight-500";var i=document.createTextNode("cc ");l.appendChild(i);var c=document.createElement("span");c.id="mailgo-cc-value",n.appendChild(l),n.appendChild(c),d.appendChild(n);var m=document.createElement("p");m.id="mailgo-bcc";var r=document.createElement("span");r.className="mailgo-weight-500";var s=document.createTextNode("bcc ");r.appendChild(s);var g=document.createElement("span");g.id="mailgo-bcc-value",m.appendChild(r),m.appendChild(g),d.appendChild(m);var p=document.createElement("p");p.id="mailgo-subject";var u=document.createElement("span");u.className="mailgo-weight-500";var h=document.createTextNode("subject");u.appendChild(h);var v=document.createElement("span");v.id="mailgo-subject-value",p.appendChild(u),p.appendChild(v),d.appendChild(p);var C=document.createElement("p");C.id="mailgo-body";var E=document.createElement("span");E.className="mailgo-weight-500";var y=document.createTextNode("body ");E.appendChild(y);var b=document.createElement("span");b.id="mailgo-body-value",C.appendChild(E),C.appendChild(b),d.appendChild(C),a.appendChild(d);var f=document.createElement("a");f.id="mailgo-gmail",f.classList.add("mailgo-open"),f.classList.add("gmail");var L=document.createTextNode("open in ");f.appendChild(L);var w=document.createElement("span");w.className="mailgo-weight-500";var x=document.createTextNode("Gmail");w.appendChild(x),f.appendChild(w),a.appendChild(f);var k=document.createElement("a");k.id="mailgo-outlook",k.classList.add("mailgo-open"),k.classList.add("outlook");var N=document.createTextNode("open in ");k.appendChild(N);var T=document.createElement("span");T.className="mailgo-weight-500";var A=document.createTextNode("Outlook");T.appendChild(A),k.appendChild(T),a.appendChild(k);var S=document.createElement("a");S.id="mailgo-open",S.href="#mailgo-open",S.classList.add("mailgo-open"),S.classList.add("mailgo-weight-500");var R=document.createTextNode("open");S.appendChild(R),a.appendChild(S);var I=document.createElement("a");I.id="mailgo-copy",I.href="#mailgo-copy",I.classList.add("mailgo-copy"),I.classList.add("mailgo-weight-500");var M=document.createTextNode("copy");I.appendChild(M),a.appendChild(I);var j=document.createElement("a");j.href="https://mailgo.js.org",j.className="mailgo-by",j.target="_blank";var O=document.createTextNode("mailgo.js.org");j.appendChild(O),a.appendChild(j),document.body.appendChild(e),t.addEventListener("click",hideMailgo,!1)},mailgoRender=function(e){var t="",a="",o="",d="",n="",l="",i="";if(e.href&&e.href.toLowerCase().startsWith(MAILTO)){t=decodeURIComponent(e.href.split("?")[0].split(MAILTO)[1].trim()),o=e.href,a=new URL(o);var c=new URLSearchParams(a.search);d=c.get("cc"),n=c.get("bcc"),l=c.get("subject"),i=c.get("body")}else t=e.getAttribute("data-address")+"@"+e.getAttribute("data-domain"),o=MAILTO+encodeURIComponent(t),a=new URL(o),d=e.getAttribute("data-cc-address")+"@"+e.getAttribute("data-cc-domain"),n=e.getAttribute("data-bcc-address")+"@"+e.getAttribute("data-bcc-domain"),l=e.getAttribute("data-subject"),i=e.getAttribute("data-body");if(validateEmail(t)){validateEmail(d)||(d=""),validateEmail(n)||(n="");var m=getE("mailgo-title"),r=(getE("mailgo-details"),getE("mailgo-cc")),s=getE("mailgo-cc-value"),g=getE("mailgo-bcc"),p=getE("mailgo-bcc-value"),u=getE("mailgo-subject"),h=getE("mailgo-subject-value"),v=getE("mailgo-body"),C=getE("mailgo-body-value"),E=getE("mailgo-gmail"),y=getE("mailgo-outlook"),b=getE("mailgo-open"),f=getE("mailgo-copy");m.textContent=t,d?(r.style.display="block",s.textContent=d):r.style.display="none",n?(g.style.display="block",p.textContent=n):g.style.display="none",l?(u.style.display="block",h.textContent=l):u.style.display="none",i?(v.style.display="block",C.textContent=i):v.style.display="none",E.href="https://mail.google.com/mail?extsrc=mailto&url="+encodeURIComponent(o),y.href="https://outlook.office.com/owa/?rru=compose&to="+encodeURIComponent(t)+a.search.replace(/^[$]/,"&");var L=encodeEmail(t);b.addEventListener("click",function(){mailToEncoded(L)},!1),f.addEventListener("click",function(e){copyToClipboard(t),f.textContent="copied",setTimeout(function(){f.textContent="copy"},999)},!1),showMailgo()}},mailgoCheckRender=function(e){var t=e.target;document.body.contains(getE("mailgo"))&&(t.href&&t.href.toLowerCase().startsWith(MAILTO)&&!t.classList.contains("no-mailgo")||t.href&&"#mailgo"===t.getAttribute("href").toLowerCase()||t.classList.contains("mailgo")||t.getAttribute("mailgo"))&&(e.preventDefault(),mailgoRender(t))},mailgoKeydown=function(e){switch(e.keyCode){case 27:hideMailgo();break;default:return}};document.addEventListener("DOMContentLoaded",mailgoInit,!1),document.body.addEventListener("click",mailgoCheckRender,!1);var validateEmail=function(e){return/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(e)},copyToClipboard=function(e){var t=document.createElement("textarea");t.value=e,t.setAttribute("readonly",""),t.style.position="absolute",t.style.left="-9999px",document.body.appendChild(t);var a=document.getSelection().rangeCount>0&&document.getSelection().getRangeAt(0);t.select(),document.execCommand("copy"),document.body.removeChild(t),a&&(document.getSelection().removeAllRanges(),document.getSelection().addRange(a))},showMailgo=function(){getE("mailgo").style.display="flex",document.body.addEventListener("keydown",mailgoKeydown,!1)},hideMailgo=function(){getE("mailgo").style.display="none",document.body.removeEventListener("keydown",mailgoKeydown,!1)},mailToEncoded=function(e){return window.location.href=MAILTO+atob(e)},encodeEmail=function(e){return btoa(e)},getE=function(e){return document.getElementById(e)}; |