N
N
NooBick2021-08-15 03:01:28
Google Chrome
NooBick, 2021-08-15 03:01:28

How to disable JavaScript injection from the site through userscripts?

How to disable JavaScript injection from the site through userscripts?

I have a site Vkontakte, there is a button - send a message.
6118bc105f25f011844894.png

I need my code to work when I click on this button, not the website code. If you simply cancel the request to the server for this javascript, then all the functionality of the site will not work. I need to somehow change the response body, or rather, after the request is executed, not much change this javascript. chrome.webRequest can't. Then I decided to simply remove the loading of this script at the beginning of the page load, but this didn’t work out for me either, the browser connects this JavaScript faster than my extension.

Then my eyes fell on userscripts. And maybe somehow through it, you can remove the script loading from html in order to insert my script. Or is it still possible to somehow change the response body so that I can edit the javascript that the server sends?

In short, I've been steaming with all this for about 5 days. I want to solve this problem. And for this, only one server-side JavaScript code bothers me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WbICHA, 2021-08-15
@NooBick

On the element where the event handler hangs , you need to hang:

.addEventListener('click', event => {
  event.stopPropagation()
  ...
}, true);

Be sure to pass true as the third argument ( https://developer.mozilla.org/ru/docs/Web/API/Even... ).

N
Nadim Zakirov, 2021-08-15
@zkrvndm

You can easily rewrite VKontakte scripts to your own through the content script / UserScript, just declare your function and it will stupidly replace the function from VKontakte.
An example of declaring the test() function via the content script / UserScript:

var script = document.createElement('script');
script.innerHTML = `function test() {
    // Ваш код
}`;
document.head.appendChild(script);

Of course, you need to declare after loading the scripts from VKontakte, for this, use the load event.
PS It remains only for you to find the function from VKontakte responsible for sending data) To do this, examine the handlers that hang on the button using the chrome inspector.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question