From 335cb19a077b0d1719e04dcdea7a2672a67cb778 Mon Sep 17 00:00:00 2001 From: Matteo Manzinello Date: Sat, 26 Oct 2019 13:30:52 +0200 Subject: [PATCH] a draft of window exists --- src/mailgo.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/mailgo.js b/src/mailgo.js index 6273a06..055c7ec 100644 --- a/src/mailgo.js +++ b/src/mailgo.js @@ -38,13 +38,15 @@ let gmailButton, callButton, copyButton; -// mailgo style (gulp) -let mailgoCSS = document.createElement("style"); -mailgoCSS.id = "mailgo-style"; -mailgoCSS.type = "text/css"; -let mailgoCSSContent = document.createTextNode(`MAILGO_STYLE`); -mailgoCSS.appendChild(mailgoCSSContent); -document.head.appendChild(mailgoCSS); +if (windowExists()) { + // mailgo style (gulp) + let mailgoCSS = document.createElement("style"); + mailgoCSS.id = "mailgo-style"; + mailgoCSS.type = "text/css"; + let mailgoCSSContent = document.createTextNode(`MAILGO_STYLE`); + mailgoCSS.appendChild(mailgoCSSContent); + document.head.appendChild(mailgoCSS); +} /** * mailgoInit @@ -759,11 +761,14 @@ const mailgoKeydown = event => { return; }; -// DOMContentLoaded -> mailgoInit (creates the modals) -document.addEventListener("DOMContentLoaded", mailgoInit); +// if the window object exists... +if (windowExists()) { + // DOMContentLoaded -> mailgoInit (creates the modals) + document.addEventListener("DOMContentLoaded", mailgoInit); -// event listener on body, if the element is mailgo-compatible the mailgo modal will be rendered -document.addEventListener("click", mailgoCheckRender); + // event listener on body, if the element is mailgo-compatible the mailgo modal will be rendered + document.addEventListener("click", mailgoCheckRender); +} // show the modal const showMailgo = (type = MAIL_TYPE) => { @@ -808,6 +813,9 @@ const encodeEmail = email => btoa(email); // getE shorthand const getE = id => document.getElementById(id); +// window exists +const windowExists = () => window && typeof window !== "undefined"; + // get display value const getDisplay = id => getE(id).style.display;