Replacing a .js File on a Website

225 views Asked by At

So I'm trying to replace a javascript file on a website. I have tried using tamper monkey and developer tools, but none of them work? Thanks.

Original code

getAvatarUrl: function(avatar, type) {
         if (!manifest[avatar]) 
            avatar = 'base01'; 
            var version = manifest[avatar][type]; 
            return base_url + "/" + avatar + type + "." + version + ".png"; 
}

Overridden code

getAvatarUrl: function() { 
   return "Custom Image url"; 
}
2

There are 2 answers

0
vinayakj On

Add the overridden code after the original code, as javascript interprets line by line. So add below code

getAvatarUrl: function() { 
   return "Custom Image url"; 
}

After

getAvatarUrl: function(avatar, type) {
         if (!manifest[avatar]) 
            avatar = 'base01'; 
            var version = manifest[avatar][type]; 
            return base_url + "/" + avatar + type + "." + version + ".png"; 
}
0
Sibren On

I don't think you have to replace the entire file, if you simply include another file after the other files, like <script src="path/to/script.js"></src> you can override the old function by declaring a new function:

function getAvatarUrl(){
    return "Custom Image URL";
}