你是一个程序员我现在需要你写一个JS代码需要实现如下功能1:屏蔽PC端访问只允许移动端访问2:不能屏蔽搜索引擎蜘蛛3:禁止使用F124:禁止浏览器查看源代码
以下是一个实现上述功能的示例代码:
// 1. 屏蔽PC端访问,只允许移动端访问
function checkMobile() {
const userAgentInfo = navigator.userAgent;
const agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
for (let i = 0; i < agents.length; i++) {
if (userAgentInfo.indexOf(agents[i]) > -1) {
return true;
}
}
return false;
}
if (!checkMobile()) {
window.location.href = "https://www.example.com/mobile-only-page"; // 跳转到移动端页面
}
// 2. 不能屏蔽搜索引擎蜘蛛
const userAgent = navigator.userAgent.toLowerCase();
const isSpider = !!(userAgent.includes('googlebot') || userAgent.includes('bingbot') || userAgent.includes('slurp'));
if (isSpider) {
// 可以执行特定的操作,如记录日志或提供不同内容给蜘蛛
}
// 3. 禁止使用F12
document.onkeydown = function (event) {
const keyCode = event.keyCode || event.which;
if (keyCode === 123) {
return false;
}
};
// 4. 禁止浏览器查看源代码
document.oncontextmenu = function () {
return false;
};
请注意,这些功能只是对一般用户起到一定的限制作用,并不能完全阻止高级用户访问和查看源代码
原文地址: https://www.cveoy.top/t/topic/iqXX 著作权归作者所有。请勿转载和采集!