Skip to content

Instantly share code, notes, and snippets.

@mlnrDev
Last active September 17, 2023 19:02
Show Gist options
  • Save mlnrDev/6a63e852e35bd81fe5f32b33ce6aa904 to your computer and use it in GitHub Desktop.
Save mlnrDev/6a63e852e35bd81fe5f32b33ce6aa904 to your computer and use it in GitHub Desktop.
Intercepts Twitter requests to remove ads
// ==UserScript==
// @name Remove Twitter ads at request level
// @description Intercepts Twitter requests to remove ads
// @version 1.0
// @author cane
// @match https://twitter.com/*
// @icon https://www.google.com/s2/favicons?domain=twitter.com
// @homepage https://gist.github.com/mlnrDev/6a63e852e35bd81fe5f32b33ce6aa904
// @downloadURL https://gist.github.com/mlnrDev/6a63e852e35bd81fe5f32b33ce6aa904/raw/remove-twitter-ads.user.js
// @updateURL https://gist.github.com/mlnrDev/6a63e852e35bd81fe5f32b33ce6aa904/raw/remove-twitter-ads.user.js
// @run-at document-start
// ==/UserScript==
(function () {
const oldOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url) {
const request = new URL(url);
if (method == "GET") {
const params = request.searchParams;
const variables = params.get("variables");
if (variables) {
const json = JSON.parse(variables);
if (json.includePromotedContent) {
json.includePromotedContent = false;
params.set("variables", JSON.stringify(json));
url = request.toString();
}
}
} else if (request.pathname.endsWith("HomeLatestTimeline")) {
this._timelineRequest = true;
}
oldOpen.apply(this, arguments);
};
const oldSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function (body) {
if (this._timelineRequest) {
const json = JSON.parse(body);
json.variables.includePromotedContent = false;
body = JSON.stringify(json);
}
oldSend.apply(this, arguments);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment