1// è½»æç¤ºç»ä»¶ï¼åºé¨è¶åæµ®å±ï¼èæµé²æãèªå¨æ¶å¤±ãæ¯æä¸»å¨å ³éã 2// ç¨æ³ï¼ 3// window.showToast('æç¤ºææ¡') // æ¾ç¤º 2000ms åèªå¨æ¶å¤± 4// window.hideToast() // 主å¨å ³é 5// window.showToast('æç¤ºææ¡', 3000) // èªå®ä¹æ¾ç¤ºæ¶é¿ï¼msï¼ 6;(function () { 7 var DEFAULT_DURATION = 2000 8 var FADE_DURATION = 250 9 10 var toastEl = null 11 var textEl = null 12 var toastLock = false 13 var showTimer = null 14 var hideTimer = null 15 var unlockTimer = null 16 17 // æå è½½æ¥æ¾ DOMï¼é¿å å¨ partial æªå¼å ¥æ¶æ¥éã 18 function ensureNodes() { 19 if (toastEl) return toastEl 20 toastEl = document.querySelector('.js-site-toast') 21 if (!toastEl) return null 22 textEl = toastEl.querySelector('.js-site-toast-text') 23 return toastEl 24 } 25 26 // æ¸ çææå®æ¶å¨ï¼ä¾¿äºé坿䏻å¨å ³éã 27 function clearTimers() { 28 if (showTimer) { clearTimeout(showTimer); showTimer = null } 29 if (hideTimer) { clearTimeout(hideTimer); hideTimer = null } 30 if (unlockTimer) { clearTimeout(unlockTimer); unlockTimer = null } 31 } 32 33 // 主å¨å ³éï¼ç«å³è§¦åæ·¡åºï¼å¹¶å¨è¿æ¸¡ç»æåè§£éã 34 window.hideToast = function () { 35 if (!toastEl) return 36 clearTimers() 37 toastEl.classList.remove('is-active') 38 unlockTimer = setTimeout(function () { 39 toastLock = false 40 }, FADE_DURATION) 41 } 42 43 /** 44 * æ¾ç¤ºè½»æç¤ºã 45 * @param {string} text æç¤ºææ¡ï¼ä¸ºç©ºæ¶ä½¿ç¨é»è®¤ææ¡ã 46 * @param {number} duration æ¾ç¤ºæ¶é¿ï¼msï¼ï¼é»è®¤ 2000ã 47 */ 48 window.showToast = function (text, duration) { 49 if (!ensureNodes() || !textEl) return 50 // èæµï¼æç¤ºæ¾ç¤ºæé´å次è°ç¨ç´æ¥ä¸¢å¼ï¼é¿å éªçã 51 if (toastLock) return 52 toastLock = true 53 54 clearTimers() 55 56 // ç¨ textContent é² XSSï¼undefined æ¶ç»é»è®¤ææ¡ã 57 textEl.textContent = (text === undefined || text === null || text === '') 58 ? 'Loading, please wait...' 59 : String(text) 60 61 // ä¸ä¸å¸§æ¿æ´»ï¼ç¡®ä¿è¿æ¸¡çæã 62 window.requestAnimationFrame(function () { 63 toastEl.classList.add('is-active') 64 }) 65 66 var showDuration = (typeof duration === 'number' && duration > 0) ? duration : DEFAULT_DURATION 67 68 // å°ç¹æ·¡åºã 69 hideTimer = setTimeout(function () { 70 toastEl.classList.remove('is-active') 71 }, showDuration) 72 73 // è¿æ¸¡ç»æåè§£éå¹¶æ¸ çã 74 unlockTimer = setTimeout(function () { 75 toastLock = false 76 }, showDuration + FADE_DURATION) 77 } 78})()
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.