有的时候很头疼,比如辛辛苦苦写的页面元素,被别人轻而易举就复制保存,然后夏柔发现了这个方案:

将图中的js代码保存到你的html项目任意js里就可以了;
当然记得html文件也要引用那个js文件,不然不会生效;
还有更强的办法就是将js代码给他加密,用哪个javascript最强加密,不出意外的话几乎没法解密,(开玩笑);
这样就会很大程度的将你的源代码保护了,嘿嘿
尝试一下吧,这个代码主要用于是防止用户将前端文件保存到本地,如果到本地,也会直接显示黑屏的
当然了,有朋友说如果他把这个js文件删了不就还是可以偷了;
至少我觉得你可以将这个 js代码复制粘贴到你的系统js文件里,然后将整个javascript加密,这样几乎就完美解决了;
当然要注意兼容问题,不然不一定能生效的~
下面是javascript代码:
// 当页面被整个盗取到本地的时候,本地打开一片空白,需要jquery
function authentication() {
var suffix = "cn",
main="royun",
red="w",
dot = ".";
var d = (main+red).toString() + dot + suffix;
if (window.location.host.indexOf(d) < 0) {
$("body").remove();
return false
}
return true
}
$(function() {
if (!authentication()) return;
$('[data-bs-toggle="tooltip"]').tooltip()
console.log = function() {};
console.error = function() {};
console.warn = function() {};
document.addEventListener('contextmenu', function (e) {
e.preventDefault();
});
window.addEventListener('devtoolschange', function(e) {
if (e.detail.isOpen) {
// 控制台已打开,执行相应操作
location.href = "https://www.baidu.com"
// 例如:跳转到其他页面或显示警告信息
}
});
document.addEventListener('keydown', function(e) {
e = window.event || e;
var keycode = e.keyCode || e.which;
if (e.ctrlKey && keycode == 83) { //屏蔽Ctrl+s 保存页面
e.preventDefault();
window.event.returnValue = false;
}
if (e.ctrlKey && keycode == 85) { //屏蔽Ctrl+u 查看页面的源代码
e.preventDefault();
window.event.returnValue = false;
}
if (keycode == 123) { //屏蔽F12
e.preventDefault();
window.event.returnValue = false;
}
if (e.ctrlKey && e.shiftKey && keycode == 73) { //屏蔽Ctrl+shift+i 屏蔽调出控制台 和F12一样
e.preventDefault();
window.event.returnValue = false;
}
});
// 当页面滚动时显示或隐藏返回顶部按钮
window.addEventListener('scroll', function() {
var scrollToTop = document.getElementById('scrollToTop');
if (window.pageYOffset > 500) {
scrollToTop.style.display = 'block';
} else {
scrollToTop.style.display = 'none';
}
});
});
(function noDebuger() {
function testDebuger() {
var wWADWeTEd1 = new window["Date"]();
console.log('%c--检测到已打开开发者模式,请遵守相关开发协议,未经开发者允许,禁止照搬本站源码--', 'color: red; font-size: 16px;');
debugger;
if (new window["Date"]() - wWADWeTEd1 > 10) {
window["document"]['body']['innerHTML'] = '<div>私有接口,请勿调用</div>';
return true
}
return false
}
function start() {
while (testDebuger()) {
testDebuger()
}
}
if (!testDebuger()) {
window['onblur'] = function() {
setTimeout(function() {
start()
}, 500)
}
} else {
start()
}
})();
// 监听鼠标滚动事件
window.addEventListener('scroll', function() {
if($("#pageName").val() === 'index' || $("#pageName").val() === 'category') {
// 获取导航栏元素
var navbar = document.querySelector('.navbar');
// 当滚动距离大于450px时显示导航栏,否则隐藏导航栏
if (window.pageYOffset > 450) {
navbar.classList.remove('transparent');
} else {
navbar.classList.add('transparent');
}
}
});
function changeTheme() {
var currTheme = localStorage.getItem('THEME') ?? 'dark';
$("body").removeClass(localStorage.getItem('THEME') ?? 'dark');
if (currTheme === 'dark') {
localStorage.setItem('THEME', 'base');
$("body").addClass('base');
} else {
localStorage.setItem('THEME', 'dark');
$("body").addClass('dark');
}
}
(function() {
const el = document.querySelector('body')
const theme = localStorage.getItem('THEME') ?? 'dark'
el.setAttribute('class', theme)
localStorage.setItem('THEME', theme)
if(theme === 'base') {
$("#themeBtn").prop('checked', true);
} else {
$("#themeBtn").prop('checked', false);
}
const html = document.documentElement
function setFont() {
const cliWidth = html.clientWidth
if (cliWidth > 1920) {
html.style.fontSize = '16px'
return
}
html.style.fontSize =
(16 * (cliWidth / 1920) < 10 ? 10 : 16 * (cliWidth / 1920)) + 'px'
}
setFont()
window.onresize = function() {
setFont()
}
})()
// 返回顶部按钮点击事件
document.getElementById('scrollToTop').addEventListener('click', function() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
