Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wsHook.before/after not triggering #5

Closed
ghost opened this issue Sep 10, 2019 · 10 comments
Closed

wsHook.before/after not triggering #5

ghost opened this issue Sep 10, 2019 · 10 comments

Comments

@ghost
Copy link

ghost commented Sep 10, 2019

I have this code to inject the script into the page once loaded. I can access wsHook from the console, however before/after never trigger.
Am I missing a step or is this not a possible implementation?

window.onload = () => {
    // make websocket hook
    let websockethook = document.createElement('script');
    websockethook.setAttribute('src', 'https://cdn.rawgit.com/skepticfx/wshook/master/wsHook.js');
    websockethook.setAttribute('type', 'text/javascript');

    document.body.appendChild(websockethook);

    wsHook.before = function (data, url) {
        console.log("Sending message to " + url + " : " + data);
        console.log(1)
        return data;
    }
    
    // Make sure your program calls `wsClient.onmessage` event handler somewhere.
    wsHook.after = function (messageEvent, url, wsObject) {
        console.log("Received message from " + url + " : " + messageEvent.data);
        return messageEvent;
    }
};
@ghost
Copy link
Author

ghost commented Sep 10, 2019

I've changed my code a bit to make sure I was okay on my end.

// make websocket hook
let websockethook = document.createElement('script');
websockethook.setAttribute('type', 'text/javascript');
document.body.appendChild(websockethook);

websockethook.onload = () => {
    wsHook.before = function (data, url) {
        console.log("Sending message to " + url + " : " + data);
        return data;
    }
}
websockethook.setAttribute('src', 'https://cdn.rawgit.com/skepticfx/wshook/master/wsHook.js');

In the console wsHook.before.toString() returns the updated function, however it does not ever trigger.

@skepticfx
Copy link
Owner

It seems to be working fine for me when the page uses a simple websocket like https://websocket.org/echo.html

Maybe your page uses a different WebSocket implementation?

image

@ghost
Copy link
Author

ghost commented Sep 10, 2019

Well, that's unfortunate, just tested in chrome and firefox (not with echo but the site I'm looking at) same way you did with no luck.
The site is open sourced on github but I'm assuming because of angular I can't find a way to access the websocket from a script.
If you have any ideas I'd love to hear them, otherwise feel free to close the issue. Thanks

@skepticfx
Copy link
Owner

can you point me to the website or the open source code? Would love to make it work. I want to support socket.io when I get a chance.

@skepticfx
Copy link
Owner

Also, what is your use case here? :)

@ghost
Copy link
Author

ghost commented Sep 10, 2019

Website is https://idle.land with the source code at https://github.com/IdleLands/IdleLands/
It's a game that sends playerdata through the websocket, and I'm trying to automate player actions using the websocket hook. I've been talking to the dev about it, and he is fine with any attempts I make for automation as long as it doesn't overload the server :)

So far it looks as though it is using socketcluster-client for creating the websocket. From what I've seen socketcluster-client uses the normal WebSocket constructor, but I'm still learning this stuff as I go.
And thank you for helping :)

@ghost
Copy link
Author

ghost commented Sep 11, 2019

Well, I've found the issue. The wshook was created after the websocket had already been made.
Adding // @run-at document-start and requiring the raw file fixed it.

@skepticfx
Copy link
Owner

Sorry I missed this. Am glad it works now. Where are you adding those @run-at document-start at?

@ghost
Copy link
Author

ghost commented Sep 13, 2019

I'm using tampermonkey for the user script, so just in the script's header.
This is the working code.

// ==UserScript==
// @name     wsHook Test
// @version  1
// @grant    none
// @require https://cdn.rawgit.com/skepticfx/wshook/master/wsHook.js
// @run-at document-start
// @match https://play.idle.land/*
// ==/UserScript==

wsHook.before = function (data, url, wsObject) {
    return data;
}

wsHook.after = function (data, url, wsObject) {
    return data;
}

Also worth noting, if you change //@grant none it will sandbox the code and prevent wsHook from working.

No worries anyways, simple fix and it looks very clean in the end.

@skepticfx
Copy link
Owner

I see. Thanks @BrightSchema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant