Intercept XHR and fetch, block requests that contain "viewSeenAt" (in URL or body)
当前为
// ==UserScript==
// @name IG Anon
// @version 1.0
// @description Intercept XHR and fetch, block requests that contain "viewSeenAt" (in URL or body)
// @author SilentArt
// @match https://www.instagram.com/*
// @grant none
// @run-at document-start
// @namespace https://greasyfork.org/users/1521018
// ==/UserScript==
(function () {
'use strict';
const URL_BLOCK_PATTERNS = [
'viewSeenAt',
'/reel/seen',
'story_media_viewer',
'reel_media_viewer',
'story_seen',
'reel_media_seen'
];
function shouldBlockUrl(url) {
if (!url) return false;
try {
const u = String(url);
for (const p of URL_BLOCK_PATTERNS) {
if (u.indexOf(p) !== -1) return true;
}
if (u.includes('story') && u.includes('seen')) return true;
if (u.includes('reel') && u.includes('seen')) return true;
} catch (e) {}
return false;
}
function bodyContainsViewSeen(body) {
if (!body) return false;
try {
if (typeof body === 'string') {
return body.indexOf('viewSeenAt') !== -1;
}
if (body instanceof FormData) {
for (const pair of body.entries()) {
try {
if (String(pair[1]).indexOf('viewSeenAt') !== -1) return true;
} catch (e) {}
}
}
return String(body).indexOf('viewSeenAt') !== -1;
} catch (e) { return false; }
}
(function patchFetch() {
if (!window.fetch) return;
const origFetch = window.fetch.bind(window);
window.fetch = async function (input, init) {
try {
let url = '';
let bodyText = '';
if (typeof input === 'string') {
url = input;
} else if (input instanceof Request) {
url = input.url;
try {
bodyText = await input.clone().text().catch(() => '');
} catch (e) {
bodyText = '';
}
}
if (init && init.body) {
if (typeof init.body === 'string') bodyText = init.body;
else if (init.body instanceof FormData) {
for (const pair of init.body.entries()) {
if (String(pair[1]).includes('viewSeenAt')) {
bodyText = String(pair[1]);
break;
}
}
} else {
try { bodyText = String(init.body); } catch (e) { bodyText = ''; }
}
}
if (shouldBlockUrl(url) || (bodyText && bodyText.includes('viewSeenAt'))) {
console.info('[IG anon] blocked fetch ->', url || '(Request object)', bodyText ? 'body contains viewSeenAt' : '');
return Promise.resolve(new Response(null, {
status: 204,
statusText: 'No Content (blocked by userscript)'
}));
}
} catch (e) {
}
return origFetch(input, init);
};
})();
(function patchXHR() {
const OriginalXHR = window.XMLHttpRequest;
if (!OriginalXHR) return;
function PatchedXHR() {
const xhr = new OriginalXHR();
const origOpen = xhr.open;
const origSend = xhr.send;
xhr.__user_url = null;
xhr.__shouldBlockByUserscript = false;
xhr.open = function (method, url) {
try {
xhr.__user_url = url;
if (shouldBlockUrl(url)) {
xhr.__shouldBlockByUserscript = true;
}
} catch (e) {}
return origOpen.apply(this, arguments);
};
xhr.send = function (body) {
try {
if (xhr.__shouldBlockByUserscript || bodyContainsViewSeen(body)) {
console.info('[IG anon] blocked XHR ->', xhr.__user_url || '(unknown)', bodyContainsViewSeen(body) ? 'body contains viewSeenAt' : '');
setTimeout(() => {
try {
try { xhr.readyState = 4; } catch (e) {}
try { Object.defineProperty(xhr, 'status', { value: 204, configurable: true }); } catch (e) {}
try { Object.defineProperty(xhr, 'responseText', { value: '', configurable: true }); } catch (e) {}
if (typeof xhr.onreadystatechange === 'function') {
try { xhr.onreadystatechange(); } catch (e) {}
}
try { xhr.dispatchEvent(new Event('load')); } catch (e) {}
} catch (e) {}
}, 0);
return;
}
} catch (e) {
}
return origSend.apply(this, arguments);
};
return xhr;
}
PatchedXHR.prototype = OriginalXHR.prototype;
window.XMLHttpRequest = PatchedXHR;
})();
console.info('Script active - watching for viewSeenAt patterns');
})();