Skip to content

Instantly share code, notes, and snippets.

@scottschreckengaust
Created January 5, 2024 05:34
Show Gist options
  • Save scottschreckengaust/19fed399d1c64a40c77a953b239b1699 to your computer and use it in GitHub Desktop.
Save scottschreckengaust/19fed399d1c64a40c77a953b239b1699 to your computer and use it in GitHub Desktop.
Creating a WebSocket Listener
// ==UserScript==
// @name A Test
// @namespace com.github
// @version 0.0.0
// @description A Test
// @author Scott Schreckengaust <scottschreckengaust@users.noreply.github.com>
// @match http://*/*
// @match https://*/*
// @grant none
// @antifeature
// @noframes
// @sandbox JavaScript
// @run-at document-start
// ==/UserScript==
/* if grants below replace `none` above websocket messages aren't received */
// @grant GM_addElement
// @grant GM_addStyle
// @grant GM_addValueChangeListener
// @grant GM_cookie
// @grant GM_deleteValue
// @grant GM_download
// @grant GM_getTab
// @grant GM_getTabs
// @grant GM_getResourceText
// @grant GM_getResourceURL
// @grant GM_getValue
// @grant GM_info
// @grant GM_listValues
// @grant GM_log
// @grant GM_notification
// @grant GM_openInTab
// @grant GM_registerMenuCommand
// @grant GM_removeValueChangeListener
// @grant GM_saveTab
// @grant GM_setClipboard
// @grant GM_setValue
// @grant GM_unregisterMenuCommand
// @grant GM_xmlhttpRequest
// @grant unsafeWindow
// @grant window.onurlchange
// @grant window.close
// @grant window.focus
// https://github.com/Tampermonkey/tampermonkey/issues/1961
// NOTE: Testing with Tampermonkey v5.0.1 in Chrome 120.0.6099.199 (Official Build) (arm64) on macOS 14.2.1 (23C71)
(function aTest() {
'use strict';
let toggled = false
function testMenu() {
try {
if (typeof(GM_registerMenuCommand) === "object") {
const menu_command_id_1 = GM_registerMenuCommand("Toggle", function(event) {
toggled = !toggled;
if (toggled) {
alert("On");
} else {
alert("Off");
}
}, {
accessKey: "t",
autoClose: true
});
} else {
console.warn("GM_registerMenuCommand doesn't exist");
}
} catch (testMenuError) {
console.error(testMenuError);
}
}
try {
testMenu();
const OriginalWebSocket = window.WebSocket;
class UpdatedWebSocket extends OriginalWebSocket {
constructor(url, protocols) {
try {
console.debug("constructor START");
super(url, protocols);
const createEventListener = (name) => {
console.debug("createEventListener");
return (event) => {
try {
console.debug("(event) START");
console.info(event);
} catch (webSocketError) {
console.error(webSocketError);
} finally {
console.debug("(event) FINALLY");
}
};
};
this.addEventListener('open', createEventListener('open'));
this.addEventListener('close', createEventListener('close'));
this.addEventListener('message', createEventListener('message'));
this.addEventListener('error', createEventListener('error'));
console.debug("constructor END");
} catch (constructorError) {
console.error(constructorError);
}
}
}
console.debug(window.WebSocket, "window.WebSocket (same as OriginalWebSocket)");
window.WebSocket = UpdatedWebSocket;
console.debug(window.WebSocket, "window.WebSocket (should be UpdatedWebSocket)");
console.debug("SUCCESS");
} catch (error) {
console.error(error);
} finally {
console.debug("FINALLY");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment