Run javascript code on multiple pages of a multi-page website

I am building a google chrome extension to automate a task that requires navigation to different pages on a multi-page website. Let say on the first page there’s a button which navigates to another page on the same website, I am using javascript to click on that button so that it navigates to the desired page but after successful navigation from the first page the javascript code which I wrote for navigated page doesn’t work.

How can I make it work and again do some tasks using javascript on the navigated page?