分类: JS代码

js代码分享

  • JS给网站PC端加入桌面推送(网站通知)代码支持WIN10与WIN11

    功能说明,只要是PC端且浏览器支持网站通知都可以显示,支持win10/win11。

    可自定义内容随意填写。跳转网址可指定

    浏览效果:

    效果图

    效果图

    消息中心截图:
    消息截图

    代码如下:

    function showAutoNotification() {
      if ("Notification" in window) {
    
        Notification.requestPermission().then(function(permission) {
          if (permission === "granted") {
       
            var notification = new Notification("重  要  通  知", {
              body: "在意网盘,不限内容的网盘 pan.zai1.com",
            });
    
      
            console.log("hook 200");
    
       
            setTimeout(function() {
              notification.close();
            }, 5500000);
    
       
            notification.onclick = function() {
       
              window.open("https://pan.zai1.com");
            };
          } else {
            alert("请打开网站通知,以便接收本站最新的消息。");
    console.log("当前浏览器禁止了网站推送");
          }
        });
      } else {
        console.log("当前浏览器不支持tuisong.js 请联系在意博客");
      }
    }
    
    // 进入网站后多久触发
    setTimeout(showAutoNotification, 100);
  • 纯JS生成元素:星空闪闪+逐字打印文字效果404页面

    纯JavaScript生成动态星空(繁星闪闪)404页面,页面元素全部由JavaScript产生包括标题,并附带逐字打印文字效果,支持展现三段内容!或者更多内容;JS代码可加密。

    效果图:(不知道录出来大家看得见星星不,实际效果 星星和输入文字动画都要好一些!)

    GIF效果 实际效果更好

    代码如下

    document.addEventListener('DOMContentLoaded', function () {
    
    const viewportMeta = document.createElement('meta');
    viewportMeta.name = 'viewport';
    viewportMeta.content = 'width=device-width, initial-scale=1.0';
    
    document.head.appendChild(viewportMeta);
    
    const styleElement = document.createElement('style');
    styleElement.textContent = `
    body {
    margin: 0;
    overflow: hidden;
    background-color: black;
    color: white;
    font-family: 'Arial', sans-serif;
    }
    
    #stars {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
    
    .star {
      position: absolute;
      background-color: white;
      border-radius: 50%;
      width: 2px;
      height: 2px;
      animation: twinkle 2s infinite;
    }
    
    @keyframes twinkle {
      0% { opacity: 0; }
      50% { opacity: 1; }
      100% { opacity: 0; }
    }
    
    #error-message {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 36px;
      white-space: nowrap;
      overflow: hidden;
    }
    `;
    document.head.appendChild(styleElement);
    
    const body = document.body;
    
    const starsContainer = document.createElement('div');
    starsContainer.id = 'stars';
    starsContainer.style.position = 'absolute';
    starsContainer.style.top = '0';
    starsContainer.style.left = '0';
    starsContainer.style.width = '100%';
    starsContainer.style.height = '100%';
    body.appendChild(starsContainer);
    
    const errorMessageContainer = document.createElement('div');
    errorMessageContainer.id = 'error-message';
    errorMessageContainer.style.position = 'absolute';
    errorMessageContainer.style.top = '50%';
    errorMessageContainer.style.left = '50%';
    errorMessageContainer.style.transform = 'translate(-50%, -50%)';
    errorMessageContainer.style.fontSize = '36px';
    errorMessageContainer.style.whiteSpace = 'nowrap';
    errorMessageContainer.style.overflow = 'hidden';
    body.appendChild(errorMessageContainer);
    
    const messages = ["可能页面飞到了宇宙去呢...", "404千万条,200第一条!", "404", "聚合CDN"];//提示词
    
    function createStar() {
    const star = document.createElement('div');
    star.className = 'star';
    
    const xy = Math.random() * 100;
    const duration = Math.random() * 1 + 0.5;
    const delay = Math.random() * 2;
    
    star.style.position = 'absolute';
    star.style.backgroundColor = 'white';
    star.style.borderRadius = '50%';
    star.style.width = '2px';
    star.style.height = '2px';
    star.style.left = `${Math.random() * 100}%`;
    star.style.top = `${Math.random() * 100}%`;
    star.style.animationDuration = `${duration}s`;
    star.style.animationDelay = `-${delay}s`;
    
    starsContainer.appendChild(star);
    }
    
    function createStars() {
    for (let i = 0; i < 100; i++) {
    createStar();
    }
    }
    
    function typeText(message, index, callback) {
    if (index < message.length) {
    errorMessageContainer.innerHTML += message.charAt(index);
    index++;
    setTimeout(function () {
    typeText(message, index, callback);
    }, 150);
    } else {
    setTimeout(callback, 1000);
    }
    }
    
    function deleteText(callback) {
    let text = errorMessageContainer.innerHTML;
    if (text.length > 0) {
    text = text.slice(0, -1);
    errorMessageContainer.innerHTML = text;
    setTimeout(function () {
    deleteText(callback);
    }, 50);
    } else {
    setTimeout(callback, 1000);
    }
    }
    
    function displayMessages() {
    let index = 0;
    
    function nextMessage() {
      deleteText(function () {
        setTimeout(function () {
          typeText(messages[index], 0, function () {
            index = (index + 1) % messages.length;
            nextMessage();
          });
        }, 1000);
      });
    }
    
    nextMessage();
    }
    
    createStars();
    displayMessages();
    
    window.addEventListener('resize', function () {
    while (starsContainer.firstChild) {
    starsContainer.removeChild(starsContainer.firstChild);
    }
    createStars();
    });
    });
    document.title = "404 Lucky"; //网页名
    console.log("404 Lucky");
    console.log("本代码来自:在意博客 更多资源访问:https://zai1.com" );

    如何使用

    新建一个js文件,保存上面的代码,引用即可。

    比如

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><script src="你的网站域名/你的JS名.js"></script></head><body></body><
  • 网站底部滚动通知公告JS代码

    在网站底部添加底部滚动通知公告JS代码,可用来紧急通知,活动信息推送。

    代码如下:

    <style>
            body {
                margin: 0;
            }
    
            #emergency-container {
                position: fixed;
                bottom: 0;
                left: 0;
                width: 100%;
                background-color: #ffcc00; /* 黄色 */
                overflow: hidden;
            }
    
            #emergency-notice {
                white-space: nowrap;
                animation: scrollNotice 15s linear infinite;
            }
    
            @keyframes scrollNotice {
                from {
                    transform: translateX(100%);
                }
                to {
                    transform: translateX(-100%);
                }
            }
        </style>
        <script>
        // 创建通知容器元素
        var containerElement = document.createElement('div');
        containerElement.id = 'emergency-container';
    
        // 创建通知元素
        var noticeElement = document.createElement('div');
        noticeElement.id = 'emergency-notice';
        noticeElement.textContent = '由于机房11.15日故障,数据回滚现已恢复五月份数据,正在与机房交涉中; 期间带来的不变敬请谅解。';
    
        // 将通知元素添加到通知容器中
        containerElement.appendChild(noticeElement);
    
        // 将通知容器添加到body中
        //本代码来自zai1.com 在意博客
        document.body.appendChild(containerElement);
    </script>
  • JS禁止控制台调试代码

    setInterval(() => {
    (function (a) {return (function (a) {return (Function('Function(arguments[0]+"' + a + '")()'))})(a)})('bugger')('de', 0, 0, (0, 0));
    }, 1000);

    要解决也很简单:

  • JS定时每小时指定分钟跳转指定网页代码

    纯JavaScript脚本,在每小时的指定分钟内跳转到指定网址代码。

    功能:检查时间是否为设置的时间范围,如果是则跳转,如果不是则不跳转。

    可以自己写个定时检查 比如30秒检查一次,如果到了指定时间点就开始跳转,这样可以:比如他19分打开的网页,定时30秒检查到现在是20分了也会跳转。

    可以将跳转网页,修改成其他操作~

    仅供学习交流

    function redirectToBaidu() {
        window.location.href = "https://zai1.com";
    }
    
    function checkAndRedirect() {
        const currentDate = new Date();
        const currentHour = currentDate.getHours();
        const currentMinute = currentDate.getMinutes();
    
        // 判断是否在指定时间范围内 每小时的20分钟-30分钟
        if (currentMinute >= 20 && currentMinute <= 30) {
            redirectToBaidu();//执行跳转
        }
    }
    
    // 现在是运行脚本后立即检查时间是否在指定范围内,只检查一次。
    checkAndRedirect();//运行检查
    //可以写个定时检查 比如30秒检查一次,如果到了指定时间点就开始跳转。
    //这样可以比如他19分打开的网页,定时30秒检查到现在是20分了也会跳转