Fix regressions caused by the recent implementation of browsing history

This is a followup of #2921, that tries to address most of the reported issues. This PR should fix #3271 and #3273. Both these issues are the result of me implementing the browsing history in what can only be described in hindsight as a sub-optimal way. The reason that this wasn't done right, is that I was overly concerned about adding too many entries in the browsers history.

When writing the original PR (#2921), I did most of the testing locally (using the server) and towards the end I also tested using the extension. Once #2921 landed, I found a pretty serious bug effecting the case when the user manually changes the hash of a loaded document. This didn't show up when testing locally, but sure enough, it's in the extension. This bug had me completely stumped for a couple of hours, effectively ruining my Saturday ;-) I think that I managed to fix this as well.

Finally, in the event that more serious bugs are found and we want to easily disable the history, I've added the variable `HISTORY_DISABLED` that only needs to be set to `true` to completely disable the history. 

@brendandahl, @yurydelendik Thanks for your feedback, and please accept my apologies for not testing the code thoroughly enough before submitting the original PR!