{"id":21271261,"url":"https://github.com/skepticfx/wshook","last_synced_at":"2025-04-10T02:28:34.444Z","repository":{"id":28848169,"uuid":"32372108","full_name":"skepticfx/wshook","owner":"skepticfx","description":"Easily intercept and modify WebSocket requests and message events.","archived":false,"fork":false,"pushed_at":"2020-09-21T20:55:22.000Z","size":105,"stargazers_count":249,"open_issues_count":1,"forks_count":55,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-24T07:56:50.203Z","etag":null,"topics":["hooks","javascript","websockets"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skepticfx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-17T04:47:48.000Z","updated_at":"2025-03-13T05:41:36.000Z","dependencies_parsed_at":"2022-09-14T10:23:45.933Z","dependency_job_id":null,"html_url":"https://github.com/skepticfx/wshook","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skepticfx%2Fwshook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skepticfx%2Fwshook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skepticfx%2Fwshook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skepticfx%2Fwshook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skepticfx","download_url":"https://codeload.github.com/skepticfx/wshook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246911497,"owners_count":20853657,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","javascript","websockets"],"created_at":"2024-11-21T08:21:33.998Z","updated_at":"2025-04-03T00:07:17.238Z","avatar_url":"https://github.com/skepticfx.png","language":"JavaScript","readme":"# wsHook\n#### Easily intercept and modify WebSocket requests and message events.\n\n\n#### ToDo\nFigure out if we still need immutable MessageEvent\n\n## Usage\n\n#### 1. Download and include `wsHook.js` in your WebSocket client\n\n```html\n\u003cscript src='wsHook.js'\u003e\u003c/script\u003e\n```\n\n#### 2. Define the `before` and `after` hooks\nDefine your custom `before` and `after` hooks on the globally exposed `wsHook` object.\n\n```javascript\nwsHook.before = function(data, url, wsObject) {\n    console.log(\"Sending message to \" + url + \" : \" + data);\n}\n\n// Make sure your program calls `wsClient.onmessage` event handler somewhere.\nwsHook.after = function(messageEvent, url, wsObject) {\n    console.log(\"Received message from \" + url + \" : \" + messageEvent.data);\n    return messageEvent;\n}\n\n// if you do not want to propagate the MessageEvent further down, just return null\nwsHook.after = function(messageEvent, url, wsObject) {\n console.log(\"Received message from \" + url + \" : \" + messageEvent.data);\n // This example can ping-pong forever, so maybe use some conditions\n wsObject.send(\"Intercepted and sent again\")\n return null;\n}\n\n```\n\n#### 3. Let your program play with WebSockets\n```javascript\nvar wsClient = new WebSocket(\"wss://echo.websocket.org\");\n\nwsClient.onopen = function() {\n    wsClient.send(\"Echo this\");\n}\n\nwsClient.onmessage = function(e){\n  console.log(e);\n}\n```\n## API\n### `wsHook.before` - function(data, url, wsObject):\nInvoked just before calling the actual WebSocket's `send()` method.\n\nThis method must return `data` which can be modified as well.\n\n### `wsHook.after` - function(event, url, wsObject):\nInvoked just after receiving the `MessageEvent` from the WebSocket server and before calling the WebSocket's `onmessage` Event Handler.\n\nThis method must return `event` whose properties can be modified as well. You might be interested in modiying, `event.data` or `event.origin` usually.\n\nThe `wsObject` refers to the corresponding `WebSocket` object used. You can use this to send a message to the server. This allows one to fully hijack the WebSocket connection programatically. \n\nIf you do not want the user's original `onmessage` event handler to be called, just return `null`.\n\n\n## Overview\n\u003cimg src=\"http://skepticfx.com/imgs/wshook.png\"\u003e\n\n## Example\n\n```javascript\n// Load wsHook.js\n// Define the 'before' and 'after' hooks as you wish.\n\nwsHook.before = function(data, url, wsObject){\n  data += \"_modified\";\n  console.log(\"Modifying data to \" + data);\n  return data;\n}\n\nvar wsClient = new WebSocket(\"wss://echo.websocket.org\");\nwsClient.onopen = function() {\n  wsClient.send(\"Echo this\");\n}\nwsClient.onmessage = function(e){\n  console.log(e);\n}\n\n```\n\n## Used by\n\n* [**Hookish**](https://github.com/skepticfx/hookish): Hooks in to interesting functions and helps reverse the web app faster.\n\n## TODO\n\n* Test cases for common WebSocket libraries.\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2015 Ahamed Nafeez\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskepticfx%2Fwshook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskepticfx%2Fwshook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskepticfx%2Fwshook/lists"}