Add ws blink mod that actually works

This commit is contained in:
ZXMushroom63 2024-06-02 14:33:19 +08:00
parent b9b9ad2594
commit 348003e4d6
1 changed files with 33 additions and 0 deletions

33
ExampleMods/blink_ws.js Normal file
View File

@ -0,0 +1,33 @@
//Blink hack mod that prototype pollutes WebSocket to implement itself.
var blinking = false;
var backlog = [];
const originalSend = WebSocket.prototype.send;
Object.defineProperty(WebSocket.prototype, 'send', {
configurable: true,
enumerable: false,
writable: false,
value: function(data) {
if (blinking) {
backlog.push({data: data, thisArg: this});
} else {
originalSend.call(this, data);
}
}
});
ModAPI.addEventListener("key", (ev)=>{
if (ev.key === 48) {
ev.preventDefault = true;
blinking = !blinking;
if (blinking === false) {
for (let i = 0; i < backlog.length; i++) {
const backlogItem = backlog[i];
originalSend.call(backlogItem.thisArg, backlogItem.data);
}
backlog = [];
}
}
});