Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

[WIP] record history from pushState and replaceState #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions app/screens/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,48 @@ const styles = StyleSheet.create({
},
});

/* eslint-disable */
const injectedJavaScript = `(${String(function() {
var pushState = window.history.pushState;
var replaceState = window.history.replaceState;

window.history.pushState = function (state, title, url) {
pushState.apply(this, arguments);
window.postMessage({
pushState: true,
state: state,
title: title,
url: url
}, '*');
};

window.history.replaceState = function (state, title, url) {
replaceState.apply(this, arguments);
window.postMessage({
replaceState: true,
state: state,
title: title,
url: url
}, '*');
};
})})();`;
/* eslint-enable */

class Home extends Component {
onNavigationStateChange = (state) => {
this.props.updateWebView({
pageTitle: state.title,
currentUrl: state.url,
webCanGoBack: state.canGoBack,
isLoading: state.loading,
webCanGoForward: state.canGoForward,
});
};

onMessage = () => {
// existance of onMesasge breaks certain pages, eg. twitter.com
};

modalHeight() {
return {
height: this.props.mode === 'search' ? Dimensions.get('window').height : 0,
Expand Down Expand Up @@ -60,13 +101,9 @@ class Home extends Component {
<WebView
ref={(el) => { this.webView = el; }}
source={{ uri: this.props.url }}
onNavigationStateChange={state => this.props.updateWebView({
pageTitle: state.title,
currentUrl: state.url,
webCanGoBack: state.canGoBack,
isLoading: state.loading,
webCanGoForward: state.canGoForward,
})}
onNavigationStateChange={this.onNavigationStateChange}
injectedJavaScript={injectedJavaScript}
onMessage={this.onMessage}
/>
<View style={[styles.modal, this.modalHeight()]}>
<SearchResults
Expand Down