Files
wordpress-theme-aksrt/assets/js/content-protect.js
T

121 lines
4.0 KiB
JavaScript

/**
* AKSRT Content Protection — Copy copyright / Right-click disable / F12 detection
*
* All features are DEFAULT OFF and configurable via Settings.
* This is a "soft" protection — it increases the barrier but cannot truly prevent copying.
*
* @package AKSRT
*/
(function () {
'use strict';
var config = (window.aksrtData && window.aksrtData.contentProtect) || {};
// Bail if nothing enabled
if (!config.enabled) return;
/* ============================================================
* 1. 复制时追加版权声明
* ============================================================ */
if (config.copyNotice) {
document.addEventListener('copy', function (e) {
var selection = window.getSelection();
if (!selection || selection.isCollapsed) return;
var text = selection.toString();
if (text.length < 20) return; // Too short, skip
var notice = '\n\n---\n' +
'版权声明:本文' + (config.copyNoticeText || '来自本站,请尊重原创版权。') + '\n' +
'链接:' + window.location.href;
// Append notice to clipboard
var clipboardData = e.clipboardData || window.clipboardData;
if (clipboardData) {
e.preventDefault();
clipboardData.setData('text/plain', text + notice);
}
});
}
/* ============================================================
* 2. 禁用右键菜单
* ============================================================ */
if (config.disableRightClick) {
document.addEventListener('contextmenu', function (e) {
// Allow on input/textarea elements
var tag = e.target.tagName;
if (tag === 'INPUT' || tag === 'TEXTAREA' || e.target.isContentEditable) return;
e.preventDefault();
// Show brief notification
showProtectionNotice('本站已禁用右键菜单');
});
}
/* ============================================================
* 3. F12 / DevTools 检测(仅提示,不阻止)
* ============================================================ */
if (config.detectDevTools) {
document.addEventListener('keydown', function (e) {
// F12
if (e.key === 'F12' || e.keyCode === 123) {
e.preventDefault();
showProtectionNotice('本站已禁用开发者工具');
return false;
}
// Ctrl+Shift+I / Cmd+Option+I
if ((e.ctrlKey || e.metaKey) && e.shiftKey && (e.key === 'I' || e.key === 'i' || e.keyCode === 73)) {
e.preventDefault();
showProtectionNotice('本站已禁用开发者工具');
return false;
}
// Ctrl+Shift+J / Cmd+Option+J
if ((e.ctrlKey || e.metaKey) && e.shiftKey && (e.key === 'J' || e.key === 'j' || e.keyCode === 74)) {
e.preventDefault();
showProtectionNotice('本站已禁用开发者工具');
return false;
}
// Ctrl+U (view source)
if ((e.ctrlKey || e.metaKey) && (e.key === 'u' || e.key === 'U' || e.keyCode === 85)) {
e.preventDefault();
showProtectionNotice('本站已禁用查看源代码');
return false;
}
});
}
/* ============================================================
* Protection notice toast
* ============================================================ */
var noticeTimer = null;
function showProtectionNotice(text) {
if (noticeTimer) clearTimeout(noticeTimer);
var el = document.getElementById('aksrt-protection-notice');
if (!el) {
el = document.createElement('div');
el.id = 'aksrt-protection-notice';
el.style.cssText =
'position:fixed;bottom:20px;left:50%;transform:translateX(-50%);' +
'background:rgba(0,0,0,0.75);color:#fff;padding:8px 20px;border-radius:8px;' +
'font-size:13px;z-index:99999;transition:opacity 0.3s;pointer-events:none;';
document.body.appendChild(el);
}
el.textContent = text;
el.style.opacity = '1';
noticeTimer = setTimeout(function () {
el.style.opacity = '0';
}, 2000);
}
})();