1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| const defaultOption = { id: 'watermark-id', preventTamper: false, width: 110, height: 80, text: 'watermark', font: '20px Times New Roman', fontColor: 'rgba(204,204,204,0.45)', rotateDegree: (30 * Math.PI) / 180, translateX: 0, translateY: 0, style: { 'pointer-events': 'none', width: '100%', height: '100%', top: 0, left: 0, position: 'fixed', 'z-index': 1000, }, };
const observeConfig = { attributes: true, childList: true, characterData: true, subtree: true, };
let container; let observer;
function createImageUrl(options) { const canvas = document.createElement('canvas'); const text = options.text; canvas.width = options.width; canvas.height = options.height;
const ctx = canvas.getContext('2d'); ctx.shadowOffsetX = 2; ctx.shadowOffsetY = 2; ctx.shadowBlur = 2; ctx.font = options.font; ctx.fillStyle = options.fontColor; ctx.rotate(options.rotateDegree); ctx.translate(options.translateX, options.translateY); ctx.textAlign = 'left'; ctx.fillText(text, 35, 32); return canvas.toDataURL('image/png'); }
function createContainer(options, forceCreate) { const oldDiv = document.getElementById(options.id); if (!forceCreate && oldDiv) return container;
const url = createImageUrl(options); const div = oldDiv || document.createElement('div'); div.id = options.id;
let parentEl = options.preventTamper ? document.body : options.parentEl || document.body;
if (typeof parentEl === 'string') { if (parentEl.startsWith('#')) parentEl = parentEl.substring(1); parentEl = document.getElementById(parentEl); } const rect = parentEl.getBoundingClientRect(); options.style.left = (options.left || rect.left) + 'px'; options.style.top = (options.top || rect.top) + 'px';
div.style.cssText = getStyleText(options); div.setAttribute('class', ''); div.style.background = 'url(' + url + ') repeat top left';
!oldDiv && parentEl.appendChild(div);
return div; }
function getStyleText(options) { let ret = ''; const style = options.style; Object.keys(style).forEach((k) => { ret += k + ': ' + style[k] + ';'; }); return ret; }
function observe(options, observeBody) { observeWatermark(options); observeBody && observeBodyElement(options); }
function observeWatermark(options) { const target = container;
const childCallBack = () => { observer.disconnect(); container = createContainer(options, true); observer.observe(target, observeConfig); };
observer = new MutationObserver(childCallBack); observer.observe(target, observeConfig); }
function observeBodyElement(options) { const callback = (mutations) => { mutations.forEach((m) => { if (m.type === 'childList' && m.removedNodes.length > 0) { let watermarkNodeRemoved = false; for (const n of m.removedNodes) { if (n.id === options.id) { watermarkNodeRemoved = true; } }
if (watermarkNodeRemoved) { container = createContainer(options); observe(options, false); } } }); }; const pObserver = new MutationObserver(callback); pObserver.observe(document.body, { childList: true, subtree: true }); }
function init(options) { options = !options ? defaultOption : { ...defaultOption, ...options }; container = createContainer(options); options.preventTamper && observe(options, true); }
init({ preventTamper: true });
|