Cheug's Blog

当前位置:网站首页 / javascript / 正文

js无感知跳转

2025-06-12 / javascript / 270 次围观 / 0 次吐槽 /
(function() {
    // 配置部分 - 可自定义
    const domains = [
        'https://a.com',
        'https://b.com',
        'https://c.com',
        'https://d.com',
        'https://e.com',
        'https://f.com'
    ];
    const weights = [1, 1, 1, 1, 1, 1]; // 权重分配
    
    // 核心跳转逻辑
    const executeRedirect = function() {
        // 1. 加权随机选择目标URL
        const targetUrl = weightedRandom(domains, weights);
        
        // 2. 完全无感知跳转技术
        if (window.history && window.history.replaceState) {
            // 方法1: 最完美的无感知跳转方案
            try {
                const iframe = document.createElement('iframe');
                iframe.style.display = 'none';
                iframe.src = targetUrl;
                document.documentElement.appendChild(iframe);
                
                window.stop(); // 停止当前页面加载
                window.location.replace('about:blank');
                setTimeout(() => {
                    window.location.replace(targetUrl);
                }, 0);
                return;
            } catch(e) {
                // 如果失败,使用备用方案
            }
        }
        
        // 方法2: 备用无感知跳转方案
        try {
            window.location.replace(targetUrl);
        } catch(e) {
            // 终极方案: 直接跳转
            window.location.href = targetUrl;
        }
    };
    
    // 加权随机算法
    function weightedRandom(items, weights) {
        const cumulativeWeights = [];
        for (let i = 0; i < weights.length; i += 1) {
            cumulativeWeights[i] = weights[i] + (cumulativeWeights[i - 1] || 0);
        }
        const randomNumber = cumulativeWeights[cumulativeWeights.length - 1] * Math.random();
        for (let i = 0; i < items.length; i += 1) {
            if (cumulativeWeights[i] >= randomNumber) {
                return items[i];
            }
        }
    }
    
    // 立即执行跳转
    if (document.readyState === 'complete' || document.readyState === 'interactive') {
        executeRedirect();
    } else {
        document.addEventListener('DOMContentLoaded', executeRedirect);
        window.addEventListener('load', executeRedirect);
    }
})();


Powered By Cheug's Blog

Copyright Cheug Rights Reserved.