Skip to content

Instantly share code, notes, and snippets.

@supernovaplus
Forked from logan-mcgee/ttools.user.js
Last active August 22, 2021 21:08
Show Gist options
  • Save supernovaplus/7633b7946aea37ae8df6225d7f9b06ea to your computer and use it in GitHub Desktop.
Save supernovaplus/7633b7946aea37ae8df6225d7f9b06ea to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name TTMAP WAYPOINT
// @namespace TTMAP WAYPOINT
// @match https://ttmap.eu/*
// @version 1.0
// @author logan + nova
// @downloadURL https://gist.github.com/sadboilogan/83c0effe25741607ccfc73ca83335c59/raw/ttools.user.js
// @description Extends TTools site
// ==/UserScript==
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
let waitCounter = 0;
;(async () => {
async function request(url) {
return new Promise(resolve => {
fetch(url).then(res => res.json()).then(res => {
resolve(res);
}).catch(err => {
console.log(err)
resolve(false);
})
});
}
async function runCommand(command, data) {
const res = await sendData('Runtime.evaluate', {
expression: `new Promise((resolve) => fetch("https://remote_connector/recvData", {method: "POST", body: '{"type": "${command}", "data": ${data ? JSON.stringify(data) : '""'} }' }).then((res) => res.text()).then((res) => resolve(res)))`,
contextId: mainScriptCtx,
awaitPromise: true
});
return res;
}
let cdpId = 0;
let mainScriptCtx = null;
let isConnected = false;
while(typeof unsafeWindow.mainMap === "undefined" && waitCounter < 15){
await wait(500);
waitCounter++;
}
if(waitCounter === 30) return;
const copyMenu = document.getElementById("copy-menu");
copyMenu.appendChild(document.createElement("br"));
document.body.appendChild(document.createElement("style")).innerText = `.waypointBtn{background-color: lime;} .waypointBtn:hover{background-color: green;color:white;} .waypointBtn:active{background-color: white;}`;
const button = copyMenu.appendChild(document.createElement("input"));
button.type = "button";
button.value = "Set Waypoint";
button.className = "waypointBtn";
button.addEventListener("click", async () => {
// console.log(unsafeWindow.lastCoords);
await runCommand('setWaypoint', { x: unsafeWindow.lastCoords[0], y: unsafeWindow.lastCoords[1] });
})
//=====================
const nuiState = await request('http://localhost:13172/json/list');
if (!nuiState) {
alert('TT-Remote: No response from the server');
return;
}
let wsUrl = nuiState.find((data) => data.title === 'CitizenFX root UI' && data.url === 'nui://game/ui/root.html').webSocketDebuggerUrl;
if (!wsUrl) {
alert('TT-Remote: WS not found');
return;
}
async function onScriptCtxSet() {
alert('TT-Remote: Connected');
isConnected = true;
}
const ws = new WebSocket(wsUrl);
await new Promise(resolve => ws.addEventListener('open', resolve, { once: true }));
const sendData = (type, params) => {
const id = cdpId++;
ws.send(JSON.stringify({
id,
method: type,
params: params ?? {}
}));
return new Promise(resolve => {
ws.addEventListener('message', ({ data }) => {
const response = JSON.parse(data);
if (response?.id === id) {
ws.removeEventListener('message', arguments.callee);
resolve(response);
}
});
});
};
ws.addEventListener('message', ({ data: msgData }) => {
const response = JSON.parse(msgData);
if (response?.id) return;
if (response.method === 'Debugger.scriptParsed') {
if (response.params.url === 'nui://remote_connector/nui/main.js') {
mainScriptCtx = response.params.executionContextId;
onScriptCtxSet();
}
}
});
await sendData('Runtime.enable');
await sendData('Debugger.enable');
})().catch(err => {
console.log(err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment