// 显示吐司提示。 // _html:要显示的内容。 // _duration:持续时间,单位毫秒,不指定时默认 2000 毫秒。 window.toast = function (_html, _duration) { if (document.getElementsByClassName) { var shown = document.getElementsByClassName("global-toast") for (var i = 0; i < shown.length; i++) { try { document.body.removeChild(shown[i]) } catch (error) { } } } if (typeof _html !== "string" || _html === "") return var box = document.createElement("span") var area = document.createElement("div") box.style.backdropFilter = "blur(5px)" box.style.backgroundColor = "#3f3f3f" box.style.borderRadius = "5px" box.style.boxShadow = "0px 10px 40px rgba(0,0,0, 0.3)" box.style.color = "white" box.style.display = "inline-block" box.style.fontFamily = "consolas, \"microsoft yahei\", \"pingfang sc\", sans-serif" box.style.fontSize = "14px" box.style.lineHeight = "28px" box.style.paddingBottom = "10px" box.style.paddingLeft = "20px" box.style.paddingRight = "20px" box.style.paddingTop = "10px" box.style.textShadow = "0px 0px 10px rgba(0,0,0, 0.5)" box.innerHTML = _html area.className = "elivo.toast" area.style.bottom = "20%" area.style.display = "block" area.style.fontSize = "0px" area.style.left = "10%" area.style.opacity = "0" area.style.position = "fixed" area.style.right = "10%" area.style.textAlign = "center" area.style.transition = "all 0.15s linear" area.style.zIndex = "200000000" area.appendChild(box) document.body.appendChild(area) setTimeout(function () { area.style.opacity = 1 var delay = 150 + parseInt((typeof _duration === "number") ? _duration : 2000) setTimeout(function () { area.style.opacity = 0 setTimeout(function () { try { document.body.removeChild(area) } catch (error) { } }, 300) }, delay) }, 1) return { area: area, box: box } } // 获取当前时钟的易读格式。 window.clock = function (date) { if (!date || !(date instanceof Date)) date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); var milli = date.getMilliseconds(); year = (year < 10 ? "000" : (year < 100 ? "00" : (year < 1000 ? "0" : ""))) + String(year); month = (month < 10 ? "0" : "") + String(month); day = (day < 10 ? "0" : "") + String(day); hour = (hour < 10 ? "0" : "") + String(hour); minute = (minute < 10 ? "0" : "") + String(minute); second = (second < 10 ? "0" : "") + String(second); milli = (milli < 10 ? "00" : (milli < 100 ? "0" : "")) + String(milli); return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "." + milli; }; // 闪烁颜色。 window.flickColor = function (color, duration) { var mask = document.getElementById("global-flick"); if (mask && mask.parentElement) mask.parentElement.removeChild(mask); if (!duration) return; mask = document.createElement("div"); mask.id = "global-flick"; mask.style.position = "fixed"; mask.style.zIndex = "10000"; mask.style.top = mask.style.right = mask.style.bottom = mask.style.left = "0px"; mask.style.backgroundColor = color; mask.style.opacity = "0.5"; document.body.appendChild(mask); setTimeout(function () { mask.style.transition = "opacity " + (duration / 1000.0).toFixed(2) + "s linear"; mask.style.opacity = "0"; }, 200); if (duration > 0) { setTimeout(function () { if (mask && mask.parentElement) mask.parentElement.removeChild(mask); }, 200 + duration); } mask.style.pointerEvents = "none"; }; // 闪烁绿屏。 window.flickGreen = function (ms) { if (!ms) ms = 400; window.flickColor("limegreen", ms); }; // 闪烁红屏。 window.flickRed = function (ms) { if (!ms) ms = 400; window.flickColor("crimson", ms); }; // 修正数字,保留指定的小数位数。 window.trimNumber = function (number, digits) { var n = number; if (typeof n === "string") n = parseFloat(n); if (typeof n !== "number") return n; if (typeof digits !== "number") digits = 4; else if (digits < 1) return Math.round(n); var p = Math.pow(10, digits); n = Math.round(n * p) / p; var t = n.toFixed(digits); n = parseFloat(t); return n; }; // 对数组排序。 window.isArray = function (value) { if (typeof Array.isArray === "function") { return Array.isArray(value); } else { return Object.prototype.toString.call(value) === "[object Array]"; }; }; window.saveAs = function (content, name, bom) { if (!content) return; var blob; if (content instanceof Blob) { var blob = content; if (bom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) { blob = new Blob([String.fromCharCode(0xFEFF), blob], { type: blob.type }); }; } else { blob = new Blob([content], { type: "application/octet-stream" }); }; var url = (window.URL || window.webkitURL).createObjectURL(blob); setTimeout(function () { if (typeof url == "string") (window.URL || window.webkitURL).revokeObjectURL(url); else file.remove(); }, 10000); // IE 10+ if (navigator.msSaveOrOpenBlob) return navigator.msSaveOrOpenBlob(blob, name); // emulate a tag var link = document.createElementNS("http://www.w3.org/1999/xhtml", "a"); if ("download" in link) { link.href = url; link.download = name; var click = new MouseEvent("click"); link.dispatchEvent(click); return; }; // popup if (!window.open(url, "_blank")) window.location.href = url; }; window.toFloat = function (number) { var input = number; var hasPercent = false; if (typeof input == "string") { input = input.replace(/,/gm, "").replace(/\ /gm, ""); if (input.indexOf("%") > -1) { hasPercent = true; input = input.replace(/%/gm, ""); } } var value = parseFloat(input); if (isNaN(value)) return 0.0; if (!isFinite(value)) return 0.0; if (hasPercent) value = value / 100.0; return value; }; window.toInt = function (number) { if (typeof number == "number") { if (isNaN(number) || !isFinite(number)) number = 0; return number; } var n = parseInt(String(number)); if (isNaN(n) || !isFinite(n)) n = 0; return n; }; window.uuid = function (hyphenation) { var array = []; var hex = "0123456789abcdef"; for (var i = 0; i < 36; i++) array[i] = hex.substr(Math.floor(Math.random() * 16), 1); array[14] = "4"; array[19] = hex.substr((array[19] & 0x3) | 0x8, 1); array[8] = array[13] = array[18] = array[23] = "-"; var result = array.join(""); if (hyphenation !== true) result = result.replace(/-/gm, ""); return result; }; // 从指定字符串中获取参数。 window.urlQuery = function (selector, decode) { var combined = window.location.search; if (!combined) return ""; switch (typeof selector) { case "string": case "number": break; default: return ""; }; var matched = combined.match(new RegExp("[\?\&#][^\?\&]+=[^\?\&]+", "g")); var array = []; if (matched) { for (var i = 0; i < matched.length; i++) { var split = matched[i].substring(1).split("="); array.push({ key: split[0], value: split[1] }); }; }; var value = ""; switch (typeof selector) { case "string": for (var i in array) { if (array[i].key == selector) { value = array[i].value; break; }; }; break; case "number": var i = parseInt(selector); if (i >= 0 && i < array.length) { value = array[i].value; break; }; break; }; if (decode) value = decodeURIComponent(value); return value; }; window.speak = function (text) { var rate = parseFloat(localStorage.getItem("readSpeed")); if (window.speechSynthesis && window.SpeechSynthesisUtterance) { if (!isNaN(rate)) { if (rate < 0.1) rate = 0.1; if (rate > 10) rate = 10; } var utterance = new SpeechSynthesisUtterance(); utterance.text = text; utterance.lang = "zh-CN"; if (rate) utterance.rate = rate; window.speechSynthesis.speak(utterance); } };