From 6daffab8bfd3228c7e1611583afa5ae1176697b3 Mon Sep 17 00:00:00 2001 From: Matteo Manzinello Date: Sun, 12 May 2019 00:21:38 +0200 Subject: [PATCH] added a lot of comments --- dist/mailgo.js | 5 ++--- dist/mailgo.min.js | 2 +- src/mailgo.js | 40 +++++++++++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/dist/mailgo.js b/dist/mailgo.js index a1a7581..d58268a 100644 --- a/dist/mailgo.js +++ b/dist/mailgo.js @@ -1,12 +1,11 @@ -const VERSION = "0.3.0"; +const V = "0.3.0"; const MAILTO = "mailto:"; // mailgo style const styleSheet = document.createElement("link"); styleSheet.rel = "stylesheet"; styleSheet.type = "text/css"; -styleSheet.href = - "https://unpkg.com/mailgo@" + VERSION + "/dist/mailgo.min.css"; +styleSheet.href = "https://unpkg.com/mailgo@" + V + "/dist/mailgo.min.css"; document.head.appendChild(styleSheet); /** diff --git a/dist/mailgo.min.js b/dist/mailgo.min.js index 088a55a..c189ef8 100644 --- a/dist/mailgo.min.js +++ b/dist/mailgo.min.js @@ -1 +1 @@ -const VERSION="0.3.0",MAILTO="mailto:",styleSheet=document.createElement("link");styleSheet.rel="stylesheet",styleSheet.type="text/css",styleSheet.href="https://unpkg.com/mailgo@0.3.0/dist/mailgo.min.css",document.head.appendChild(styleSheet),mailgoInit=(()=>{let e=document.createElement("div");e.id="mailgo",e.classList.add("mailgo-modal"),e.style.display="none";let t=document.createElement("div");t.className="mailgo-modal-background",e.appendChild(t);let l=document.createElement("div");l.className="mailgo-modal-content",e.appendChild(l);let a=document.createElement("strong");a.id="mailgo-title",a.className="mailgo-title",l.appendChild(a);let o=document.createElement("div");o.id="mailgo-details",o.className="mailgo-details";let d=document.createElement("p");d.id="mailgo-cc";let n=document.createElement("span");n.className="mailgo-weight-500";let i=document.createTextNode("cc ");n.appendChild(i);let c=document.createElement("span");c.id="mailgo-cc-value",d.appendChild(n),d.appendChild(c),o.appendChild(d);let m=document.createElement("p");m.id="mailgo-bcc";let s=document.createElement("span");s.className="mailgo-weight-500";let p=document.createTextNode("bcc ");s.appendChild(p);let g=document.createElement("span");g.id="mailgo-bcc-value",m.appendChild(s),m.appendChild(g),o.appendChild(m);let u=document.createElement("p");u.id="mailgo-subject";let r=document.createElement("span");r.className="mailgo-weight-500";let h=document.createTextNode("subject");r.appendChild(h);let E=document.createElement("span");E.id="mailgo-subject-value",u.appendChild(r),u.appendChild(E),o.appendChild(u);let C=document.createElement("p");C.id="mailgo-body";let y=document.createElement("span");y.className="mailgo-weight-500";let b=document.createTextNode("body ");y.appendChild(b);let L=document.createElement("span");L.id="mailgo-body-value",C.appendChild(y),C.appendChild(L),o.appendChild(C),l.appendChild(o);let v=document.createElement("a");v.id="mailgo-gmail",v.classList.add("mailgo-open"),v.classList.add("gmail");let x=document.createTextNode("open in ");v.appendChild(x);let N=document.createElement("span");N.className="mailgo-weight-500";let f=document.createTextNode("Gmail");N.appendChild(f),v.appendChild(N),l.appendChild(v);let T=document.createElement("a");T.id="mailgo-outlook",T.classList.add("mailgo-open"),T.classList.add("outlook");let w=document.createTextNode("open in ");T.appendChild(w);let k=document.createElement("span");k.className="mailgo-weight-500";let R=document.createTextNode("Outlook");k.appendChild(R),T.appendChild(k),l.appendChild(T);let I=document.createElement("a");I.id="mailgo-open",I.href="#mailgo-open",I.classList.add("mailgo-open"),I.classList.add("mailgo-weight-500");let A=document.createTextNode("open");I.appendChild(A),l.appendChild(I);let j=document.createElement("a");j.id="mailgo-copy",j.href="#mailgo-copy",j.classList.add("mailgo-copy"),j.classList.add("mailgo-weight-500");let M=document.createTextNode("copy");j.appendChild(M),l.appendChild(j);let B=document.createElement("a");B.href="https://mailgo.js.org",B.className="mailgo-by",B.target="_blank";let S=document.createTextNode("mailgo.js.org");B.appendChild(S),l.appendChild(B),document.body.appendChild(e),mailgo.addEventListener("keydown",e=>{27===e.keyCode&&hideMailgo()},!1),t.addEventListener("click",e=>{hideMailgo()},!1)}),mailgoRender=(e=>{let t="",l="",a="",o="",d="",n="";if(e.href&&e.href.toLowerCase().startsWith(MAILTO)){t=decodeURIComponent(e.href.split("?")[0].split(MAILTO)[1].trim()),l=e.href,url=new URL(l);let i=new URLSearchParams(url.search);a=i.get("cc"),o=i.get("bcc"),d=i.get("subject"),n=i.get("body")}else t=e.getAttribute("data-address")+"@"+e.getAttribute("data-domain"),l=MAILTO+encodeURIComponent(t),url=new URL(l);if(!validateEmail(t))return;titleEl=getE("mailgo-title"),detailsEl=getE("mailgo-details"),ccEl=getE("mailgo-cc"),ccValueEl=getE("mailgo-cc-value"),bccEl=getE("mailgo-bcc"),bccValueEl=getE("mailgo-bcc-value"),subjectEl=getE("mailgo-subject"),subjectValueEl=getE("mailgo-subject-value"),bodyEl=getE("mailgo-body"),bodyValueEl=getE("mailgo-body-value"),gmailButton=getE("mailgo-gmail"),outlookButton=getE("mailgo-outlook"),openButton=getE("mailgo-open"),copyButton=getE("mailgo-copy"),titleEl.textContent=t,a?ccValueEl.textContent=a:ccEl.style.display="none",o?bccValueEl.textContent=o:bccEl.style.display="none",d?subjectValueEl.textContent=d:subjectEl.style.display="none",n?bodyValueEl.textContent=n:bodyEl.style.display="none",gmailButton.href="https://mail.google.com/mail?extsrc=mailto&url="+encodeURIComponent(l),outlookButton.href="https://outlook.office.com/owa/?rru=compose&to="+encodeURIComponent(t)+url.search.replace(/^[$]/,"&");let i=encodeEmail(t);openButton.addEventListener("click",()=>{mailToEncoded(i)},!1),copyButton.addEventListener("click",e=>{copyToClipboard(t),copyButton.textContent="copied",setTimeout(()=>{copyButton.textContent="copy"},999)},!1),showMailgo()}),mailgoCheckRender=(e=>{let t=e.target;console.log(t),document.body.contains(getE("mailgo"))&&(t.href&&t.href.toLowerCase().startsWith(MAILTO)&&!t.classList.contains("no-mailgo")||t.href&&"#mailgo"===t.href.toLowerCase()||t.classList.contains("mailgo"))&&(e.preventDefault(),mailgoRender(t))}),document.addEventListener("DOMContentLoaded",mailgoInit,!1),document.addEventListener("click",mailgoCheckRender,!1),validateEmail=(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=(e=>{const t=document.createElement("textarea");t.value=e,t.setAttribute("readonly",""),t.style.position="absolute",t.style.left="-9999px",document.body.appendChild(t);const l=document.getSelection().rangeCount>0&&document.getSelection().getRangeAt(0);t.select(),document.execCommand("copy"),document.body.removeChild(t),l&&(document.getSelection().removeAllRanges(),document.getSelection().addRange(l))}),showMailgo=(()=>getE("mailgo").style.display="flex"),hideMailgo=(()=>getE("mailgo").style.display="none"),mailToEncoded=(e=>window.location.href=MAILTO+atob(e)),encodeEmail=(e=>btoa(e)),getE=(e=>document.getElementById(e)); \ No newline at end of file +const V="0.3.0",MAILTO="mailto:",styleSheet=document.createElement("link");styleSheet.rel="stylesheet",styleSheet.type="text/css",styleSheet.href="https://unpkg.com/mailgo@0.3.0/dist/mailgo.min.css",document.head.appendChild(styleSheet),mailgoInit=(()=>{let e=document.createElement("div");e.id="mailgo",e.classList.add("mailgo-modal"),e.style.display="none";let t=document.createElement("div");t.className="mailgo-modal-background",e.appendChild(t);let l=document.createElement("div");l.className="mailgo-modal-content",e.appendChild(l);let a=document.createElement("strong");a.id="mailgo-title",a.className="mailgo-title",l.appendChild(a);let o=document.createElement("div");o.id="mailgo-details",o.className="mailgo-details";let d=document.createElement("p");d.id="mailgo-cc";let n=document.createElement("span");n.className="mailgo-weight-500";let i=document.createTextNode("cc ");n.appendChild(i);let c=document.createElement("span");c.id="mailgo-cc-value",d.appendChild(n),d.appendChild(c),o.appendChild(d);let m=document.createElement("p");m.id="mailgo-bcc";let s=document.createElement("span");s.className="mailgo-weight-500";let p=document.createTextNode("bcc ");s.appendChild(p);let g=document.createElement("span");g.id="mailgo-bcc-value",m.appendChild(s),m.appendChild(g),o.appendChild(m);let u=document.createElement("p");u.id="mailgo-subject";let r=document.createElement("span");r.className="mailgo-weight-500";let h=document.createTextNode("subject");r.appendChild(h);let E=document.createElement("span");E.id="mailgo-subject-value",u.appendChild(r),u.appendChild(E),o.appendChild(u);let C=document.createElement("p");C.id="mailgo-body";let y=document.createElement("span");y.className="mailgo-weight-500";let b=document.createTextNode("body ");y.appendChild(b);let L=document.createElement("span");L.id="mailgo-body-value",C.appendChild(y),C.appendChild(L),o.appendChild(C),l.appendChild(o);let v=document.createElement("a");v.id="mailgo-gmail",v.classList.add("mailgo-open"),v.classList.add("gmail");let x=document.createTextNode("open in ");v.appendChild(x);let f=document.createElement("span");f.className="mailgo-weight-500";let N=document.createTextNode("Gmail");f.appendChild(N),v.appendChild(f),l.appendChild(v);let T=document.createElement("a");T.id="mailgo-outlook",T.classList.add("mailgo-open"),T.classList.add("outlook");let w=document.createTextNode("open in ");T.appendChild(w);let k=document.createElement("span");k.className="mailgo-weight-500";let R=document.createTextNode("Outlook");k.appendChild(R),T.appendChild(k),l.appendChild(T);let A=document.createElement("a");A.id="mailgo-open",A.href="#mailgo-open",A.classList.add("mailgo-open"),A.classList.add("mailgo-weight-500");let I=document.createTextNode("open");A.appendChild(I),l.appendChild(A);let j=document.createElement("a");j.id="mailgo-copy",j.href="#mailgo-copy",j.classList.add("mailgo-copy"),j.classList.add("mailgo-weight-500");let M=document.createTextNode("copy");j.appendChild(M),l.appendChild(j);let B=document.createElement("a");B.href="https://mailgo.js.org",B.className="mailgo-by",B.target="_blank";let S=document.createTextNode("mailgo.js.org");B.appendChild(S),l.appendChild(B),document.body.appendChild(e),mailgo.addEventListener("keydown",e=>{27===e.keyCode&&hideMailgo()},!1),t.addEventListener("click",e=>{hideMailgo()},!1)}),mailgoRender=(e=>{let t="",l="",a="",o="",d="",n="";if(e.href&&e.href.toLowerCase().startsWith(MAILTO)){t=decodeURIComponent(e.href.split("?")[0].split(MAILTO)[1].trim()),l=e.href,url=new URL(l);let i=new URLSearchParams(url.search);a=i.get("cc"),o=i.get("bcc"),d=i.get("subject"),n=i.get("body")}else t=e.getAttribute("data-address")+"@"+e.getAttribute("data-domain"),l=MAILTO+encodeURIComponent(t),url=new URL(l);if(!validateEmail(t))return;titleEl=getE("mailgo-title"),detailsEl=getE("mailgo-details"),ccEl=getE("mailgo-cc"),ccValueEl=getE("mailgo-cc-value"),bccEl=getE("mailgo-bcc"),bccValueEl=getE("mailgo-bcc-value"),subjectEl=getE("mailgo-subject"),subjectValueEl=getE("mailgo-subject-value"),bodyEl=getE("mailgo-body"),bodyValueEl=getE("mailgo-body-value"),gmailButton=getE("mailgo-gmail"),outlookButton=getE("mailgo-outlook"),openButton=getE("mailgo-open"),copyButton=getE("mailgo-copy"),titleEl.textContent=t,a?ccValueEl.textContent=a:ccEl.style.display="none",o?bccValueEl.textContent=o:bccEl.style.display="none",d?subjectValueEl.textContent=d:subjectEl.style.display="none",n?bodyValueEl.textContent=n:bodyEl.style.display="none",gmailButton.href="https://mail.google.com/mail?extsrc=mailto&url="+encodeURIComponent(l),outlookButton.href="https://outlook.office.com/owa/?rru=compose&to="+encodeURIComponent(t)+url.search.replace(/^[$]/,"&");let i=encodeEmail(t);openButton.addEventListener("click",()=>{mailToEncoded(i)},!1),copyButton.addEventListener("click",e=>{copyToClipboard(t),copyButton.textContent="copied",setTimeout(()=>{copyButton.textContent="copy"},999)},!1),showMailgo()}),mailgoCheckRender=(e=>{let t=e.target;console.log(t),document.body.contains(getE("mailgo"))&&(t.href&&t.href.toLowerCase().startsWith(MAILTO)&&!t.classList.contains("no-mailgo")||t.href&&"#mailgo"===t.href.toLowerCase()||t.classList.contains("mailgo"))&&(e.preventDefault(),mailgoRender(t))}),document.addEventListener("DOMContentLoaded",mailgoInit,!1),document.addEventListener("click",mailgoCheckRender,!1),validateEmail=(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=(e=>{const t=document.createElement("textarea");t.value=e,t.setAttribute("readonly",""),t.style.position="absolute",t.style.left="-9999px",document.body.appendChild(t);const l=document.getSelection().rangeCount>0&&document.getSelection().getRangeAt(0);t.select(),document.execCommand("copy"),document.body.removeChild(t),l&&(document.getSelection().removeAllRanges(),document.getSelection().addRange(l))}),showMailgo=(()=>getE("mailgo").style.display="flex"),hideMailgo=(()=>getE("mailgo").style.display="none"),mailToEncoded=(e=>window.location.href=MAILTO+atob(e)),encodeEmail=(e=>btoa(e)),getE=(e=>document.getElementById(e)); \ No newline at end of file diff --git a/src/mailgo.js b/src/mailgo.js index f717f04..58776e3 100644 --- a/src/mailgo.js +++ b/src/mailgo.js @@ -1,12 +1,11 @@ -const VERSION = "MAILGO_VERSION"; +const V = "MAILGO_VERSION"; const MAILTO = "mailto:"; // mailgo style const styleSheet = document.createElement("link"); styleSheet.rel = "stylesheet"; styleSheet.type = "text/css"; -styleSheet.href = - "https://unpkg.com/mailgo@" + VERSION + "/dist/mailgo.min.css"; +styleSheet.href = "https://unpkg.com/mailgo@" + V + "/dist/mailgo.min.css"; document.head.appendChild(styleSheet); /** @@ -186,7 +185,7 @@ mailgoRender = mailgo => { subject = "", bodyMail = ""; - // mailgo all the element with href=^"mailto:" + // if the element href=^"mailto:" if (mailgo.href && mailgo.href.toLowerCase().startsWith(MAILTO)) { mail = decodeURIComponent( mailgo.href @@ -205,7 +204,7 @@ mailgoRender = mailgo => { subject = urlParams.get("subject"); bodyMail = urlParams.get("body"); } else { - // mailgo all the element with href="#mailgo" or class="mailgo" + // if the element href="#mailgo" or class="mailgo" // email = data-address + @ + data-domain mail = mailgo.getAttribute("data-address") + @@ -218,6 +217,7 @@ mailgoRender = mailgo => { // validate the email address if (!validateEmail(mail)) return; + // information titleEl = getE("mailgo-title"); detailsEl = getE("mailgo-details"); ccEl = getE("mailgo-cc"); @@ -229,22 +229,34 @@ mailgoRender = mailgo => { bodyEl = getE("mailgo-body"); bodyValueEl = getE("mailgo-body-value"); + // actions gmailButton = getE("mailgo-gmail"); outlookButton = getE("mailgo-outlook"); openButton = getE("mailgo-open"); copyButton = getE("mailgo-copy"); + // the title of the modal (email address) titleEl.textContent = mail; - cc ? (ccValueEl.textContent = cc) : (ccEl.style.display = "none"); - bcc ? (bccValueEl.textContent = bcc) : (bccEl.style.display = "none"); + // add the details if provided + cc + ? ((ccEl.style.display = "block"), (ccValueEl.textContent = cc)) + : (ccEl.style.display = "none"); + + bcc + ? ((bccEl.style.display = "block"), (bccValueEl.textContent = bcc)) + : (bccEl.style.display = "none"); + subject - ? (subjectValueEl.textContent = subject) + ? ((subjectEl.style.display = "block"), + (subjectValueEl.textContent = subject)) : (subjectEl.style.display = "none"); + bodyMail - ? (bodyValueEl.textContent = bodyMail) + ? ((bodyEl.style.display = "block"), (bodyValueEl.textContent = bodyMail)) : (bodyEl.style.display = "none"); + // add the actions gmailButton.href = "https://mail.google.com/mail?extsrc=mailto&url=" + encodeURIComponent(mailtoHref); @@ -275,15 +287,21 @@ mailgoRender = mailgo => { false ); + // show the mailgo showMailgo(); }; +/** + * mailgoCheckRender + * function to check if an element is mailgo-enabled or not referencing to the old + * document.querySelectorAll( + * 'a[href^="mailto:" i]:not(.no-mailgo), a[href="#mailgo"], a.mailgo' + * ); + */ mailgoCheckRender = event => { // the target element let e = event.target; - console.log(e); - // check if the id=mailgo exists in the body if (!document.body.contains(getE("mailgo"))) return;