Jenkins不刷新问题修复

(function() {
    'use strict';

    // === 核心配置 ===
    const CONFIG = {
        pollInterval: 2000,        // 2秒刷新一次
        jenkinsBaseUrl: 'http://10.130.212.2:8080' // 你的Jenkins地址(仅改这一处即可)
    };

    // === 新增核心方法:自动提取当前页面的JobName ===
    function getCurrentJobName() {
        // 方案1:从URL提取(最可靠,Jenkins Job页面URL格式:/job/[jobName]/[buildNumber]/)
        const urlPath = window.location.pathname;
        const jobPathMatch = urlPath.match(/\/job\/([^\/]+)/);
        if (jobPathMatch && jobPathMatch[1]) {
            return jobPathMatch[1];
        }

        // 方案2:从DOM提取(兜底,适配URL异常的情况)
        const jobTitleEl = document.querySelector('.job-index-headline, .jenkins-build-caption h1');
        if (jobTitleEl) {
            return jobTitleEl.textContent.trim();
        }

        // 方案3:从app-bar的链接提取
        const appBarLink = document.querySelector('.jenkins-app-bar__content a[href*="/job/"]');
        if (appBarLink) {
            const linkHref = appBarLink.getAttribute('href');
            const linkMatch = linkHref.match(/\/job\/([^\/]+)/);
            if (linkMatch && linkMatch[1]) {
                return linkMatch[1];
            }
        }

        console.warn('无法自动识别JobName,请检查页面URL/DOM');
        return null;
    }

    // === 核心方法 ===
    // 1. 获取app-bar区域(要刷新的整个父容器)
    function getAppBar() {
        return document.querySelector('.jenkins-app-bar');
    }

    // 2. 刷新整个app-bar区域(完全还原原生样式)
    async function refreshAppBar() {
        const appBarEl = getAppBar();
        const jobName = getCurrentJobName(); // 自动获取当前JobName
        if (!appBarEl || !jobName) return;

        // Step1: 拼接当前Job的详情页URL
        const jobUrl = `${CONFIG.jenkinsBaseUrl}/job/${jobName}/`;
        
        try {
            // Step2: 请求Job详情页,提取原生的app-bar HTML
            const response = await fetch(jobUrl, {
                headers: {
                    'Accept': 'text/html' // 只请求HTML
                },
                credentials: 'same-origin' // 携带登录凭证,避免403
            });

            if (!response.ok) throw new Error(`请求失败: ${response.status}`);
            const html = await response.text();

            // Step3: 从返回的HTML中提取原生jenkins-app-bar部分
            const tempDiv = document.createElement('div');
            tempDiv.innerHTML = html;
            const newAppBar = tempDiv.querySelector('.jenkins-app-bar');
            
            if (newAppBar) {
                // Step4: 替换当前页面的app-bar(完全原生,无样式丢失)
                appBarEl.parentNode.replaceChild(newAppBar, appBarEl);
                console.log(`[${jobName}] AppBar已原生刷新,图标状态更新完成`);
            }
        } catch (err) {
            console.warn(`[${jobName}] AppBar刷新失败:`, err);
        }
    }

    // 3. 初始化:页面加载完成后启动轮询
    function init() {
        // 等待页面完全加载(避免操作未渲染的DOM)
        const initRefresh = () => {
            refreshAppBar();
            setInterval(refreshAppBar, CONFIG.pollInterval);
        };

        if (document.readyState === 'complete') {
            initRefresh();
        } else {
            window.addEventListener('load', initRefresh);
        }
    }

    // 启动执行
    init();
})();
posted @ 2026-03-23 16:33  EJW  阅读(11)  评论(0)    收藏  举报