To modify the response body of HTTP requests after they are made using the Tampermonkey browser extension, you can use several approaches. Below are the main methods:
1. Overriding XMLHttpRequest or fetch
Tampermonkey scripts can intercept and modify HTTP requests by overriding the XMLHttpRequest or fetch methods. This allows you to manipulate the response body before it is processed by the website.
Example for XMLHttpRequest:
(function() {
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
this._url = url; // Store the URL for later use
return originalOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function(body) {
this.addEventListener('readystatechange', function() {
if (this.readyState === 4 && this.status === 200) {
// Modify the response body here
let modifiedResponse = this.responseText.replace('original', 'modified');
Object.defineProperty(this, 'responseText', { value: modifiedResponse });
}
});
return originalSend.apply(this, arguments);
};
})();
This script intercepts XMLHttpRequest calls, modifies the response text, and replaces it before the website processes it[2][7].
Example for fetch:
(function() {
const originalFetch = window.fetch;
window.fetch = async function(input, init) {
const response = await originalFetch(input, init);
const clonedResponse = response.clone();
const text = await clonedResponse.text();
const modifiedText = text.replace('original', 'modified');
return new Response(modifiedText, {
status: response.status,
statusText: response.statusText,
headers: response.headers
});
};
})();
This method intercepts fetch requests and modifies the response body before returning it to the website[2][7].
2. Using GM_xmlhttpRequest
Tampermonkey provides the GM_xmlhttpRequest API, which allows you to make HTTP requests and manipulate their responses. You can use this to replace the website's requests with your own.
Example:
GM_xmlhttpRequest({
method: "GET",
url: "https://example.com/api",
onload: function(response) {
let modifiedResponse = response.responseText.replace('original', 'modified');
console.log(modifiedResponse); // Use the modified response
}
});
This approach is useful for making custom requests and handling the responses independently of the website's logic[1][2].
3. Using GM_webRequest
The GM_webRequest API allows you to intercept and modify HTTP requests and responses. This is particularly useful for modifying response bodies directly.
Example:
GM_webRequest([
{
selector: '*://example.com/api/*',
action: {
redirect: {
from: '(.*)',
to: (match) => {
// Modify the response body dynamically
return match[1].replace('original', 'modified');
}
}
}
}
]);
This method is more advanced and requires specific permissions in the script's metadata[1][2].
4. Using External Tools (e.g., Requestly)
If Tampermonkey's capabilities are insufficient, you can use tools like Requestly to modify HTTP responses. Requestly allows you to create rules to intercept and modify response bodies programmatically or statically.
Steps:
- Install the Requestly browser extension.
- Create a "Modify API Response" rule.
- Specify the URL to intercept and provide the modified response body.
- Save the rule and refresh the target website to see the changes[3][6].
Limitations
- Tampermonkey cannot modify response headers due to browser security restrictions[5].
- Some advanced features, like modifying WebSocket responses, may require custom browser extensions or external tools.
By combining these methods, you can effectively intercept and modify HTTP response bodies in Tampermonkey scripts.