UNPKG

watrmark

Version:

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

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