Last active
June 11, 2025 08:38
-
-
Save thachnuida/8e43ece0f783056ef25e1efefc8c1520 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
// Check if the script is already loaded | |
if (document.getElementById("mrh-proxy-script")) { | |
console.log("Script already loaded, skipping..."); | |
return; | |
} | |
// Add a float button to the page to show that the script is running | |
var floatButton = document.createElement("div"); | |
floatButton.style.position = "fixed"; | |
floatButton.style.bottom = "20px"; | |
floatButton.style.left = "20px"; | |
floatButton.style.backgroundColor = "rgb(0, 123, 255, 0.7)"; | |
floatButton.style.color = "white"; | |
floatButton.style.padding = "10px 20px"; | |
floatButton.style.borderRadius = "5px"; | |
floatButton.style.zIndex = "9999"; | |
floatButton.style.cursor = "pointer"; | |
floatButton.innerText = "MRH Proxy Running"; | |
document.body.appendChild(floatButton); | |
// Minimize the float button when clicked | |
floatButton.onclick = function () { | |
if (floatButton.innerText === "MRH Proxy Running") { | |
floatButton.innerText = "Proxy"; | |
} else { | |
floatButton.innerText = "MRH Proxy Running"; | |
} | |
}; | |
// Code to connect socket.io: | |
var socket; | |
var script = document.createElement("script"); | |
script.src = "https://cdn.socket.io/4.0.1/socket.io.min.js"; | |
script.id = "mrh-proxy-script"; | |
document.head.appendChild(script); | |
// Once the script is loaded, establish Socket.io connection | |
script.onload = function () { | |
socket = io("http://localhost:3000", { | |
query: { | |
url: window.location.href, // Sending the current website's URL as a query parameter | |
}, | |
}); | |
// Parse the request | |
socket.on("http_request", async (req) => { | |
const { method, url, headers, body, requestId } = req; | |
try { | |
// console.log(headers); | |
headers.role = JSON.parse(localStorage.getItem('ls.role')); | |
headers.database = JSON.parse(localStorage.getItem('ls.database')); | |
headers.authorization = 'Bearer ' + JSON.parse(localStorage.getItem('ls.authorizationData')).access_token; | |
let options = { | |
method, | |
headers, | |
}; | |
if (method !== "GET") { | |
// Only include body for non-GET requests | |
options.body = JSON.stringify(body); | |
} | |
console.log(`PROXY - Requesting ${requestId}: ${method} ${url}`); | |
const response = await fetch(url, options); | |
const resHeaders = {}; | |
for (var pair of response.headers.entries()) { | |
resHeaders[pair[0]] = pair[1]; | |
} | |
let data = ''; | |
if (resHeaders['content-type'] && resHeaders['content-type'].includes('application/json')) { | |
try { | |
data = await response.json() | |
} catch (error) { | |
} | |
} else if (resHeaders['content-type'] && resHeaders['content-type'].includes('octet-stream')) { | |
try { | |
data = await response.arrayBuffer(); | |
} catch (error) { | |
} | |
} else { | |
try { | |
data = await response.text(); | |
} catch (error) {} | |
} | |
const responseData = { | |
status: response.status, | |
data: data, | |
requestId, | |
headers: resHeaders | |
}; | |
// Send the response back to the server | |
socket.emit("http_response", responseData); | |
} catch (error) { | |
console.error(error.message); | |
// Send error response back to the server | |
socket.emit("http_response", { | |
status: 500, | |
data: { message: error.message }, | |
requestId, | |
}); | |
} | |
}); | |
}; | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment