| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Navigation related APIs. |
| 7 */ |
| 8 |
| 9 goog.provide('__crWeb.navigation'); |
| 10 |
| 11 goog.require('__crWeb.message'); |
| 12 |
| 13 /** Beginning of anonymouse object */ |
| 14 (function() { |
| 15 |
| 16 /** |
| 17 * A popstate event needs to be fired anytime the active history entry |
| 18 * changes without an associated document change. Either via back, forward, go |
| 19 * navigation or by loading the URL, clicking on a link, etc. |
| 20 */ |
| 21 __gCrWeb['dispatchPopstateEvent'] = function(stateObject) { |
| 22 var popstateEvent = window.document.createEvent('HTMLEvents'); |
| 23 popstateEvent.initEvent('popstate', true, false); |
| 24 if (stateObject) |
| 25 popstateEvent.state = JSON.parse(stateObject); |
| 26 |
| 27 // setTimeout() is used in order to return immediately. Otherwise the |
| 28 // dispatchEvent call waits for all event handlers to return, which could |
| 29 // cause a ReentryGuard failure. |
| 30 window.setTimeout(function() { |
| 31 window.dispatchEvent(popstateEvent); |
| 32 }, 0); |
| 33 }; |
| 34 |
| 35 /** |
| 36 * A hashchange event needs to be fired after a same-document history |
| 37 * navigation between two URLs that are equivalent except for their fragments. |
| 38 */ |
| 39 __gCrWeb['dispatchHashchangeEvent'] = function(oldURL, newURL) { |
| 40 var hashchangeEvent = window.document.createEvent('HTMLEvents'); |
| 41 hashchangeEvent.initEvent('hashchange', true, false); |
| 42 if (oldURL) |
| 43 hashchangeEvent.oldURL = oldURL; |
| 44 if (newURL) |
| 45 hashchangeEvent.newURL = newURL |
| 46 |
| 47 // setTimeout() is used in order to return immediately. Otherwise the |
| 48 // dispatchEvent call waits for all event handlers to return, which could |
| 49 // cause a ReentryGuard failure. |
| 50 window.setTimeout(function() { |
| 51 window.dispatchEvent(hashchangeEvent); |
| 52 }, 0); |
| 53 }; |
| 54 |
| 55 /** |
| 56 * Keep the original pushState() and replaceState() methods. It's needed to |
| 57 * update the web view's URL and window.history.state property during history |
| 58 * navigations that don't cause a page load. |
| 59 * @private |
| 60 */ |
| 61 var originalWindowHistoryPushState = window.history.pushState; |
| 62 var originalWindowHistoryReplaceState = window.history.replaceState; |
| 63 |
| 64 __gCrWeb['replaceWebViewURL'] = function(url, stateObject) { |
| 65 originalWindowHistoryReplaceState.call(history, stateObject, '', url); |
| 66 }; |
| 67 |
| 68 /** |
| 69 * Intercept window.history methods to call back/forward natively. |
| 70 */ |
| 71 window.history.back = function() { |
| 72 __gCrWeb.message.invokeOnHost({'command': 'window.history.back'}); |
| 73 }; |
| 74 |
| 75 window.history.forward = function() { |
| 76 __gCrWeb.message.invokeOnHost({'command': 'window.history.forward'}); |
| 77 }; |
| 78 |
| 79 window.history.go = function(delta) { |
| 80 __gCrWeb.message.invokeOnHost( |
| 81 {'command': 'window.history.go', 'value': delta | 0}); |
| 82 }; |
| 83 |
| 84 window.history.pushState = function(stateObject, pageTitle, pageUrl) { |
| 85 __gCrWeb.message.invokeOnHost( |
| 86 {'command': 'window.history.willChangeState'}); |
| 87 // Calling stringify() on undefined causes a JSON parse error. |
| 88 var serializedState = |
| 89 typeof(stateObject) == 'undefined' ? '' : |
| 90 __gCrWeb.common.JSONStringify(stateObject); |
| 91 pageUrl = pageUrl || window.location.href; |
| 92 originalWindowHistoryPushState.call(history, stateObject, |
| 93 pageTitle, pageUrl); |
| 94 __gCrWeb.message.invokeOnHost( |
| 95 {'command': 'window.history.didPushState', |
| 96 'stateObject': serializedState, |
| 97 'baseUrl': document.baseURI, |
| 98 'pageUrl': pageUrl.toString()}); |
| 99 }; |
| 100 |
| 101 window.history.replaceState = function(stateObject, pageTitle, pageUrl) { |
| 102 __gCrWeb.message.invokeOnHost( |
| 103 {'command': 'window.history.willChangeState'}); |
| 104 |
| 105 // Calling stringify() on undefined causes a JSON parse error. |
| 106 var serializedState = |
| 107 typeof(stateObject) == 'undefined' ? '' : |
| 108 __gCrWeb.common.JSONStringify(stateObject); |
| 109 pageUrl = pageUrl || window.location.href; |
| 110 originalWindowHistoryReplaceState.call(history, stateObject, |
| 111 pageTitle, pageUrl); |
| 112 __gCrWeb.message.invokeOnHost( |
| 113 {'command': 'window.history.didReplaceState', |
| 114 'stateObject': serializedState, |
| 115 'baseUrl': document.baseURI, |
| 116 'pageUrl': pageUrl.toString()}); |
| 117 }; |
| 118 |
| 119 window.addEventListener('hashchange', function(evt) { |
| 120 __gCrWeb.message.invokeOnHost({'command': 'window.hashchange'}); |
| 121 }); |
| 122 |
| 123 /** Flush the message queue. */ |
| 124 if (__gCrWeb.message) { |
| 125 __gCrWeb.message.invokeQueues(); |
| 126 } |
| 127 |
| 128 }()); // End of anonymouse object |
| OLD | NEW |