Xosrov
Xosrov

Reputation: 729

Overriding core JS commands?

I'm trying to modify/limit/prevent access to certain JS commands of my browser. For example commands like navigator.clipboard; However, I'm not sure how to approach this.

Is it possible to override these commands with user-defined javascript injected in the page, or do i have to edit the browser's javascript compiler and re-compile it from source for this?

I'm not really familiar with browsers and want to save time by knowing a general direction to follow. Thanks

Upvotes: 0

Views: 89

Answers (1)

MauriceNino
MauriceNino

Reputation: 6757

First of all navigator.clipboard is not a function, but here is an example using the read function of navigator.clipboard:

navigator.clipboard.read = function (originalFunction) {
    return function (yourParamsYouWantForThisFunction) {
        // Do Stuff you wanna do before the real call. For example:
        console.log(yourParamsYouWantForThisFunction);

        // Call the original function
        return originalFunction.call();
    };
}(navigator.clipboard.read); // Pass the original function reference as a parameter

You may wonder, why there are two function statements:

  • The first one is there, so that we can pass the original function at runtime. If we would not do that, we would not be able to access the original navigator.clipboard.read function.
  • The second function is the actual function, that you will be using later, when you call navigator.clipboard.read().

Upvotes: 1

Related Questions