当前位置:首页 > 代码 > 正文内容

判断搜索引擎蜘蛛访问网页是正常页面 人为访问会变成备案主页

老野2024-04-10 12:37:2462

下面是js代码,在目录下创建js文件并保存调用。蜘蛛访问网页是正常页面,人为访问会变成备案主页。代码里的html主页代码可以自行改动替换。

document.addEventListener("DOMContentLoaded", function () {
var userAgent = navigator.userAgent.toLowerCase();
var isMobile = /iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(userAgent);
var isSpider = /bot|crawl|spider|slurp|bingbot|googlebot|msnbot|teoma|twiceler|yandex/i.test(userAgent);

if (!isMobile && !isSpider) {
document.title = "个人生活日记";
document.body.innerHTML = `
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>公司名称</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            flex-direction: column;
        }

        .container {
            text-align: center;
            max-width: 960px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .header img {
            max-width: 100%;
            height: auto;
            margin-bottom: 20px;
        }

        .article-list {
            list-style: none;
            padding: 0;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
        }

        .article-item {
            border: 1px solid #ccc;
            padding: 10px;
            border-radius: 5px;
        }

        footer {
            text-align: center;
            margin-top: 20px;
            color: #999;
            font-size: 14px;
        }
        
        @media (max-width: 567px) {
            .article-list {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div>
        <div>
            <img src="/logo.png" alt="公司名称">
        </div>
        <h1>关于我们</h1>
        <ul>
            <li>
                <p>我们是一家专注于互联网技术创新与应用的前沿企业,致力于为全球用户提供卓越的数字化解决方案和服务。</p>
            </li>
      
        </ul>
    </div>
    <footer>
        <p>&copy; 2023 <a href="/" target="_blank">XXX网络</a> 版权所有
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow">桂ICP备XXXXXXXX号</a></p>
    </footer>
</body>
</html>
        `;
}
// 对于搜索蜘蛛和移动端用户,不需要进行任何操作
});