0

A website contains a "random" link, which loads a url that returns a 307 redirecting to the url we want. It works fine: click it and you load a random page. The problem is that each time you click it, the browser assumes you're loading the same page: so if you're on the homepage, then you follow the random link 5 times, then you press back, you'll be taken all the way back to the homepage, with no way to find the random pages you were just looking at. I want to modify this behavior so that users can access previous random pages via the back and forward buttons.

I don't own the website, so I can't just change the redirect code.

Here's what I've tried, all of which has failed.

  • Predicting what would be redirected to. While somewhat possible, there would be no way to avoid failure in up to .1% of clicks, and it would react very poorly to unexpected events, like a page that's published a day late, let alone a sit structure change.
  • Loading the 307 page via ajax. The request stops at readystate == 2 and I can't access the location header.
  • Cancel the click event and instead set location.href = random_link.href. This has no effect - the new page still doesn't go into history.
  • Have the new page call history.pushState. This successfully adds the page to history, but I can't find a way to distinguish between new pages and ones being opened via the back button, so the history quickly becomes very corrupted.
  • Keeping my own history in localStorage. As above, I can't tell when the back button is being used.

I'm working on a solution that I'm pretty sure will work, involving loading the page in an iframe over the existing page and using a background process and messaging to work around the fact that content injections from chrome extensions can't access window.parent from within iframes. And using the history API to reflect the current iframe's URL in the address bar, and get the back and forwards buttons to apply to the current iframe where appropriate.

While I'm pretty sure the last solution can be made to work, it's a hideously complex and heavyweight approach to what seems like a simple problem. So I thought I'd ask you guys before I continue: any other ideas?

st-boost
  • 1,877
  • 1
  • 14
  • 17

1 Answers1

0

Have you tried storing the locations in localStorage, then hi-jacking the back button ?

I am sure you know how localStorage works, for hi-jacking the back button you can refer to this : Is there a way to catch the back button event in javascript?

T.

Community
  • 1
  • 1
tomdemuyt
  • 4,572
  • 2
  • 31
  • 60
  • Thanks, but I'd rather use the real urls, which means using the history API. And if I'm doing that I can use history events instead of polling, and we're back at the final solution in my question. – st-boost Jul 02 '12 at 21:18
  • Good enough, I thought anything is better than a hack with iFrames. – tomdemuyt Jul 02 '12 at 21:20
  • Oh, wait, I misread your answer. Yes, I did try something of the sort, and the big problem was that the page couldn't tell whether it was loading for the first time or being navigated back to, and thus didn't know whether to add itself to the history stack in localStorage. – st-boost Jul 02 '12 at 21:22