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:
@@ -3,81 +3,39 @@ let headers = [];
|
|||||||
let stacja;
|
let stacja;
|
||||||
let requestResult;
|
let requestResult;
|
||||||
let decodedData;
|
let decodedData;
|
||||||
chrome.webRequest.onSendHeaders.addListener(
|
let port1;
|
||||||
(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(
|
chrome.runtime.onMessage.addListener((msg, sender) => {
|
||||||
(res) => {
|
if (msg.command === "CAPTURED") {
|
||||||
if (res.method == "POST") {
|
decodedData = msg.data;
|
||||||
if (res.url.endsWith("getgeoidx") == true) {
|
if (port1) {
|
||||||
console.log("new decoded data")
|
port1.postMessage({ command: "STARTED", data: decodedData });
|
||||||
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) {
|
chrome.runtime.onConnect.addListener((port) => {
|
||||||
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) {
|
|
||||||
port1 = port;
|
port1 = port;
|
||||||
port1.postMessage({ greeting: "hi there content script!" });
|
port1.postMessage({ greeting: "hi there content script!" });
|
||||||
port1.onMessage.addListener(function (m) {
|
port1.onMessage.addListener((m) => {
|
||||||
if (m.response == "fetchStacja") {
|
if (m.response === "fetchStacja") {
|
||||||
console.log("fetching stacja");
|
console.log("fetching stacja");
|
||||||
sendStacja();
|
sendStacja();
|
||||||
}
|
}
|
||||||
if (m.response == "getLines") {
|
if (m.response === "getLines") {
|
||||||
getLines();
|
getLines();
|
||||||
}
|
}
|
||||||
if (m.response == "getChelmiec") {
|
if (m.response === "getChelmiec") {
|
||||||
getChelmiec(m.XMIN, m.YMIN, m.XMAX, m.YMAX);
|
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);
|
getLososina(m.XMIN, m.YMIN, m.XMAX, m.YMAX);
|
||||||
}
|
}
|
||||||
if (m.response == "getNawojowa") {
|
if (m.response === "getNawojowa") {
|
||||||
getNawojowa(m.X, m.Y);
|
getNawojowa(m.X, m.Y);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
chrome.runtime.onConnect.addListener(connected);
|
|
||||||
|
|
||||||
function getLines() {
|
function getLines() {
|
||||||
const URL = "https://pdse.tauron-dystrybucja.pl/backend/api/getgeoidx";
|
const URL = "https://pdse.tauron-dystrybucja.pl/backend/api/getgeoidx";
|
||||||
@@ -85,7 +43,7 @@ function getLines() {
|
|||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
accept: "application/json, text/plain, */*",
|
accept: "application/json, text/plain, */*",
|
||||||
tokenauthorization: headers[0][1].value,
|
tokenauthorization: "",
|
||||||
userobjcfg: "default",
|
userobjcfg: "default",
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -149,11 +107,6 @@ sendReq = function (word) {
|
|||||||
.then((data) => console.log(data));
|
.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) {
|
function getChelmiec(XMIN, YMIN, XMAX, YMAX) {
|
||||||
let X = XMIN + Math.abs((XMAX - XMIN) / 2);
|
let X = XMIN + Math.abs((XMAX - XMIN) / 2);
|
||||||
|
|||||||
BIN
js/core.js
BIN
js/core.js
Binary file not shown.
38
js/inject.js
Normal file
38
js/inject.js
Normal 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
11
js/intercept.js
Normal 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 });
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user