为防止jenkins错误发布生产,我写了一个油猴脚本,增加了一个按钮,需要发布生产的时候点击才会显示生产的链接。而且每天会定时重置状态。
我们jenkins的大概是这样的, 生产的会有前缀。我也是根据前缀判断是否是生产环境

我会把涉及到的东西存在localstorage里,

效果图如下

在油猴新建一个脚本就可以使用了

// ==UserScript==
// @name         发布生产链接控制器
// @namespace    http://tampermonkey.net/
// @version      1.11
// @description  控制“生产环境”链接显示/隐藏(按钮居中 + 自动刷新 + 确认弹窗 + DOM监控 + 每日重置)
// @author       小生
// @match        http://cfjk.xxxxx.com:8080/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const STORAGE_KEY = 'cfjk_production_toggle';
    const LAST_DATE_KEY = 'cfjk_last_visit_date';

    // 当前日期字符串(如 2025-05-23)
    const today = new Date().toISOString().split('T')[0];
    const lastVisit = localStorage.getItem(LAST_DATE_KEY);

    // 如果是新的一天,重置开关为关闭
    if (lastVisit !== today) {
        localStorage.setItem(STORAGE_KEY, 'false');
        localStorage.setItem(LAST_DATE_KEY, today);
    }

    // 当前开关状态
    let isVisible = localStorage.getItem(STORAGE_KEY) === 'true';

    // 创建按钮
    const button = document.createElement('button');
    button.textContent = `发布生产:${isVisible ? '开启' : '关闭'}`;
    button.style.position = 'fixed';
    button.style.top = '5px';
    button.style.left = '50%';
    button.style.transform = 'translateX(-50%)';
    button.style.padding = '10px 20px';
    button.style.fontSize = '14px';
    button.style.zIndex = '9999';
    button.style.border = '1px solid #666';
    button.style.borderRadius = '6px';
    button.style.backgroundColor = isVisible ? '#4caf50' : '#f44336';
    button.style.color = '#fff';
    button.style.cursor = 'pointer';
    button.style.boxShadow = '0 2px 6px rgba(0,0,0,0.3)';
    document.body.appendChild(button);

    // 按钮点击事件
    button.addEventListener('click', () => {
        if (!isVisible) {
            const confirmed = confirm('确认发布生产?');
            if (!confirmed) return;
        }

        isVisible = !isVisible;
        localStorage.setItem(STORAGE_KEY, isVisible);
        updateProdLinkVisibility(); // 更新状态而非刷新页面
    });

    // 控制“生产环境”链接显示
    function updateProdLinkVisibility() {
        const links = document.querySelectorAll('a');
        links.forEach(link => {
            if (link.textContent.includes('生产环境')) {
                link.style.display = isVisible ? '' : 'none';
            }
        });

        // 同步按钮状态文本和颜色
        button.textContent = `发布生产:${isVisible ? '开启' : '关闭'}`;
        button.style.backgroundColor = isVisible ? '#4caf50' : '#f44336';
    }

    // 延迟执行更新操作,确保页面初始化后执行
    setTimeout(() => {
        updateProdLinkVisibility(); // 初始化时控制链接的显示/隐藏
    }, 0); // 使用零延迟确保按钮和状态已设置

    // 防抖函数,确保 mutation observer 只在一定时间内触发一次
    let debounceTimeout;
    function debounceUpdate() {
        clearTimeout(debounceTimeout);
        debounceTimeout = setTimeout(() => {
            updateProdLinkVisibility();
        }, 200); // 延迟200ms
    }

    // 监听 DOM 变化,动态更新链接隐藏状态
    const observer = new MutationObserver(() => {
        debounceUpdate(); // 使用防抖处理
    });

    // 使用更精确的监控目标,减少不必要的触发
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true, // 监听属性变化
        characterData: true // 监听文本变化
    });

})();


0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注

站点统计

  • 文章总数:324 篇
  • 分类总数:20 个
  • 标签总数:193 个
  • 运行天数:1398 天
  • 访问总数:561191 人次

浙公网安备33011302000604

辽ICP备20003309号