mailymaily/src/mailgo.ts

856 lines
24 KiB
TypeScript
Raw Normal View History

2020-04-08 13:18:35 +00:00
const mailgoVersion: string = "MAILGO_VERSION";
2019-10-27 13:05:42 +00:00
2020-04-24 16:01:25 +00:00
type MailgoConfig = {};
const mailgo = (config?: MailgoConfig): void => {
2019-10-27 13:05:42 +00:00
// links
2020-04-07 15:21:47 +00:00
const MAILTO: string = "mailto:";
const TEL: string = "tel:";
const CALLTO: string = "callto:";
2019-10-27 13:05:42 +00:00
// mailgo types
2020-04-07 15:21:47 +00:00
const MAIL_TYPE: string = "mail";
const TEL_TYPE: string = "tel";
2019-10-27 13:05:42 +00:00
// default href for links
2020-04-07 15:21:47 +00:00
const DEFAULT_BTN_HREF: string = "javascript:void(0);";
2019-10-27 13:05:42 +00:00
2020-01-14 14:16:01 +00:00
// html tags
2020-04-07 15:21:47 +00:00
const span: string = "span";
2020-01-14 14:16:01 +00:00
2019-10-27 13:05:42 +00:00
// mailgo variables
2020-04-08 13:07:48 +00:00
let url: URL,
2020-04-07 15:21:47 +00:00
mail: string = "",
encEmail: string = "",
cc: string = "",
bcc: string = "",
subject: string = "",
bodyMail: string = "";
2019-10-27 13:05:42 +00:00
// mailgo tel variables
2020-04-07 15:21:47 +00:00
let tel: string = "",
msg: string = "",
telegramUsername: string = "",
skypeUsername: string = "";
2019-10-27 13:05:42 +00:00
2019-10-28 18:17:47 +00:00
// the DOM elements
2020-04-07 15:21:47 +00:00
let title: HTMLElement,
titleTel: HTMLElement,
detailCc: HTMLElement,
detailBcc: HTMLElement,
detailSubject: HTMLElement,
detailBody: HTMLElement,
ccValue: HTMLElement,
bccValue: HTMLElement,
subjectValue: HTMLElement,
bodyValue: HTMLElement;
2019-10-28 18:17:47 +00:00
2019-10-28 20:03:22 +00:00
// mailgo buttons (actions)
2020-04-08 13:07:48 +00:00
let gmail: HTMLLinkElement,
outlook: HTMLLinkElement,
open: HTMLLinkElement,
telegram: HTMLLinkElement,
wa: HTMLLinkElement,
skype: HTMLLinkElement,
call: HTMLLinkElement,
copyMail: HTMLLinkElement,
copyTel: HTMLLinkElement;
2019-10-27 13:05:42 +00:00
/**
* mailgoInit
* the function that creates the mailgo elements in DOM
*/
2020-04-08 13:18:35 +00:00
const mailgoInit = (): void => {
2019-10-27 13:05:42 +00:00
// mailgo mail
{
// modal
2020-01-14 14:16:01 +00:00
let modal = createElement();
2019-10-27 13:05:42 +00:00
modal.style.display = "none";
modal.id = "mailgo";
2019-10-28 20:16:33 +00:00
modal.classList.add("m-modal");
2019-10-27 13:05:42 +00:00
// background
2020-01-14 14:16:01 +00:00
let modalBackground = createElement();
2019-10-28 20:16:33 +00:00
modalBackground.className = "m-modal-back";
2020-04-08 13:07:48 +00:00
modal.appendChild(modalBackground);
2019-10-27 13:05:42 +00:00
// modal content
2020-01-14 14:16:01 +00:00
let modalContent = createElement();
2019-10-28 20:16:33 +00:00
modalContent.className = "m-modal-content";
2020-04-08 13:07:48 +00:00
modal.appendChild(modalContent);
2019-10-27 13:05:42 +00:00
// title (email address)
2019-11-07 08:20:14 +00:00
title = createElement("strong");
2019-10-28 20:16:33 +00:00
title.id = "m-title";
title.className = "m-title";
2020-04-08 13:07:48 +00:00
modalContent.appendChild(title);
2019-10-27 13:05:42 +00:00
// details
2020-01-14 14:16:01 +00:00
let details = createElement();
2019-10-28 20:16:33 +00:00
details.id = "m-details";
details.className = "m-details";
2019-10-27 13:05:42 +00:00
2019-11-07 08:20:14 +00:00
detailCc = createElement("p");
2019-10-28 20:16:33 +00:00
detailCc.id = "m-cc";
2020-01-14 14:16:01 +00:00
let ccSpan = createElement(span);
2019-10-28 19:57:33 +00:00
ccSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
ccSpan.appendChild(createTextNode("cc "));
2020-01-14 14:16:01 +00:00
ccValue = createElement(span);
2019-10-28 20:16:33 +00:00
ccValue.id = "m-cc-value";
2020-04-08 13:07:48 +00:00
detailCc.appendChild(ccSpan);
detailCc.appendChild(ccValue);
details.appendChild(detailCc);
2019-10-27 13:05:42 +00:00
2019-11-07 08:20:14 +00:00
detailBcc = createElement("p");
2019-10-28 20:16:33 +00:00
detailBcc.id = "m-bcc";
2020-01-14 14:16:01 +00:00
let bccSpan = createElement(span);
2019-10-28 19:57:33 +00:00
bccSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
bccSpan.appendChild(createTextNode("bcc "));
2020-01-14 14:16:01 +00:00
bccValue = createElement(span);
2019-10-28 20:16:33 +00:00
bccValue.id = "m-bcc-value";
2020-04-08 13:07:48 +00:00
detailBcc.appendChild(bccSpan);
detailBcc.appendChild(bccValue);
details.appendChild(detailBcc);
2019-10-27 13:05:42 +00:00
2019-11-07 08:20:14 +00:00
detailSubject = createElement("p");
2019-10-28 20:16:33 +00:00
detailSubject.id = "m-subject";
2020-01-14 14:16:01 +00:00
let subjectSpan = createElement(span);
2019-10-28 19:57:33 +00:00
subjectSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
subjectSpan.appendChild(createTextNode("subject "));
2020-01-14 14:16:01 +00:00
subjectValue = createElement(span);
2019-10-28 20:16:33 +00:00
subjectValue.id = "m-subject-value";
2020-04-08 13:07:48 +00:00
detailSubject.appendChild(subjectSpan);
detailSubject.appendChild(subjectValue);
details.appendChild(detailSubject);
2019-10-27 13:05:42 +00:00
2019-11-07 08:20:14 +00:00
detailBody = createElement("p");
2019-10-28 20:16:33 +00:00
detailBody.id = "m-body";
2020-01-14 14:16:01 +00:00
let bodySpan = createElement(span);
2019-10-28 19:57:33 +00:00
bodySpan.className = "w-500";
2020-04-08 13:07:48 +00:00
bodySpan.appendChild(createTextNode("body "));
2020-01-14 14:16:01 +00:00
bodyValue = createElement(span);
2019-10-28 20:16:33 +00:00
bodyValue.id = "m-body-value";
2020-04-08 13:07:48 +00:00
detailBody.appendChild(bodySpan);
detailBody.appendChild(bodyValue);
details.appendChild(detailBody);
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(details);
2019-10-27 13:05:42 +00:00
// Gmail
2020-04-08 13:07:48 +00:00
gmail = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
gmail.id = "m-gmail";
2019-10-27 13:05:42 +00:00
gmail.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
gmail.classList.add("m-open");
gmail.classList.add("m-gmail");
2020-04-08 13:07:48 +00:00
gmail.appendChild(createTextNode("open in "));
2020-01-14 14:16:01 +00:00
let gmailSpan = createElement(span);
2019-10-28 19:57:33 +00:00
gmailSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
gmailSpan.appendChild(createTextNode("Gmail"));
gmail.appendChild(gmailSpan);
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(gmail);
2019-10-27 13:05:42 +00:00
// Outlook
2020-04-08 13:07:48 +00:00
outlook = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
outlook.id = "m-outlook";
2019-10-27 13:05:42 +00:00
outlook.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
outlook.classList.add("m-open");
outlook.classList.add("m-outlook");
2020-04-08 13:07:48 +00:00
outlook.appendChild(createTextNode("open in "));
2020-01-14 14:16:01 +00:00
let outlookSpan = createElement(span);
2019-10-28 19:57:33 +00:00
outlookSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
outlookSpan.appendChild(createTextNode("Outlook"));
outlook.appendChild(outlookSpan);
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(outlook);
2019-10-27 13:05:42 +00:00
// open default
2020-04-08 13:07:48 +00:00
open = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
open.id = "m-open";
2019-10-27 13:05:42 +00:00
open.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
open.classList.add("m-open");
open.classList.add("m-default");
2020-01-14 14:16:01 +00:00
let openSpan = createElement(span);
2019-10-28 19:57:33 +00:00
openSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
openSpan.appendChild(createTextNode("open"));
open.appendChild(openSpan);
open.appendChild(createTextNode(" default"));
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(open);
2019-10-27 13:05:42 +00:00
// copy
2020-04-08 13:07:48 +00:00
copyMail = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
copyMail.id = "m-copy";
2019-10-28 18:17:47 +00:00
copyMail.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
copyMail.classList.add("m-copy");
2019-10-28 19:57:33 +00:00
copyMail.classList.add("w-500");
2020-04-08 13:07:48 +00:00
copyMail.appendChild(createTextNode("copy"));
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(copyMail);
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(byElement());
2019-10-27 13:05:42 +00:00
// add the modal at the end of the body
2020-04-08 13:07:48 +00:00
document.body.appendChild(modal);
2019-10-27 13:05:42 +00:00
// every click outside the modal will hide the modal
modalBackground.addEventListener("click", hideMailgo);
2019-09-16 21:06:40 +00:00
}
2019-10-27 13:05:42 +00:00
// mailgo tel
{
// modal
2020-01-14 14:16:01 +00:00
let modal = createElement();
2019-10-27 13:05:42 +00:00
modal.style.display = "none";
modal.id = "mailgo-tel";
2019-10-28 20:16:33 +00:00
modal.classList.add("m-modal");
2019-10-27 13:05:42 +00:00
// background
2020-01-14 14:16:01 +00:00
let modalBackground = createElement();
2019-10-28 20:16:33 +00:00
modalBackground.className = "m-modal-back";
2020-04-08 13:07:48 +00:00
modal.appendChild(modalBackground);
2019-10-27 13:05:42 +00:00
// modal content
2020-01-14 14:16:01 +00:00
let modalContent = createElement();
2019-10-28 20:16:33 +00:00
modalContent.className = "m-modal-content";
2020-04-08 13:07:48 +00:00
modal.appendChild(modalContent);
2019-10-27 13:05:42 +00:00
// title (telephone number)
2019-11-07 08:20:14 +00:00
titleTel = createElement("strong");
2019-10-28 20:16:33 +00:00
titleTel.id = "m-tel-title";
titleTel.className = "m-title";
2020-04-08 13:07:48 +00:00
modalContent.appendChild(titleTel);
2019-10-27 13:05:42 +00:00
// Telegram
2020-04-08 13:07:48 +00:00
telegram = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
telegram.id = "m-tg";
2019-10-27 13:05:42 +00:00
telegram.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
telegram.classList.add("m-open");
telegram.classList.add("m-tg");
2019-10-27 13:05:42 +00:00
// by default not display
telegram.style.display = "none";
2020-04-08 13:07:48 +00:00
telegram.appendChild(createTextNode("open in "));
2020-01-14 14:16:01 +00:00
let telegramSpan = createElement(span);
2019-10-28 19:57:33 +00:00
telegramSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
telegramSpan.appendChild(createTextNode("Telegram"));
telegram.appendChild(telegramSpan);
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(telegram);
2019-10-27 13:05:42 +00:00
// WhatsApp
2020-04-08 13:07:48 +00:00
wa = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
wa.id = "m-wa";
2019-10-27 13:05:42 +00:00
wa.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
wa.classList.add("m-open");
wa.classList.add("m-wa");
2020-04-08 13:07:48 +00:00
wa.appendChild(createTextNode("open in "));
2020-01-14 14:16:01 +00:00
let waSpan = createElement(span);
2019-10-28 19:57:33 +00:00
waSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
waSpan.appendChild(createTextNode("WhatsApp"));
wa.appendChild(waSpan);
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(wa);
2019-10-27 13:05:42 +00:00
// Skype
2020-04-08 13:07:48 +00:00
skype = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
skype.id = "m-skype";
2019-10-27 13:05:42 +00:00
skype.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
skype.classList.add("m-open");
skype.classList.add("m-skype");
2020-04-08 13:07:48 +00:00
skype.appendChild(createTextNode("open in "));
2020-01-14 14:16:01 +00:00
let skypeSpan = createElement(span);
2019-10-28 19:57:33 +00:00
skypeSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
skypeSpan.appendChild(createTextNode("Skype"));
skype.appendChild(skypeSpan);
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(skype);
2019-10-27 13:05:42 +00:00
// call default
2020-04-08 13:07:48 +00:00
call = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
call.id = "m-call";
2019-10-27 13:05:42 +00:00
call.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
call.classList.add("m-open");
call.classList.add("m-default");
2020-01-14 14:16:01 +00:00
let callSpan = createElement(span);
2019-10-28 19:57:33 +00:00
callSpan.className = "w-500";
2020-04-08 13:07:48 +00:00
callSpan.appendChild(createTextNode("call"));
call.appendChild(callSpan);
call.appendChild(createTextNode(" as default"));
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(call);
2019-10-27 13:05:42 +00:00
// copy
2020-04-08 13:07:48 +00:00
copyTel = <HTMLLinkElement>createElement("a");
2019-10-28 20:16:33 +00:00
copyTel.id = "m-tel-copy";
2019-10-28 18:17:47 +00:00
copyTel.href = DEFAULT_BTN_HREF;
2019-10-28 20:16:33 +00:00
copyTel.classList.add("m-copy");
2019-10-28 19:57:33 +00:00
copyTel.classList.add("w-500");
2020-04-08 13:07:48 +00:00
copyTel.appendChild(createTextNode("copy"));
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(copyTel);
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
modalContent.appendChild(byElement());
2019-10-27 13:05:42 +00:00
// add the modal at the end of the body
2020-04-08 13:07:48 +00:00
document.body.appendChild(modal);
2019-10-27 13:05:42 +00:00
// every click outside the modal will hide the modal
modalBackground.addEventListener("click", hideMailgo);
}
};
/**
* mailgoRender
* function to render a mailgo (mail or tel)
*/
2020-04-08 13:07:48 +00:00
const mailgoRender = (type = MAIL_TYPE, mailgo: any) => {
2019-10-27 13:05:42 +00:00
// mailgo mail
if (type === MAIL_TYPE) {
// if the element href=^"mailto:"
if (mailgo.href && mailgo.href.toLowerCase().startsWith(MAILTO)) {
mail = decodeURIComponent(
2020-04-07 15:21:47 +00:00
mailgo.href.split("?")[0].split(MAILTO)[1].trim()
2019-10-27 13:05:42 +00:00
);
url = new URL(mailgo.href);
2020-04-08 13:07:48 +00:00
let urlParams: URLSearchParams = url.searchParams;
2019-10-27 13:05:42 +00:00
// optional parameters for the email
cc = urlParams.get("cc");
bcc = urlParams.get("bcc");
subject = urlParams.get("subject");
bodyMail = urlParams.get("body");
} else {
// if the element href="#mailgo" or class="mailgo"
// mail = data-address + @ + data-domain
mail =
mailgo.getAttribute("data-address") +
"@" +
mailgo.getAttribute("data-domain");
url = new URL(MAILTO + encodeURIComponent(mail));
// cc = data-cc-address + @ + data-cc-domain
cc =
mailgo.getAttribute("data-cc-address") +
"@" +
mailgo.getAttribute("data-cc-domain");
// bcc = data-bcc-address + @ + data-bcc-domain
bcc =
mailgo.getAttribute("data-bcc-address") +
"@" +
mailgo.getAttribute("data-bcc-domain");
// subject = data-subject
subject = mailgo.getAttribute("data-subject");
// body = data-body
bodyMail = mailgo.getAttribute("data-body");
}
2019-10-27 13:05:42 +00:00
// validate the email address
if (!validateEmails(mail.split(","))) return;
// if cc, bcc is not valid cc, bcc = ""
if (cc && !validateEmails(cc.split(","))) cc = "";
if (bcc && !validateEmails(bcc.split(","))) bcc = "";
// the title of the modal (email address)
2019-10-28 18:17:47 +00:00
title.innerHTML = mail.split(",").join("<br/>");
2019-10-27 13:05:42 +00:00
// add the details if provided
cc
2019-10-28 18:17:47 +00:00
? ((detailCc.style.display = "block"),
(ccValue.innerHTML = cc.split(",").join("<br/>")))
: (detailCc.style.display = "none");
2019-10-27 13:05:42 +00:00
bcc
2019-10-28 18:17:47 +00:00
? ((detailBcc.style.display = "block"),
(bccValue.innerHTML = bcc.split(",").join("<br/>")))
: (detailBcc.style.display = "none");
2019-10-27 13:05:42 +00:00
subject
2019-10-28 18:17:47 +00:00
? ((detailSubject.style.display = "block"),
(subjectValue.textContent = subject))
: (detailSubject.style.display = "none");
2019-10-27 13:05:42 +00:00
bodyMail
2019-10-28 18:17:47 +00:00
? ((detailBody.style.display = "block"),
(bodyValue.textContent = bodyMail))
: (detailBody.style.display = "none");
2019-10-27 13:05:42 +00:00
// add the actions
2019-10-28 18:17:47 +00:00
gmail.addEventListener("click", openGmail);
2019-10-27 13:05:42 +00:00
2019-10-28 18:17:47 +00:00
outlook.addEventListener("click", openOutlook);
2019-10-27 13:05:42 +00:00
encEmail = encodeEmail(mail);
2019-10-28 18:17:47 +00:00
open.addEventListener("click", openDefault);
2019-10-27 13:05:42 +00:00
2019-10-28 18:17:47 +00:00
copyMail.addEventListener("click", () => copy(mail));
2019-09-16 21:06:40 +00:00
}
2019-10-27 13:05:42 +00:00
// mailgo tel
if (type === TEL_TYPE) {
if (mailgo.href && mailgo.href.toLowerCase().startsWith(TEL)) {
tel = decodeURIComponent(
2020-04-07 15:21:47 +00:00
mailgo.href.split("?")[0].split(TEL)[1].trim()
2019-10-27 13:05:42 +00:00
);
} else if (mailgo.href && mailgo.href.toLowerCase().startsWith(CALLTO)) {
tel = decodeURIComponent(
2020-04-07 15:21:47 +00:00
mailgo.href.split("?")[0].split(CALLTO)[1].trim()
2019-10-27 13:05:42 +00:00
);
} else if (mailgo.hasAttribute("data-tel")) {
tel = mailgo.getAttribute("data-tel");
msg = mailgo.getAttribute("data-msg");
}
2019-07-19 10:03:21 +00:00
2019-10-27 13:05:42 +00:00
// information
2019-10-28 20:16:33 +00:00
// let titleEl = getE("m-tel-title");
2019-07-19 10:03:21 +00:00
2019-10-27 13:05:42 +00:00
// Telegram username
if (mailgo.hasAttribute("data-telegram")) {
telegramUsername = mailgo.getAttribute("data-telegram");
}
2019-10-27 13:05:42 +00:00
// Telegram username
if (mailgo.hasAttribute("data-skype")) {
skypeUsername = mailgo.getAttribute("data-skype");
}
// the title of the modal (tel)
2019-10-28 18:17:47 +00:00
titleTel.innerHTML = tel;
2019-10-27 13:05:42 +00:00
// add the actions to buttons
2019-10-28 18:17:47 +00:00
wa.addEventListener("click", openWhatsApp);
2019-10-27 13:05:42 +00:00
if (telegramUsername) {
2019-10-28 20:16:33 +00:00
setDisplay("m-tg", "block");
2019-10-28 18:17:47 +00:00
telegram.addEventListener("click", openTelegram);
2019-10-27 13:05:42 +00:00
}
2019-07-19 10:03:21 +00:00
2019-10-28 18:17:47 +00:00
skype.addEventListener("click", openSkype);
2019-07-19 10:03:21 +00:00
2019-10-28 18:17:47 +00:00
call.addEventListener("click", callDefault);
2019-10-28 18:17:47 +00:00
copyTel.addEventListener("click", () => copy(tel));
2019-09-19 10:31:42 +00:00
}
2019-10-27 13:05:42 +00:00
// show the mailgo
showMailgo(type);
2019-10-27 13:05:42 +00:00
// add listener keyDown
document.addEventListener("keydown", mailgoKeydown);
};
2019-09-16 22:23:41 +00:00
2019-10-27 13:05:42 +00:00
// actions
2019-10-28 07:40:22 +00:00
const openGmail = () => {
2019-10-28 07:36:59 +00:00
// Gmail url
let gmailUrl =
"https://mail.google.com/mail/u/0/?view=cm&source=mailto&to=" +
encodeURIComponent(mail);
// the details if provided
if (cc) gmailUrl = gmailUrl.concat("&cc=" + encodeURIComponent(cc));
if (bcc) gmailUrl = gmailUrl.concat("&bcc=" + encodeURIComponent(bcc));
if (subject) gmailUrl = gmailUrl.concat("&subject=" + subject);
if (bodyMail) gmailUrl = gmailUrl.concat("&body=" + bodyMail);
// open the link
window.open(gmailUrl, "_blank");
// hide the modal
hideMailgo();
};
2019-05-26 11:43:17 +00:00
2019-10-28 07:40:22 +00:00
const openOutlook = () => {
2019-10-28 07:36:59 +00:00
// Outlook url
let outlookUrl =
"https://outlook.live.com/owa/?path=/mail/action/compose&to=" +
encodeURIComponent(mail);
2019-09-13 20:29:20 +00:00
2019-10-28 07:36:59 +00:00
// the details if provided
if (subject) outlookUrl = outlookUrl.concat("&subject=" + subject);
if (bodyMail) outlookUrl = outlookUrl.concat("&body=" + bodyMail);
2019-09-13 20:29:20 +00:00
2019-10-28 07:36:59 +00:00
// open the link
window.open(outlookUrl, "_blank");
2019-10-27 13:05:42 +00:00
2019-10-28 07:36:59 +00:00
// hide the modal
hideMailgo();
};
2019-10-27 13:05:42 +00:00
2019-10-28 07:40:22 +00:00
const openDefault = () => {
2019-10-28 07:36:59 +00:00
mailToEncoded(encEmail);
hideMailgo();
};
2019-10-27 13:05:42 +00:00
2019-10-28 07:40:22 +00:00
const openTelegram = () => {
2019-10-28 07:36:59 +00:00
// Telegram url
let tgUrl = "https://t.me/" + telegramUsername;
2019-10-27 13:13:52 +00:00
2019-10-28 07:36:59 +00:00
// open the url
window.open(tgUrl, "_blank");
2019-10-27 13:13:52 +00:00
2019-10-28 07:36:59 +00:00
// hide the modal
hideMailgo();
};
2019-10-27 13:05:42 +00:00
2019-10-28 07:40:22 +00:00
const openSkype = () => {
2019-10-28 07:36:59 +00:00
let skype = skypeUsername !== "" ? skypeUsername : tel;
2019-10-27 13:13:52 +00:00
2019-10-28 07:36:59 +00:00
// Telegram url
let skypeUrl = "skype:" + skype;
2019-10-27 13:13:52 +00:00
2019-10-28 07:36:59 +00:00
// open the url
window.open(skypeUrl, "_blank");
2019-10-27 13:13:52 +00:00
2019-10-28 07:36:59 +00:00
// hide the modal
hideMailgo();
};
2019-10-27 13:05:42 +00:00
2019-10-28 07:40:22 +00:00
const openWhatsApp = () => {
2019-10-28 07:36:59 +00:00
// WhatsApp url
let waUrl = "https://wa.me/" + tel;
2019-10-27 13:13:52 +00:00
2019-10-28 07:36:59 +00:00
// the details if provided
if (msg) waUrl + "?text=" + msg;
2019-10-27 13:13:52 +00:00
2019-10-28 07:36:59 +00:00
// open the url
window.open(waUrl, "_blank");
2019-10-27 13:13:52 +00:00
2019-10-28 07:36:59 +00:00
// hide the modal
hideMailgo();
};
2019-10-28 07:40:22 +00:00
const callDefault = () => {
2019-10-28 07:36:59 +00:00
let callUrl = "tel:" + tel;
window.open(callUrl);
hideMailgo();
};
2019-10-27 13:05:42 +00:00
2020-04-08 13:07:48 +00:00
const copy = (content: string) => {
2019-10-28 07:36:59 +00:00
copyToClipboard(content);
2020-04-08 13:07:48 +00:00
let activeCopy: HTMLElement;
2019-10-28 07:36:59 +00:00
// the correct copyButton (mail or tel)
mailgoIsShowing(MAIL_TYPE)
2019-10-28 18:29:38 +00:00
? (activeCopy = copyMail)
: (activeCopy = copyTel);
activeCopy.textContent = "copied";
2019-10-28 07:36:59 +00:00
setTimeout(() => {
2019-10-28 18:29:38 +00:00
activeCopy.textContent = "copy";
2019-10-28 07:36:59 +00:00
// hide after the timeout
2019-10-27 13:05:42 +00:00
hideMailgo();
2019-10-28 07:36:59 +00:00
}, 999);
2019-10-27 13:05:42 +00:00
};
// function that returns if an element is a mailgo
2020-04-08 13:07:48 +00:00
const isMailgo = (element: HTMLElement, type: string = MAIL_TYPE) => {
let href: string = (element as HTMLLinkElement).href;
2019-10-27 13:05:42 +00:00
// mailgo type mail
if (type === MAIL_TYPE) {
return (
// first case: it is an <a> element with "mailto:..." in href and no no-mailgo in classList
2020-04-08 13:07:48 +00:00
(href &&
href.toLowerCase().startsWith(MAILTO) &&
2019-10-27 13:05:42 +00:00
!element.classList.contains("no-mailgo")) ||
(element.hasAttribute("data-address") &&
// second case: the href=#mailgo
2020-04-08 13:07:48 +00:00
((href && element.getAttribute("href").toLowerCase() === "#mailgo") ||
2019-10-27 13:05:42 +00:00
// third case: the classList contains mailgo
(element.classList && element.classList.contains("mailgo"))))
);
2019-09-16 21:34:57 +00:00
}
2019-10-27 13:05:42 +00:00
// mailgo type tel
if (type === TEL_TYPE) {
return (
// first case: it is an <a> element with "tel:..." or "callto:..." in href and no no-mailgo in classList
2020-04-08 13:07:48 +00:00
(href &&
(href.toLowerCase().startsWith(TEL) ||
href.toLowerCase().startsWith(CALLTO)) &&
2019-10-27 13:05:42 +00:00
!element.classList.contains("no-mailgo")) ||
2019-11-17 11:22:33 +00:00
(element.hasAttribute("data-tel") &&
2019-10-27 13:05:42 +00:00
// second case: the href=#mailgo
2020-04-08 13:07:48 +00:00
href &&
2019-11-17 11:22:33 +00:00
element.getAttribute("href").toLowerCase() === "#mailgo") ||
// third case: the classList contains mailgo
(element.classList && element.classList.contains("mailgo"))
2019-10-27 13:05:42 +00:00
);
}
return false;
};
/**
* mailgoCheckRender
* function to check if an element is mailgo-enabled or not referencing to
* mail:
* document.querySelectorAll(
* 'a[href^="mailto:" i]:not(.no-mailgo), a[href="#mailgo"], a.mailgo'
* );
* tel:
* document.querySelectorAll(
* 'a[href^="tel:" i]:not(.no-mailgo), a[href="#mailgo"], a.mailgo'
* );
* or
* document.querySelectorAll(
* 'a[href^="callto:" i]:not(.no-mailgo), a[href="#mailgo"], a.mailgo'
* );
*/
2020-04-08 13:07:48 +00:00
const mailgoCheckRender = (event: Event) => {
2019-10-27 13:05:42 +00:00
// check if the id=mailgo exists in the body
if (
!document.contains(getE("mailgo")) ||
!document.contains(getE("mailgo-tel"))
)
return;
// if a mailgo is already showing do nothing
if (mailgoIsShowing(MAIL_TYPE) || mailgoIsShowing(TEL_TYPE)) return;
// the path of the event
let path =
(event.composedPath && event.composedPath()) ||
2020-04-08 13:07:48 +00:00
composedPath(event.target as HTMLElement);
2019-10-27 13:05:42 +00:00
if (path) {
2020-04-07 15:21:47 +00:00
path.forEach((element: HTMLElement) => {
2019-10-27 13:05:42 +00:00
if (element instanceof HTMLDocument || element instanceof Window)
return;
// go in the event.path to find if the user has clicked on a mailgo element
if (isMailgo(element, MAIL_TYPE)) {
// stop the normal execution of the element click
event.preventDefault();
// render mailgo
mailgoRender(MAIL_TYPE, element);
return;
}
if (isMailgo(element, TEL_TYPE)) {
// stop the normal execution of the element click
event.preventDefault();
// render mailgo
mailgoRender(TEL_TYPE, element);
return;
}
});
}
return;
2019-10-27 13:05:42 +00:00
};
/**
* mailgoKeydown
* function to manage the keydown event when the modal is showing
*/
2020-04-07 15:21:47 +00:00
const mailgoKeydown = (event: KeyboardEvent) => {
2019-10-27 13:05:42 +00:00
// if mailgo is showing
if (mailgoIsShowing(MAIL_TYPE)) {
switch (event.keyCode) {
case 27:
// Escape
hideMailgo();
break;
case 71:
// g -> open GMail
2019-10-28 07:36:59 +00:00
openGmail();
2019-10-27 13:05:42 +00:00
break;
case 79:
// o -> open Outlook
2019-10-28 07:36:59 +00:00
openOutlook();
2019-10-27 13:05:42 +00:00
break;
case 32:
case 13:
// spacebar or enter -> open default
2019-10-28 07:36:59 +00:00
openDefault();
2019-10-27 13:05:42 +00:00
break;
case 67:
// c -> copy
2019-10-28 07:36:59 +00:00
copy(mail);
2019-10-27 13:05:42 +00:00
break;
default:
return;
}
} else if (mailgoIsShowing(TEL_TYPE)) {
switch (event.keyCode) {
case 27:
// Escape
hideMailgo();
break;
case 84:
// t -> open Telegram
2019-10-28 07:36:59 +00:00
openTelegram();
2019-10-27 13:05:42 +00:00
break;
case 87:
// w -> open WhatsApp
2019-10-28 07:36:59 +00:00
openWhatsApp();
2019-10-27 13:05:42 +00:00
break;
case 32:
case 13:
// spacebar or enter -> call default
2019-10-28 07:36:59 +00:00
callDefault();
2019-10-27 13:05:42 +00:00
break;
case 67:
// c -> copy
2019-10-28 07:36:59 +00:00
copy(tel);
2019-10-27 13:05:42 +00:00
break;
default:
return;
}
}
return;
2019-10-27 13:05:42 +00:00
};
// show the modal
const showMailgo = (type = MAIL_TYPE) => {
// show mailgo type mail
if (type === MAIL_TYPE) {
setDisplay("mailgo", "flex");
return;
2019-05-26 11:43:17 +00:00
}
2019-10-27 13:05:42 +00:00
// show mailgo type tel
if (type === TEL_TYPE) {
setDisplay("mailgo-tel", "flex");
return;
}
};
// hide the modal
const hideMailgo = () => {
setDisplay("mailgo", "none");
setDisplay("mailgo-tel", "none");
// remove listener keyDown
document.removeEventListener("keydown", mailgoKeydown);
};
// is the mailgo modal hidden?
const mailgoIsShowing = (type = MAIL_TYPE) => {
2019-10-28 10:17:33 +00:00
return type === MAIL_TYPE
? getDisplay("mailgo") === "flex"
: type === TEL_TYPE
? getDisplay("mailgo-tel") === "flex"
: false;
2019-10-27 13:05:42 +00:00
};
2019-10-28 20:03:22 +00:00
const byElement = () => {
// by
2020-04-08 13:07:48 +00:00
let by: HTMLLinkElement = <HTMLLinkElement>createElement("a");
2019-10-28 20:03:22 +00:00
by.href = "https://mailgo.js.org?ref=mailgo-modal";
2019-10-28 20:16:33 +00:00
by.className = "m-by";
2019-10-28 20:03:22 +00:00
by.target = "_blank";
by.rel = "noopener noreferrer";
2020-04-08 13:07:48 +00:00
by.appendChild(createTextNode("mailgo.js.org"));
2019-10-28 20:03:22 +00:00
return by;
};
2019-11-07 08:20:14 +00:00
// create element
2020-04-08 13:07:48 +00:00
const createElement = (element: string = "div") =>
document.createElement(element);
2019-11-07 08:20:14 +00:00
// create text node
2020-04-08 13:07:48 +00:00
const createTextNode = (element: string) => document.createTextNode(element);
2019-11-07 08:20:14 +00:00
2019-10-27 13:05:42 +00:00
// decrypt email
2020-04-07 15:21:47 +00:00
const mailToEncoded = (encoded: string) =>
2019-10-27 13:05:42 +00:00
(window.location.href = MAILTO + atob(encoded));
// encode email
2020-04-08 13:07:48 +00:00
const encodeEmail = (email: string) => btoa(email);
2019-10-27 13:05:42 +00:00
// getE shorthand
2020-04-08 13:07:48 +00:00
const getE = (id: string) => document.getElementById(id);
2019-10-27 13:05:42 +00:00
// get display value
2020-04-08 13:07:48 +00:00
const getDisplay = (id: string) => getE(id).style.display;
2019-10-27 13:05:42 +00:00
// get display value
2020-04-08 13:07:48 +00:00
const setDisplay = (id: string, value: string) =>
(getE(id).style.display = value);
2019-05-26 11:43:17 +00:00
2019-10-27 13:05:42 +00:00
// custom composedPath if path or event.composedPath() are not defined
2020-04-08 13:07:48 +00:00
const composedPath = (el: HTMLElement) => {
2019-10-27 13:05:42 +00:00
let path = [];
while (el) {
path.push(el);
if (el.tagName === "HTML") {
path.push(document);
path.push(window);
return path;
}
el = el.parentElement;
}
};
// validate a single email with regex
2020-04-08 13:07:48 +00:00
const validateEmail = (email: string) =>
2019-10-28 18:18:40 +00:00
/^(([^<>()[\]\\.,;:\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(
email
);
2019-10-27 13:05:42 +00:00
// validate an array of emails
2020-04-08 13:07:48 +00:00
const validateEmails = (arr: string[]) => arr.every(validateEmail);
2019-10-27 13:05:42 +00:00
// copy of a string
2020-04-08 13:07:48 +00:00
const copyToClipboard = (str: string) => {
let el: HTMLInputElement = <HTMLInputElement>createElement("textarea");
2019-10-27 13:05:42 +00:00
el.value = str;
el.setAttribute("readonly", "");
el.style.position = "absolute";
el.style.left = "-9999px";
2020-04-08 13:07:48 +00:00
document.body.appendChild(el);
2019-10-27 13:05:42 +00:00
let selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false;
el.select();
document.execCommand("copy");
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
// start mailgo
(() => {
// if the window is defined...
if (window && typeof window !== "undefined") {
// if the window object exists...
// mailgo style (gulp)
let mailgoCSS: HTMLStyleElement = <HTMLStyleElement>(
createElement("style")
);
mailgoCSS.id = "mailgo-style";
mailgoCSS.type = "text/css";
mailgoCSS.appendChild(createTextNode(`MAILGO_STYLE`));
document.head.appendChild(mailgoCSS);
// 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);
}
2020-04-24 10:00:23 +00:00
})();
};
2019-10-27 13:05:42 +00:00
2020-04-24 10:14:26 +00:00
// if mailgo is included as a script
2020-04-24 10:00:23 +00:00
if (window && typeof window !== "undefined") {
mailgo();
}
2019-10-27 13:05:42 +00:00
2020-04-24 10:14:26 +00:00
// export mailgo
2020-04-24 13:34:10 +00:00
export default mailgo;