Add request interception for getgeoidx API calls

- Introduced a new script (inject.js) to override the fetch and XMLHttpRequest methods to capture requests to the "getgeoidx" endpoint.
- Captured request body and URL, and sent the data to the extension via postMessage.
- Added intercept.js to inject the new script into the page and listen for messages to forward captured data to the extension.
This commit is contained in:
2025-06-05 11:32:35 +02:00
parent 69fc5bcd12
commit 1e002d5bd7
4 changed files with 193 additions and 191 deletions

View File

@@ -3,81 +3,39 @@ let headers = [];
let stacja;
let requestResult;
let decodedData;
chrome.webRequest.onSendHeaders.addListener(
(res) => {
if (res.method == "POST") {
if (res.url.endsWith("getgeoidx") == true) {
console.log("new header")
//prevRequests.push(res);
headers.push(res.requestHeaders);
if (headers.length > 1) {
headers.shift();
}
}
}
},
{ urls: ["*://*.tauron-dystrybucja.pl/*"] },
["requestHeaders"]
);
chrome.webRequest.onBeforeRequest.addListener(
(res) => {
if (res.method == "POST") {
if (res.url.endsWith("getgeoidx") == true) {
console.log("new decoded data")
prevRequests.push(res);
let results = res.requestBody.raw[0].bytes;
var decoder = new TextDecoder("utf-8");
decodedData = decoder.decode(new Uint8Array(results));
port1.postMessage({
command: "STARTED",
data: decodedData,
});
if (prevRequests.length > 1) {
prevRequests.shift();
}
}
} else if (res.method == "GET") {
if (
res.url.startsWith(
"https://pdse.tauron-dystrybucja.pl/backend/api/objdata/STACJE_SN_NN"
) == true
) {
stacja = res;
port1.postMessage({ command: "gotStacja" });
}
}
},
{ urls: ["*://*.tauron-dystrybucja.pl/*"] },
["requestBody"]
);
let port1;
function connected(port) {
chrome.runtime.onMessage.addListener((msg, sender) => {
if (msg.command === "CAPTURED") {
decodedData = msg.data;
if (port1) {
port1.postMessage({ command: "STARTED", data: decodedData });
}
}
});
chrome.runtime.onConnect.addListener((port) => {
port1 = port;
port1.postMessage({ greeting: "hi there content script!" });
port1.onMessage.addListener(function (m) {
if (m.response == "fetchStacja") {
port1.onMessage.addListener((m) => {
if (m.response === "fetchStacja") {
console.log("fetching stacja");
sendStacja();
}
if (m.response == "getLines") {
if (m.response === "getLines") {
getLines();
}
if (m.response == "getChelmiec") {
if (m.response === "getChelmiec") {
getChelmiec(m.XMIN, m.YMIN, m.XMAX, m.YMAX);
}
if (m.response == "getLososina") {
if (m.response === "getLososina") {
getLososina(m.XMIN, m.YMIN, m.XMAX, m.YMAX);
}
if (m.response == "getNawojowa") {
if (m.response === "getNawojowa") {
getNawojowa(m.X, m.Y);
}
});
}
chrome.runtime.onConnect.addListener(connected);
});
function getLines() {
const URL = "https://pdse.tauron-dystrybucja.pl/backend/api/getgeoidx";
@@ -85,7 +43,7 @@ function getLines() {
headers: {
"Content-Type": "application/json",
accept: "application/json, text/plain, */*",
tokenauthorization: headers[0][1].value,
tokenauthorization: "",
userobjcfg: "default",
},
@@ -149,11 +107,6 @@ sendReq = function (word) {
.then((data) => console.log(data));
};
chrome.contextMenus.create({
title: "Więcej o stacji",
contexts: ["all"], // ContextType
onclick: sendStacja, // A callback function
});
function getChelmiec(XMIN, YMIN, XMAX, YMAX) {
let X = XMIN + Math.abs((XMAX - XMIN) / 2);
@@ -199,7 +152,7 @@ function getLososina(XMIN, YMIN, XMAX, YMAX) {
method: "POST",
referrerPolicy: "same-origin",
"credentials": "omit"
})
})
.then((response) => response.text())
.then((str) => {
console.log("http://195.116.43.211/?link=" + link);

Binary file not shown.

38
js/inject.js Normal file
View File

@@ -0,0 +1,38 @@
(function() {
const origFetch = window.fetch;
window.fetch = async function(input, init = {}) {
const url = typeof input === "string" ? input : input.url;
if (url.endsWith("getgeoidx")) {
let body = init.body || null;
if (body instanceof ArrayBuffer) {
body = new TextDecoder().decode(new Uint8Array(body));
} else if (body instanceof Blob) {
body = await new Response(body).text();
} else if (body != null && typeof body !== "string") {
try { body = JSON.stringify(body); } catch(e) { body = String(body); }
}
window.postMessage({ type: "ZMS_CAPTURE", url, body }, "*");
}
return origFetch.apply(this, arguments);
};
// patch XMLHttpRequest to catch getgeoidx
const origOpen = XMLHttpRequest.prototype.open;
const origSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function(method, url, ...rest) {
this._zms_url = url;
return origOpen.call(this, method, url, ...rest);
};
XMLHttpRequest.prototype.send = function(body) {
if (this._zms_url && this._zms_url.endsWith("getgeoidx")) {
let captured = body;
if (body instanceof ArrayBuffer) {
captured = new TextDecoder().decode(new Uint8Array(body));
} else if (body != null && typeof body !== "string") {
try { captured = JSON.stringify(body); } catch(e) { captured = String(body); }
}
window.postMessage({ type: "ZMS_CAPTURE", url: this._zms_url, body: captured }, "*");
}
return origSend.call(this, body);
};
})();

11
js/intercept.js Normal file
View File

@@ -0,0 +1,11 @@
// inject into page
const s = document.createElement("script");
s.src = chrome.runtime.getURL("js/inject.js");
(document.head||document.documentElement).appendChild(s);
// listen for page→extension
window.addEventListener("message", e => {
if (e.source === window && e.data?.type === "ZMS_CAPTURE") {
chrome.runtime.sendMessage({ command: "CAPTURED", data: e.data.body });
}
});