EaglerForge/ExampleMods/blink_ws.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-06-02 01:33:19 -05:00
//Blink hack mod that prototype pollutes WebSocket to implement itself.
2024-06-02 01:36:08 -05:00
//Blinking state
2024-06-02 01:33:19 -05:00
var blinking = false;
2024-06-02 01:36:08 -05:00
//The backlog of packets that need to be sent on disable
2024-06-02 01:33:19 -05:00
var backlog = [];
2024-06-02 01:36:08 -05:00
//Store the original, actual WebSocket send method
2024-06-02 01:33:19 -05:00
const originalSend = WebSocket.prototype.send;
2024-06-02 01:36:08 -05:00
//Override WebSocket.send, so when eagler tries to send messages, it runs our code instead
2024-06-02 01:33:19 -05:00
Object.defineProperty(WebSocket.prototype, 'send', {
configurable: true,
enumerable: false,
writable: false,
value: function(data) {
2024-06-02 01:36:08 -05:00
//If blinking, push data to backlog along with it's websocket instance.
2024-06-02 01:33:19 -05:00
if (blinking) {
backlog.push({data: data, thisArg: this});
2024-06-02 01:36:08 -05:00
} else { //Else send the data as normal
2024-06-02 01:33:19 -05:00
originalSend.call(this, data);
}
}
});
ModAPI.addEventListener("key", (ev)=>{
2024-06-02 01:36:08 -05:00
if (ev.key === 48) { //KEY_B
2024-06-02 01:33:19 -05:00
ev.preventDefault = true;
2024-06-02 01:36:08 -05:00
blinking = !blinking; //Toggle blinking boolean
if (blinking === false) { //If blink just turned off, send data.
2024-06-02 01:33:19 -05:00
for (let i = 0; i < backlog.length; i++) {
const backlogItem = backlog[i];
originalSend.call(backlogItem.thisArg, backlogItem.data);
}
backlog = [];
}
}
});