Block Sites and Redirect

自动屏蔽微博、联合早报等指定网站,跳转至百度

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Block Sites and Redirect
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  自动屏蔽微博、联合早报等指定网站,跳转至百度
// @author       YourName
// @match        *://*/*
// @run-at       document-start
// @grant        none
// @license     GPL version 3
// ==/UserScript==

(function() {
    'use strict';

    // 正则表达式黑名单(支持多级子域名)
    const blockedRegex = [
        /(^|\.)weibo\.com$/,          // 匹配 weibo.com 及所有子域
        /(^|\.)zaobao\.com(\.|$)/,    // 匹配 zaobao.com 及其多级子域(如zaobao.com.sg)
        /(^|\.)tophub\.today$/        // 匹配 tophub.today 及所有子域
    ];

    // 获取当前域名(转换为小写避免大小写问题)
    const currentHost = window.location.hostname.toLowerCase();

    // 执行正则匹配检测
    const shouldBlock = blockedRegex.some(regex => regex.test(currentHost));

    // 执行屏蔽逻辑
    if (shouldBlock) {
        // 跳转前验证是否已经在目标网站(防止循环跳转)
        if (!/^www\.baidu\.com$/i.test(currentHost)) {
            window.location.replace('https://www.baidu.com/');
        }
    }
})();