Veverke
Veverke

Reputation: 11358

Handle document.location (href) changes

I am trying to automate navigation through search results. For instance, amazon's search results page like this one (purposely small). I can't find many posts on the topic, and most of what is out there like this one mention handling events such as window's popstate, hashchange. But they will not fire.

Code snippet:

window.addEventListener('popstate', () => console.log('pop state changed'))
window.addEventListener('hashchange', () => console.log('hash changed'))

document.location.assign('/s?k=comic+books&i=digital-text&rh=n%3A156104011%2Cp_n_feature_thirty-three_browse-bin%3A18116648011&dc&page=2&qid=1588508957&rnid=18116644011&swrs=A812F8AB9BA7A775DB9E4C23ADDCD5B3&ref=sr_pg_1') 
//tried ...location = '...' as well

Tried document.onload as well... to no avail. Seems like the window object is reset when document.location changes (properties I attach to the windows object are lost after document.location changes)

I want to automatically start executing some code after I manually navigate to the next results page. How do I accomplish that ?

Upvotes: 0

Views: 482

Answers (1)

Quentin
Quentin

Reputation: 943211

You can't. A browser's Console runs code in the context of the current page.

If you navigate to a new page then the environment the code was running in goes away and so does everything the code you run in the console left in memory (such as variables and event handlers).

You need to write a browser extension to achieve this.

Upvotes: 1

Related Questions