hexo防止被镜像-省流版

master ,这是我的小站,欢迎访问哦~~

为网站添加 JavaScript 脚本

原理是检测当前站点是否为正确站点,否则:

  • 弹窗提示框,说明此站点非官方站点。或直接重定向到官方站点。
  • 站点加上水印
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    var validDomain = 'uuanqin.top';  
    var redirectUrl = 'https://blog.uuanqin.top';
    var hostname = document.location.hostname;
    var isLocalHost = (hostname === 'localhost' || hostname === '127.0.0.1');


    if (!isLocalHost && !hostname.endsWith(validDomain)) {
    createWatermark(validDomain)
    var userResponse = confirm(\`警告:本页面非官方页面,可能存在有害信息!建议您立即跳转 ${validDomain} 并向站长举报该镜像站:${hostname}!\`);
    if (userResponse) {
    window.location.replace(redirectUrl);
    }
    }

    function createWatermark(text) {
    var watermarkDiv = document.createElement('div');
    watermarkDiv.style.pointerEvents = 'none';
    watermarkDiv.style.position = 'fixed';
    watermarkDiv.style.top = '0';
    watermarkDiv.style.left = '0';
    watermarkDiv.style.width = '100%';
    watermarkDiv.style.height = '100%';
    watermarkDiv.style.zIndex = '9999';
    watermarkDiv.style.opacity = '0.1';
    watermarkDiv.style.background = 'transparent';
    watermarkDiv.style.overflow = 'hidden';
    watermarkDiv.style.display = 'flex';
    watermarkDiv.style.justifyContent = 'center';
    watermarkDiv.style.alignItems = 'center';
    watermarkDiv.style.flexWrap = 'wrap';

    var watermarkText = document.createElement('div');
    watermarkText.innerText = text;
    watermarkText.style.color = 'black';
    watermarkText.style.fontSize = '30px';
    watermarkText.style.transform = 'rotate(-30deg)';
    watermarkText.style.whiteSpace = 'nowrap';
    watermarkText.style.margin = '20px';

    for (var i = 0; i < 100; i++) {
    watermarkDiv.appendChild(watermarkText.cloneNode(true));
    }

    document.body.appendChild(watermarkDiv);
    }
    js 引入前记得做混淆处理:JavaScript Obfuscator Tool,勾选 unicode escape sequence选项把中文进行转义处理。

原文

半方池水半方田-我吃柠檬!网站被镜像自己却后知后觉