Skip to content

Commit

Permalink
extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Aug 10, 2024
1 parent 332cb58 commit e786101
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "OldTweetDeck",
"description": "Returns old TweetDeck, for free!",
"version": "4.0.0",
"version": "4.0.1",
"manifest_version": 3,
"homepage_url": "/~https://github.com/dimdenGD/OldTweetDeck",
"permissions": [
Expand Down
2 changes: 1 addition & 1 deletion src/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function solveChallenge(path, method) {
solveCallbacks[id].reject('Solver timed out');
delete solveCallbacks[id];
}
}, 300);
}, 500);
}
});
}
Expand Down
54 changes: 44 additions & 10 deletions src/interception.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const PUBLIC_TOKENS = [
];
const NEW_API = `https://${location.hostname}/i/api/graphql`;
const cursors = {};
const OTD_INIT_TIME = Date.now();

const generateID = () => {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
Expand Down Expand Up @@ -1763,7 +1764,12 @@ XMLHttpRequest = function () {
this.proxyRoute.beforeRequest(this);
}

this.open(this.modMethod, this.modUrl, async, username, password);
// both handlers must be set, because if openHandler never opens the request 'send' will always error
if(this.proxyRoute && this.proxyRoute.openHandler && this.proxyRoute.sendHandler) {
this.proxyRoute.openHandler(this, this.modMethod, this.modUrl, async, username, password);
} else {
this.open(this.modMethod, this.modUrl, async, username, password);
}
},
setRequestHeader(name, value) {
this.modReqHeaders[name] = value;
Expand All @@ -1776,30 +1782,54 @@ XMLHttpRequest = function () {
} else {
method = method.toUpperCase();
}
if(this.modUrl.includes("api.twitter.com") || this.modUrl.includes("api.x.com") || this.modUrl.includes("twitter.com/i/api") || this.modUrl.includes("x.com/i/api")) {
if(
this.readyState === 1 &&
(
this.modUrl.includes("api.twitter.com") ||
this.modUrl.includes("api.x.com") ||
this.modUrl.includes("twitter.com/i/api") ||
this.modUrl.includes("x.com/i/api")
)
) {
if(localStorage.device_id) this.setRequestHeader('X-Client-UUID', localStorage.device_id);
if(window.solveChallenge) {
try {
this.setRequestHeader('x-client-transaction-id', await solveChallenge(parsedUrl.pathname, method));
} catch (e) {
console.error("Error solving challenge", e);
if(localStorage.secureRequests && Date.now() - OTD_INIT_TIME > 3000) {
throw e;
}
}
}
}
if (this.proxyRoute && this.proxyRoute.beforeSendHeaders) {
this.proxyRoute.beforeSendHeaders(this);
}
for (const [name, value] of Object.entries(this.modReqHeaders)) {
this.setRequestHeader(name, value);
try {
for (const [name, value] of Object.entries(this.modReqHeaders)) {
this.setRequestHeader(name, value);
}
} catch(e) {
if(!String(e).includes('OPENED')) {
console.error(e);
}
}
if (this.proxyRoute && this.proxyRoute.beforeSendBody) {
body = this.proxyRoute.beforeSendBody(this, body);
}
this.send(body);
if(this.proxyRoute && this.proxyRoute.sendHandler) {
this.proxyRoute.sendHandler(this, body);
} else {
this.send(body);
}
},
get(xhr, key) {
if (!key in xhr) return undefined;
if (key === "responseText" && xhr._responseText) return xhr._responseText;
if (key === "responseText") return this.interceptResponseText(xhr);
if (key === "readyState" && xhr._readyState) return xhr._readyState;
if (key === "status" && xhr._status) return xhr._status;
if (key === "statusText" && xhr._statusText) return xhr._statusText;

let value = xhr[key];
if (typeof value === "function") {
Expand All @@ -1813,6 +1843,9 @@ XMLHttpRequest = function () {
if (key in xhr) {
xhr[key] = value;
}
if(key === "onload") {
xhr.onloadFn = value;
}
return value;
},
interceptResponseText(xhr) {
Expand All @@ -1829,7 +1862,8 @@ XMLHttpRequest = function () {
getAllResponseHeaders() {
let headers = this.getAllResponseHeaders();

if (this.proxyRoute && this.proxyRoute.responseHeaderOverride) {
let override = this.responseHeaderOverride ? this.responseHeaderOverride : this.proxyRoute ? this.proxyRoute.responseHeaderOverride : undefined;
if (this.proxyRoute && override) {
let splitHeaders = headers.split("\r\n");
let objHeaders = {};
for (let header of splitHeaders) {
Expand All @@ -1838,10 +1872,10 @@ XMLHttpRequest = function () {
let headerValue = splitHeader[1];
objHeaders[headerName.toLowerCase()] = headerValue;
}
for(let header in this.proxyRoute.responseHeaderOverride) {
objHeaders[header.toLowerCase()] = this.proxyRoute.responseHeaderOverride[header]();
for(let header in override) {
objHeaders[header.toLowerCase()] = override[header]();
}
headers = Object.entries(objHeaders).map(([name, value]) => `${name}: ${value}`).join("\r\n");
headers = Object.entries(objHeaders).filter(([_, value]) => value).map(([name, value]) => `${name}: ${value}`).join("\r\n");
}

return headers;
Expand Down

0 comments on commit e786101

Please sign in to comment.