undefinedfix
Sign in

How to modify the JS of a web page and run it?

alxh28 edited in Tue, 28 Jun 2022

The JS code of a web page is wrong, but I want to run this web page again. I have to find a way to modify its JS code.

Its JS code is in the script element and will be executed when the page is loaded. I have two ideas now

1. Directly modify the code in the script element, and when refreshing, do not request a new page from the server, replace it with the modified one

2. Write an extension to modify the error code before the browser resolves to the error code

However, I don't know if it can be realized. Maybe there are other ways?

7 Replies
Chuck
commented on Tue, 28 Jun 2022

If you can open this link, I'd like to add one more thing( developers.google.com )

clipboard.png

guybrush
commented on Wed, 29 Jun 2022

Entering new code in the console can be executed directly, but what has already been executed can't be done. Theoretically, you can clear the page and write it again, but considering the workload, it doesn't make sense

Prof
commented on Wed, 29 Jun 2022

The HTTP request process includes sending a request, the server responding to the request, receiving the response, and browser rendering. In your case, the server can't move. If the browser moves, it may be too troublesome as @ ostrich bird said. You can directly intercept the request in the middle and replace it with your own page, Other requests are not intercepted. The most suitable one is to use proxy, build one with koa, or use anyproxy directly

user1108069
commented on Wed, 29 Jun 2022

Man in the middle attack can be realized, but considering what you said, I temporarily understand it as: the code of the page is embedded in HTML, not external.

I have tried some methods before, such as using scripts to execute the contents of the scripts before loading the page. If the error in your script is a function, or something like that, you can use it in the script Object.defineProperty Or proxy, hijack the variable or function in advance.

If you want to do man in the middle attack, tamper with the page before the arrival of the page, as said upstairs, you can use some specific proxy scheme

Ar2Dee2
commented on Wed, 29 Jun 2022

Use the proxy (Charles) to proxy that JS file to your local file, so that you can write your own JS

obeha
commented on Thu, 30 Jun 2022

The last method I use is to modify JS through the firebox extension.

Using the webRequest.filterResponseDataAPI , you can modify the contents of the HTML file before rendering, so that you can correct the wrong JS code.

But webrequest . It seems that filterresponsedata can only be used in foxes, document link

panzi
commented on Thu, 30 Jun 2022

Think about the life cycle of page opening.

Let's see which phase can inject code.

The effective local code of chrome can only be effective but not executed and HTML code.

lock This question has been locked and the reply function has been disabled.