0

Basically, in my chrome extension, it creates a redirect on certain pages within a domain. This is indeed the behavior I want it to be doing. I do not want this changed - it is required.

What I want to do instead, is to have it so that when the user presses the back button, it goes back twice when the script is run - hence circumventing the issue.

See below for an example:

1) User visits http://domain.com/page1.html
2) User clicks on the page http://domain.com/page2.html
3) Browser extension script causes it to redirect to http://domain.com/page2.html&aaa immediately.
4a) When user presses back button once, it goes back to http://domain.com/page2.html which then redirects them back to http://domain.com/page2.html&aaa, making them frustrated.
4b) When user presses back button twice in a row, it goes back to http://domain.com/page1.html like they had intended to do.

What I want to do, is add something that causes it so that whatever pages the javascript which causes the redirect is run on, also makes it so that when a user clicks the back button once, it goes back twice.


Update:

Here is what I am working on:

function backTwo()
  {
  window.history.go(-2)
  }

if {
  [SOME CODE TO SAY THAT THE STRING &aaa EXISTS IN THE URL]
}

then {
  [SOME CODE TO SAY RUN backTwo() FUNCTION]
}

else {
  [SOME CODE TO SAY DO NOTHING]
}

I'm not really sure what to do next. I'm not really a javascript coder, I'm just trying to add this feature to the extension.

If you could give me an example, it would help a lot.

progammingaddict
  • 291
  • 3
  • 14

2 Answers2

3

Instead of using background pages to modify the history navigation behaviour, avoid the problem by not creating the history entry in the first place.

For example, by using location.replace in a content script:

// Running on http://domain.com/page2.html
location.replace('http://domain.com/page2.html&aaa');

Manifest file:

  "content_scripts": [{
      "matches": ["*://domain.com/page2.html"],
      "js": ["redirect.js"],
      "run_at": "document_start",
      "all_frames": true
  }],

EDIT:
You alleged that location.replace does not fetch the resource from the server. That's not true. What you're experiencing is that your page was cached by the browser. To make sure that the page is reloaded from the server, use a cache busting method, such as appending a random query string parameter to your URL:

// Running on http://domain.com/page2.html
location.replace('http://domain.com/page2.html?aaa&_t=' + new Date().getTime());

In the previous case, you would navigate to http://domain.com/page2.html?aaa&_t=1234.... The value of 1234... is reasonably unique, so the page is never fetched your browser's local cache.

Do you control the server? If yes, you can also disable the cache through headers. For more information on this subject, see Making sure a web page is not cached, across all browsers.

Community
  • 1
  • 1
Rob W
  • 315,396
  • 71
  • 752
  • 644
2

Look into the onpopstate, you can manipulate the browsers history and push/pop the state of the browser

https://developer.mozilla.org/en-US/docs/DOM/window.onpopstate

chadpeppers
  • 1,938
  • 1
  • 17
  • 27
  • Voted you up for giving me a valuable suggestion; even if it won't help (don't know yet because I haven't read it all yet) it is good information so thanks. Do you have any possible examples I could try? – progammingaddict Apr 30 '13 at 02:29
  • Update, this is probably what I need, I found the window history option and I updated my question with more details, I hope you can help :) – progammingaddict Apr 30 '13 at 02:45