大学生
https://director4168.github.io
 
- 金币
- 3108
- 好评
- 8
- 信誉
- 100
   

|
本帖最后由 director_mark 于 2026-4-8 14:09 编辑
分享一个基础的保护H5项目的俩JS
- (function() {
- 'use strict';
- const PROTECT_SRC = './js/Protect.js';
- let coreInitialized = false;
- function isProtectReallyLoaded() {
- const scripts = document.querySelectorAll('script[src]');
- let scriptFound = false;
- for (let s of scripts) {
- const src = s.src || '';
- if (src.includes('Protect.js') || src.endsWith('Protect.js')) {
- scriptFound = true;
- break;
- }
- }
- const flagSet = window.__PROTECT_LOADED === true;
- return scriptFound && flagSet;
- }
- function CoreFeatures() {
- if (coreInitialized) return;
- coreInitialized = true;
- =========================
- 你的JS
- =========================
- }
- function startAntiDebug() {
- setInterval(() => {
- const devtools = window.outerWidth - window.innerWidth > 200 ||
- window.outerHeight - window.innerHeight > 200;
- if (devtools) {
- console.clear();
- document.body.innerHTML = '<h1 style="color:red;text-align:center;padding:50px;">ERROR</h1>';
- }
- },
- 600);
- }
- function checkAndRun() {
- if (coreInitialized) return;
- let attempts = 0;
- const maxAttempts = 20;
- const checker = setInterval(() => {
- attempts++;
- if (isProtectReallyLoaded()) {
- clearInterval(checker);
- CoreFeatures();
- return;
- }
- if (attempts >= maxAttempts) {
- clearInterval(checker);
- console.warn('[ERROR] ERROR');
- const warnDiv = document.createElement('div');
- warnDiv.style.cssText = 'position:fixed;top:0;left:0;right:0;background:#d32f2f;color:#fff;padding:14px;text-align:center;z-index:99999;font-size:15px;';
- warnDiv.textContent = 'ERROR';
- document.body.prepend(warnDiv);
- }
- },
- 100);
- }
- if (document.readyState === 'loading') {
- document.addEventListener('DOMContentLoaded', checkAndRun);
- } else {
- checkAndRun();
- }
- window.__CORE_READY = true;
- })();
复制代码
提供基础反F12,检查是否加载了指定JS(这里检查的是是否加载了同目录下Protect.js,如果没有检测加载了指定JS,则不会运行CoreFeatures函数内的JS,并在顶部添加一个红色的警告框内容为ERROR
- document.addEventListener('DOMContentLoaded', function() {
- Object.defineProperty(document, 'designMode', {
- value: 'off',
- writable: false,
- configurable: false
- });
- document.onkeydown = function(e) {
- if (
- e.key === 'F12' ||
- (e.ctrlKey && e.shiftKey && e.key === 'I') ||
- (e.ctrlKey && e.shiftKey && e.key === 'J') ||
- (e.ctrlKey && e.key === 'u')
- ) {
- return false;
- }
- };
- document.oncontextmenu = function(e) {
- if (e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA') {
- return false;
- }
- };
- const observer = new MutationObserver(function(mutations) {
- mutations.forEach(function(mutation) {
- if (mutation.type === 'characterData') {
- mutation.target.textContent = mutation.oldValue;
- }
- if (mutation.type === 'childList') {
- mutation.removedNodes.forEach(node => {
- if (node.parentNode) {
- node.parentNode.appendChild(node);
- }
- });
- }
- });
- });
- observer.observe(document.body, {
- childList: true,
- characterData: true,
- characterDataOldValue: true,
- subtree: true
- });
- window.__PROTECT_LOADED = true;
- });
复制代码
提供基础反F12、反右键、反编辑、反开发者快捷键、防篡改监控
并告诉是上一个JS,他要求必须加载的那个JS已加载
以上两个都只能针对小白,仅提供基础防护功能
我的网站由InfinityFree提供托管
我的网站由Github Pages提供托管
例图(没有加载Protect.js,可以看到,顶部出现了一个红色警告框,并且预览器处于深色模式,但是网页没有正常处于深色模式)
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|