Skip to content
Advertisement

Inject Javascript code into a web page

I’d like to download web page as html file. Before I save the web page source code in html file, I’d like to edit some of the page content first. I assume I can edit the content using Javascript. Unfortunately I have little experience with Javascript. I guess I have to inject my script into the web page so that the browser can execute them together. How should I write my script? Should I write a standalone script and pass the page url to my script so that they can be executed at the same time? Or there are other ways to inject my script?

EDIT: To make my problem clear, see this post and this post

Advertisement

Answer

As you are only doing this once, starting your script from the browsers JavaScript console should be enough. Open the developer tools, navigate to the console tab, paste your script content, and press enter.

To get the edited HTML, evaluate the expression document.documentElement.outerHTML in the console. Copy the output to a text editor of your choice, prepend it with a doctype, and save it as html.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement