-2

Let's imagine you are visiting a website that utilizes JavaScript (jquery). Of course, there are some function definitions in the script. Like this one:

$(function () {
            // Some necessary code here...
            someObject.doSomethingUndesirable();
           // Some necessary code here...
        });

My problem is that I do not want that line of code to be executed. Of course I can comment out that particular line after the page is loaded, but since the script is already in memory, the modification will not work.

So my question is simple: Can I somehow interfere with the loading process and prevent the loading (and therefore execution) of a particular line of JavaScript code?

SCilek
  • 23
  • 5

2 Answers2

0

If you can inject code before the site code runs - such as with a userscript and the @run-at document-start directive - you can overwrite $ to ignore the callback:

// Userscript code
$ = (callback) => {};

// Site code
$(function() {
  someObject.doSomethingUndesirable();
});

If $ may not be defined yet, you can also define a setter on window.$, so that its initial assignment gets ignored or overwritten:

// Userscript code
Object.defineProperty(window, '$', {
  get() {
    return (callback) => {};
  }
});

// Site code
$(function() {
  someObject.doSomethingUndesirable();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Tricker but more flexible approaches are to detect the appending of the site's <script> node and patch it, or to patch it in advance with Chrome Local Overrides or a network tool like Fiddler.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • I want to prevent the execution of that particular line of code only; the function contains useful and necessary code too. – SCilek Sep 08 '21 at 05:13
  • Please make sure your question contains all the relevant details when posting - the current question does not indicate such a thing. A specific example of what `someObject.doSomethingUndesirable();` really is could help a lot too, if it's interspersed around other innocuous code. – CertainPerformance Sep 08 '21 at 05:14
  • Fair enough... I have edited my question. – SCilek Sep 08 '21 at 05:19
  • Can you show what `someObject.doSomethingUndesirable();` really is? If the object or method can be accessed externally, it should be pretty doable. Harder otherwise. – CertainPerformance Sep 08 '21 at 05:20
  • It is kind of sensitive content, so I could not post it here. However, if you'd like to see it, I could send you an obfuscated version of it privately. – SCilek Sep 08 '21 at 05:48
  • How could it be sensitive if it's running clientside? – Sebastian Ciocarlan Sep 08 '21 at 07:19
  • It can be "kind of" sensitive if only certain people are supposed to access the content and the piece of undesirable code in question has been put there to prevent those users from circumventing an obligation, which is a drudgery. – SCilek Sep 08 '21 at 08:22
  • IDK who did that but running any code clientside to prevent users from doing anything i'ts not an effective way to do it. – Sebastian Ciocarlan Sep 08 '21 at 09:56
  • It appears it *is* an effective way to do it if the users don't know how to circumvent the prevention scheme. – SCilek Sep 09 '21 at 08:25
  • @SCilek Unless you're going to provide more details on what exactly the object is, I'm going to close this - impossible to answer at the moment – CertainPerformance Sep 09 '21 at 21:46
0

I guess you could use userscript to modify the class of the object if it's static. One way to do this is to listen on changes over the 'someObject' variable via the Proxy object. Then whenever he defines the doSomethingUndesirable function you could just replace it with a function that returns nothing, null or whatever you want.

//Init variable
var someObject = {};

var someObjectProxy = new Proxy(someObject, {
  set: function (target, key, value) {
      if(key = "doSomethingUndesirable"){
      //Redefinition of the function
      target[key] = ()=>{return null};}
      return true;
  }
});
/*....*/
//Example of definition
someObject.somethingUndesirable = ()=>{return 10+10}
//The proxy will trigger and redefine the function to return null