Greasy Fork

Greasy Fork is available in English.

拦截非当前域名请求

拦截请求,过滤非当前域名请求。同域名不同端口号不会过滤。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         拦截非当前域名请求
// @namespace    https://github.com/guoshiqiufeng
// @version      1.0.1
// @description  拦截请求,过滤非当前域名请求。同域名不同端口号不会过滤。
// @author       yanghq
// @license MIT
// @match        http://*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 保存当前域名
    var currentDomain = window.location.hostname;

    // 重写XMLHttpRequest的open方法
    var originalOpen = XMLHttpRequest.prototype.open;

    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
        // 检查请求的目标域名
        var targetDomain = (new URL(url)).hostname;

        // 如果目标域名与当前域名不匹配,则拦截请求
        if (targetDomain !== currentDomain) {
            console.log('拦截请求:', url);
            return;
        }

        // 调用原始的open方法
        originalOpen.apply(this, arguments);
    };

    var originalCreateElement = document.createElement;

    document.createElement = function(tagName) {
         var element = originalCreateElement.apply(this, arguments);

        // 拦截创建 script 元素的情况
        if (tagName.toLowerCase() === 'script') {
            interceptScriptElement(element);
        }

        return element;
    };

    function interceptScriptElement(scriptElement) {
        // 监听 src 属性的设置
        var originalSetAttribute = scriptElement.setAttribute;
        scriptElement.setAttribute = function(name, value) {
            // 拦截设置 src 属性的情况
            if (name.toLowerCase() === 'src') {
                interceptScriptSrc(value);
            }

            // 调用原始的 setAttribute 方法
            originalSetAttribute.apply(this, arguments);
        };
    }

    function interceptScriptSrc(srcValue) {
        // 检查目标域名,可以根据需要修改条件
        var targetDomain = (new URL(srcValue)).hostname;

        // 如果目标域名不是当前域名,拦截请求
        if (targetDomain !== window.location.hostname) {
            console.log('拦截外链脚本请求:', srcValue);
            // 可以根据需要执行其他操作,比如取消加载
        }
    }
})();