-
-
Save coo11/785f399908e619a3b47623ad6cfad048 to your computer and use it in GitHub Desktop.
Simple Toast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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