Fix remaining issues in the browsing history

This PR is a follow-up of #2921 and #3278. It fixes the remaining issue #3272, and also improves the previous fixes of issues #3271 and #3273.
This PR also fixes an issue I found, that in some cases can cause the same position to be added to the browser history twice when the hash of a loaded document is changed by the user.
Since fixing #3272 required a number of changes in the code, I've also taken the opportunity to do a little bit of refactoring of the code.

I think that the browsing history implementation in pdf.js is now finally working as it should have been from the start, so hopefully this third patch should mean that this is done.

/cc @brendandahl