To modify the response body of HTTP requests using the Tampermonkey browser extension, you can employ several methods. Below are the approaches and techniques available:
1. Monkey Patching fetch and XMLHttpRequest
Tampermonkey scripts can intercept and modify HTTP requests and responses by overriding the fetch API or XMLHttpRequest methods. This allows you to manipulate the response body before it is processed by the website.
Example for fetch API:
const { fetch: originalFetch } = window;
window.fetch = async (...args) => {
const [resource, config] = args;
const response = await originalFetch(resource, config);
// Intercept and modify the response
const json = () => response.clone().json().then(data => {
data.modifiedKey = "modifiedValue"; // Modify the response data
return data;
});
response.json = json;
return response;
};
This code intercepts the fetch call, modifies the JSON response, and returns the altered data to the caller[[7]].
Example for XMLHttpRequest:
You can override the XMLHttpRequest prototype to intercept and modify responses:
const originalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url) {
this.addEventListener('load', function () {
if (url.includes('target-endpoint')) {
const modifiedResponse = this.responseText.replace('originalValue', 'newValue');
Object.defineProperty(this, 'responseText', { value: modifiedResponse });
}
});
originalOpen.apply(this, arguments);
};
This approach listens for the load event and modifies the response text dynamically[[4]][[7]].
2. Using Tampermonkey's GM_xmlhttpRequest
Tampermonkey provides the GM_xmlhttpRequest function, which allows you to intercept and modify HTTP requests and responses. This method is particularly useful for cross-origin requests.
Example:
GM_xmlhttpRequest({
method: "GET",
url: "https://example.com/api",
onload: function (response) {
let modifiedResponse = response.responseText.replace('originalValue', 'newValue');
console.log(modifiedResponse); // Use the modified response
}
});
This function enables you to fetch data, modify it, and use it as needed[[8]].
3. Using Requestly Extension
Although not specific to Tampermonkey, the Requestly browser extension offers a user-friendly way to modify response bodies. It supports both static and dynamic modifications using JavaScript.
Steps:
- Install the Requestly extension.
- Create a "Modify API Response" rule.
- Use JavaScript to dynamically modify the response body:
function modifyResponse({ responseJSON }) {
responseJSON.modifiedKey = "modifiedValue";
return responseJSON;
}
- Save and activate the rule to apply it to specific requests[[1]][[3]][[6]].
4. Static and Dynamic Overrides
For simpler use cases, you can use static overrides to replace the entire response body with predefined content. Dynamic overrides, on the other hand, allow you to write JavaScript logic to modify the response based on conditions.
Example:
5. Combining with Express.js for Advanced Use Cases
For more complex scenarios, you can use Tampermonkey in conjunction with a local server (e.g., Express.js) to handle and modify responses before they are sent back to the browser. This setup is useful for testing and debugging[[8]].
Summary
- Monkey Patching: Override
fetch or XMLHttpRequest to intercept and modify responses.
- Tampermonkey API: Use
GM_xmlhttpRequest for cross-origin requests.
- Requestly Extension: A GUI-based tool for modifying responses.
- Static/Dynamic Overrides: Replace or programmatically alter response bodies.
Each method has its use cases, depending on the complexity of the modifications and the level of control required.