UNPKG

watrmark

Version:

watermark 是一个用于在网页中创建水印的 SDK。它提供了一个简单易用的接口,让你可以轻松地添加自定义水印到网页内容中。

80 lines (79 loc) 1.99 kB
/** * watrmark * version: v1.1.2 */ function S() { return (/* @__PURE__ */ new Date()).getTime().toString(36) + Math.random().toString(36).substr(2); } const s = /* @__PURE__ */ new Map(); function p(t, e, c = {}) { const f = Math.max(document.body.scrollWidth, window.screen.width), g = Math.max(document.body.scrollHeight, window.screen.height), { width: a = 200, height: d = 200, opacity: b = 0.1, rotate: y = 330, fontSize: w = "24px", fontFamily: M = "微软雅黑", color: v = "#333" } = c, u = document.createElement("span"), x = { position: "fixed", width: a + "px", height: d + "px", opacity: String(b), transform: `rotate(-${y}deg)`, fontSize: w, fontFamily: M, pointerEvents: "none", color: v }; Object.assign(u.style, x); const W = Math.ceil(f / a), j = Math.ceil(g / d), m = document.createDocumentFragment(); for (let o = 0; o < j; o++) { const r = o * d + "px"; for (let l = 0; l < W; l++) { const i = u.cloneNode(!0); Object.assign(i.style, { left: l * a + "px", top: r }), Array.isArray(e) ? i.innerHTML = e.join("<br />") : i.innerText = e, m.appendChild(i); } } const n = document.createElement("div"); n.append(m); const h = new MutationObserver((o) => { for (let r of o) (r.target === n || r.target.parentNode === n || r.removedNodes[0] === n) && (T(t), p(t, e, c)); }); return s.set(t, { remove: () => { h.disconnect(), n.remove(), s.delete(t); } }), document.body.append(n), h.observe(document.body, { attributes: !0, childList: !0, subtree: !0 }), t; } function E(t, e = {}) { return p(S(), t, e); } function T(t) { try { if (t) { const { remove: e } = s.get(t); e == null || e(); } else for (const [e, { remove: c }] of s) c(); } catch (e) { console.error(e); } } export { T as clearWatrmark, E as generate };