Regenerate editingStatsId on unload

So that the ID isn't reused when the user uses the back button
to navigate back to an edit page.

It would be more natural to use pageshow for this, but that
event also fires on initial load. There is a .persisted property
that indicates whether the page was recycled, but that
property doesn't work in Chrome:
https://code.google.com/p/chromium/issues/detail?id=344507

Doing this from pagehide doesn't work either, because that
runs before unload (at least in Chrome) and causes the
abort event to be emitted with the new ID. So instead,
regenerate the ID in the unload handler after the
abort event has been sent.

Bug: T95919
Change-Id: I20a602a7896e75ffa116dcd2c137306ca84164b6
This commit is contained in:
Roan Kattouw 2015-04-19 16:47:37 -07:00 committed by Catrope
parent 1a96528f69
commit 418baf1efa

View file

@ -3,6 +3,8 @@
*/
( function ( $, mw ) {
var editingSessionId;
function logEditEvent( action, data ) {
if ( mw.loader.getState( 'schema.Edit' ) === null ) {
return;
@ -44,14 +46,14 @@
$( function () {
var $textarea = $( '#wpTextbox1' ),
editingSessionIdInput = $( '#editingStatsId' ),
editingSessionId, submitting, onUnloadFallback;
$editingSessionIdInput = $( '#editingStatsId' ),
submitting, onUnloadFallback;
// Initialize wikiEditor
$textarea.wikiEditor();
if ( editingSessionIdInput.length ) {
editingSessionId = editingSessionIdInput.val();
if ( $editingSessionIdInput.length ) {
editingSessionId = $editingSessionIdInput.val();
logEditEvent( 'ready', {
editingSessionId: editingSessionId
} );
@ -73,6 +75,15 @@
} );
}
// If/when the user uses the back button to go back to the edit form
// and the browser serves this from bfcache, regenerate the session ID
// so we don't use the same ID twice. Ideally we'd do this by listening to the pageshow
// event and checking e.originalEvent.persisted, but that doesn't work in Chrome:
// https://code.google.com/p/chromium/issues/detail?id=344507
// So instead we modify the DOM here, after sending the abort event.
editingSessionId = mw.user.generateRandomSessionId();
$editingSessionIdInput.val( editingSessionId );
return fallbackResult;
};
}