Skip to content

Instantly share code, notes, and snippets.

@janfabian
Last active January 18, 2025 13:08
Show Gist options
  • Save janfabian/3206b19043157d0c74f347f090687757 to your computer and use it in GitHub Desktop.
Save janfabian/3206b19043157d0c74f347f090687757 to your computer and use it in GitHub Desktop.
replace-inj-sentry-host.user.js
// ==UserScript==
// @name Replace RPC and Rest injective sentry in Requests
// @version 1.0
// @description Replaces the host for outgoing network requests (fetch & XHR)
// @match https://market.injera.io/*
// @match https://injera.io/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
var oldRPC = "sentry.tm.injective.network";
var newRPC = "injective-rpc.publicnode.com";
var oldRest = "sentry.lcd.injective.network";
var newRest = "injective-rest.publicnode.com";
var oldGrpc = "sentry.chain.grpc-web.injective.network";
var newGrpc = "injective-grpc-web.publicnode.com";
// --------------------------------------------
// 1) Intercept fetch
// --------------------------------------------
const originalFetch = window.fetch;
window.fetch = async function (input, init) {
// 'input' can be a string (URL) or a Request object
let url = (typeof input === 'string') ? input : input.url;
// Build a URL object to manipulate easily
const urlObj = new URL(url, location.origin);
if (urlObj.host === oldRPC) {
urlObj.host = newRPC;
}
if (urlObj.host === oldRest) {
urlObj.host = newRest;
}
if (urlObj.host === oldGrpc) {
urlObj.host = newGrpc;
}
// Call the original fetch with the updated URL
return originalFetch.call(this, urlObj.toString(), init);
};
// --------------------------------------------
// 2) Intercept XMLHttpRequest (XHR)
// --------------------------------------------
const origOpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function (method, url, ...args) {
// Create a URL object, relative to current page if needed
const urlObj = new URL(url, location.origin);
if (urlObj.host === oldRPC) {
urlObj.host = newRPC;
}
if (urlObj.host === oldRest) {
urlObj.host = newRest;
}
if (urlObj.host === oldGrpc) {
urlObj.host = newGrpc;
}
// Call the original XHR .open() with our modified URL
return origOpen.call(this, method, urlObj.toString(), ...args);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment