Skip to content

Instantly share code, notes, and snippets.

@coo11
Forked from superRaytin/simpleToast.js
Last active July 21, 2024 19:45
Show Gist options
  • Select an option

  • Save coo11/785f399908e619a3b47623ad6cfad048 to your computer and use it in GitHub Desktop.

Select an option

Save coo11/785f399908e619a3b47623ad6cfad048 to your computer and use it in GitHub Desktop.
Simple Toast
function toast(text, duration) {
window.simpleToastTimer && clearTimeout(window.simpleToastTimer);
const wrapStyle =
"display: flex; justify-content: center; padding: 0.6rem 1rem; font-size: 1.5rem; color: #fff; background: rgba(0, 0, 0, 0.7); border-radius: 0.4rem; position: fixed; top: 50%; left: 50%; z-index: 100; transform: translate3d(-50%, -50%, 1px);",
contentStyle = "max-width: calc(96vw - 2rem); overflow-wrap: break-word;",
toastEleId = "__GM_toast",
toastInnerHTML = `<div style="${wrapStyle}"><div style="${contentStyle}">${text}</div></div>`;
let toastEl = document.getElementById(toastEleId);
if (toastEl) toastEl.innerHTML = toastInnerHTML;
else {
toastEl = document.createElement("div");
toastEl.setAttribute("id", toastEleId);
toastEl.innerHTML = toastInnerHTML;
document.body.appendChild(toastEl);
}
window.simpleToastTimer = setTimeout(() => {
toastEl.parentNode.removeChild(toastEl);
}, duration || 2e3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment