lelms-copyright/js/watermark.js
2025-03-10 12:26:57 +08:00

167 lines
5.3 KiB
JavaScript

(function($) {
'use strict';
const options = lelmsData.options;
function addWatermark() {
const container = $('#lelms-watermark');
const username = lelmsData.username;
// 使用 Math.max 获取真实文档高度
const documentHeight = Math.max(
$(document).height(),
$(window).height(),
$('body').height(),
$('.site-content').height() || 0
);
const windowWidth = $(window).width();
container.empty().css('height', documentHeight + 'px');
const spacing = parseInt(options.watermark_spacing);
const rows = Math.ceil(documentHeight / spacing);
const cols = Math.ceil(windowWidth / spacing);
// 使用文档片段优化性能
const fragment = document.createDocumentFragment();
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const watermark = $('<div>', {
class: 'watermark-text',
text: username,
css: {
left: (j * spacing) + 'px',
top: (i * spacing) + 'px',
color: options.watermark_color,
opacity: options.watermark_opacity,
fontSize: options.watermark_size + 'px',
transform: `rotate(${options.watermark_angle}deg)`
}
})[0];
fragment.appendChild(watermark);
}
}
container[0].appendChild(fragment);
}
function preventDevTools() {
if (options.enable_devtools_protection !== 'yes') return;
// 优化的事件处理
document.addEventListener('keydown', function(e) {
if (e.key === 'F12' ||
(e.ctrlKey && e.shiftKey && e.key === 'I') ||
(e.ctrlKey && e.key === 'U')) {
e.preventDefault();
alert(lelmsData.warningMessage);
return false;
}
});
// 改进的右键菜单处理
document.addEventListener('contextmenu', function(e) {
const target = e.target;
const allowedSelectors = [
'input', 'textarea', 'select',
'[contenteditable="true"]',
'.comment-form *',
'.wp-block-post-comments *',
'.wp-block-post-comments-form *',
'form *'
].join(',');
if (!target.matches(allowedSelectors)) {
e.preventDefault();
return false;
}
});
// 优化的开发者工具检测
let devToolsCounter = 0;
let lastCheck = Date.now();
const checkDevTools = () => {
const now = Date.now();
if (now - lastCheck < 800) return; // 防止过于频繁检查
lastCheck = now;
const threshold = 160;
const widthDiff = window.outerWidth - window.innerWidth;
const heightDiff = window.outerHeight - window.innerHeight;
if (widthDiff > threshold || heightDiff > threshold) {
devToolsCounter++;
if (devToolsCounter > 3) {
alert(lelmsData.warningMessage);
setTimeout(() => location.reload(), 100);
}
} else {
devToolsCounter = Math.max(0, devToolsCounter - 1);
}
};
const rafLoop = () => {
checkDevTools();
requestAnimationFrame(rafLoop);
};
requestAnimationFrame(rafLoop);
}
function preventCopy() {
if (options.enable_copy_protection !== 'yes') return;
const allowedSelectors = [
'input', 'textarea',
'[contenteditable="true"]',
'.comment-form *',
'.wp-block-post-comments *',
'.wp-block-post-comments-form *',
'form *'
].join(',');
document.addEventListener('copy', function(e) {
if (!e.target.matches(allowedSelectors)) {
e.preventDefault();
return false;
}
});
document.addEventListener('selectstart', function(e) {
if (!e.target.matches(allowedSelectors)) {
e.preventDefault();
return false;
}
});
}
// 初始化
$(document).ready(function() {
preventDevTools();
addWatermark();
preventCopy();
// 使用 ResizeObserver 监听尺寸变化
const resizeObserver = new ResizeObserver(_.debounce(() => {
addWatermark();
}, 200));
resizeObserver.observe(document.body);
// 监听动态内容变化
const mutationObserver = new MutationObserver(_.debounce(() => {
addWatermark();
}, 200));
mutationObserver.observe(document.body, {
childList: true,
subtree: true,
attributes: true
});
// 清理函数
$(window).on('unload', () => {
resizeObserver.disconnect();
mutationObserver.disconnect();
});
});
})(jQuery);