// ==UserScript== // @name 拦截非当前域名请求 // @namespace https://github.com/guoshiqiufeng // @version 1.0.0 // @description 拦截请求,过滤非当前域名请求。同域名不同端口号不会过滤。 // @author yanghq // @license MIT // @match http://*/* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @grant none // @downloadURL 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); }; })();