Search code examples
javascripthtmlunity-game-engineembedgreasemonkey

Greasemonkey replace unityObject source in webpage


I'm completely new to Greasemonkey, and I searched for quite a bit looking for what I want to do and didn't find anything. I'm doing some whitehat hacking for this browser-based Unity FPS, and I'm trying to make the webpage embed a direct-linked edited copy of the game file instead of the normal game client. Specifically, I'm trying to change the URL in the second attribute of the UnityObject.embedUnity() function on line 335 of the source of this page. I figure Greasemonkey is the way to go with it.

My confusion is coming from the fact that viewing the source (ctrl+u) shows this source in a javascript function, but using the inspector in dev tools shows the result of the function in an embed tag. I'm not sure which to try to account for in my Greasemonkey script. I don't have the direct link file up yet, but I tried this script (which didn't work) (google is just a placeholder to see if the text replacement works):

var tags = document.getElementsByTagName('embed'); tags[0].src = tags[0].src.replace('http://oldurl.com', 'http://newurl.com')

How would I go about writing a script to do this?


Solution

  • That embed element is inserted by Unity Player js code residing inside <script> tag of the page, which is executed automatically when the browser parses it.

    Firefox still has beforescriptexecute event you can use to change that script before it runs:

    document.addEventListener("beforescriptexecute", replacePlayer, true);
    function replacePlayer(e) {
        var lookFor = 'http://data.warmerise.com/Warmerise/Production/Warmerise.unity3d?5.7.5';
        var replaceWith = 'http://google.com';
        if (e.target.text.indexOf(lookFor) > 0) {
            e.target.text = e.target.text.replace(lookFor, replaceWith);
        }
    }