How to monitor changes in webview document title with react native
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I basically try to call a function in react native as soon as the document title in my webview changes. I already get the document.title of the web page that is shown in my webview by using this code:
handleMessage(message) {
alert(message.nativeEvent.data);
}
render() {
return (
<WebView
source={{ uri: "https://website.com/" }}
injectedJavaScript="window.postMessage(document.title)"
onMessage={this.handleMessage}
/>
)
}
Now I want to monitor the title for changes and call a function as soon as this happens. How can I do this?
javascript react-native onchange
add a comment |
I basically try to call a function in react native as soon as the document title in my webview changes. I already get the document.title of the web page that is shown in my webview by using this code:
handleMessage(message) {
alert(message.nativeEvent.data);
}
render() {
return (
<WebView
source={{ uri: "https://website.com/" }}
injectedJavaScript="window.postMessage(document.title)"
onMessage={this.handleMessage}
/>
)
}
Now I want to monitor the title for changes and call a function as soon as this happens. How can I do this?
javascript react-native onchange
add a comment |
I basically try to call a function in react native as soon as the document title in my webview changes. I already get the document.title of the web page that is shown in my webview by using this code:
handleMessage(message) {
alert(message.nativeEvent.data);
}
render() {
return (
<WebView
source={{ uri: "https://website.com/" }}
injectedJavaScript="window.postMessage(document.title)"
onMessage={this.handleMessage}
/>
)
}
Now I want to monitor the title for changes and call a function as soon as this happens. How can I do this?
javascript react-native onchange
I basically try to call a function in react native as soon as the document title in my webview changes. I already get the document.title of the web page that is shown in my webview by using this code:
handleMessage(message) {
alert(message.nativeEvent.data);
}
render() {
return (
<WebView
source={{ uri: "https://website.com/" }}
injectedJavaScript="window.postMessage(document.title)"
onMessage={this.handleMessage}
/>
)
}
Now I want to monitor the title for changes and call a function as soon as this happens. How can I do this?
javascript react-native onchange
javascript react-native onchange
asked Aug 15 '18 at 17:45
Nixen85Nixen85
309213
309213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I came here looking for a solution to the same problem but couldn't find one on stackoverflow. My exact requirement was to read the title from WebView and update it on a text box somewhere else and this is how I solved it,
My injected javascript monitors changes in the URL within WebView and calls postMessage
whenever there is a change.
Here is my injected script:
window.currentloc = location.href;
setInterval(function() {
if (window.currentloc != location.href) {
window.currentloc = location.href;
window.postMessage('$#doctitle-' + document.title);
}
}, 500);
I also prefixed my message with $#doctitle-
to avoid processing all messages coming from my WebView.
My component looks like this:
<WebView
ref={r => (this.webview = r)}
style={{ flex: 1 }}
injectedJavaScript="window.currentloc = location.href;setInterval(function() {if (window.currentloc != location.href) {window.currentloc = location.href;window.postMessage('$#doctitle-' + document.title);}}, 500);"
javaScriptEnabled={true}
onMessage={this.handleMessage.bind(this)}
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
source={{
uri: 'https://website.com',
}}
/>
My message handler code:
handleMessage(event) {
if(event.nativeEvent.data && event.nativeEvent.data.indexOf("$#doctitle-")==0){
this.setState({
windowTitle: event.nativeEvent.data.split('-')[1]
});
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51863829%2fhow-to-monitor-changes-in-webview-document-title-with-react-native%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I came here looking for a solution to the same problem but couldn't find one on stackoverflow. My exact requirement was to read the title from WebView and update it on a text box somewhere else and this is how I solved it,
My injected javascript monitors changes in the URL within WebView and calls postMessage
whenever there is a change.
Here is my injected script:
window.currentloc = location.href;
setInterval(function() {
if (window.currentloc != location.href) {
window.currentloc = location.href;
window.postMessage('$#doctitle-' + document.title);
}
}, 500);
I also prefixed my message with $#doctitle-
to avoid processing all messages coming from my WebView.
My component looks like this:
<WebView
ref={r => (this.webview = r)}
style={{ flex: 1 }}
injectedJavaScript="window.currentloc = location.href;setInterval(function() {if (window.currentloc != location.href) {window.currentloc = location.href;window.postMessage('$#doctitle-' + document.title);}}, 500);"
javaScriptEnabled={true}
onMessage={this.handleMessage.bind(this)}
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
source={{
uri: 'https://website.com',
}}
/>
My message handler code:
handleMessage(event) {
if(event.nativeEvent.data && event.nativeEvent.data.indexOf("$#doctitle-")==0){
this.setState({
windowTitle: event.nativeEvent.data.split('-')[1]
});
}
}
add a comment |
I came here looking for a solution to the same problem but couldn't find one on stackoverflow. My exact requirement was to read the title from WebView and update it on a text box somewhere else and this is how I solved it,
My injected javascript monitors changes in the URL within WebView and calls postMessage
whenever there is a change.
Here is my injected script:
window.currentloc = location.href;
setInterval(function() {
if (window.currentloc != location.href) {
window.currentloc = location.href;
window.postMessage('$#doctitle-' + document.title);
}
}, 500);
I also prefixed my message with $#doctitle-
to avoid processing all messages coming from my WebView.
My component looks like this:
<WebView
ref={r => (this.webview = r)}
style={{ flex: 1 }}
injectedJavaScript="window.currentloc = location.href;setInterval(function() {if (window.currentloc != location.href) {window.currentloc = location.href;window.postMessage('$#doctitle-' + document.title);}}, 500);"
javaScriptEnabled={true}
onMessage={this.handleMessage.bind(this)}
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
source={{
uri: 'https://website.com',
}}
/>
My message handler code:
handleMessage(event) {
if(event.nativeEvent.data && event.nativeEvent.data.indexOf("$#doctitle-")==0){
this.setState({
windowTitle: event.nativeEvent.data.split('-')[1]
});
}
}
add a comment |
I came here looking for a solution to the same problem but couldn't find one on stackoverflow. My exact requirement was to read the title from WebView and update it on a text box somewhere else and this is how I solved it,
My injected javascript monitors changes in the URL within WebView and calls postMessage
whenever there is a change.
Here is my injected script:
window.currentloc = location.href;
setInterval(function() {
if (window.currentloc != location.href) {
window.currentloc = location.href;
window.postMessage('$#doctitle-' + document.title);
}
}, 500);
I also prefixed my message with $#doctitle-
to avoid processing all messages coming from my WebView.
My component looks like this:
<WebView
ref={r => (this.webview = r)}
style={{ flex: 1 }}
injectedJavaScript="window.currentloc = location.href;setInterval(function() {if (window.currentloc != location.href) {window.currentloc = location.href;window.postMessage('$#doctitle-' + document.title);}}, 500);"
javaScriptEnabled={true}
onMessage={this.handleMessage.bind(this)}
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
source={{
uri: 'https://website.com',
}}
/>
My message handler code:
handleMessage(event) {
if(event.nativeEvent.data && event.nativeEvent.data.indexOf("$#doctitle-")==0){
this.setState({
windowTitle: event.nativeEvent.data.split('-')[1]
});
}
}
I came here looking for a solution to the same problem but couldn't find one on stackoverflow. My exact requirement was to read the title from WebView and update it on a text box somewhere else and this is how I solved it,
My injected javascript monitors changes in the URL within WebView and calls postMessage
whenever there is a change.
Here is my injected script:
window.currentloc = location.href;
setInterval(function() {
if (window.currentloc != location.href) {
window.currentloc = location.href;
window.postMessage('$#doctitle-' + document.title);
}
}, 500);
I also prefixed my message with $#doctitle-
to avoid processing all messages coming from my WebView.
My component looks like this:
<WebView
ref={r => (this.webview = r)}
style={{ flex: 1 }}
injectedJavaScript="window.currentloc = location.href;setInterval(function() {if (window.currentloc != location.href) {window.currentloc = location.href;window.postMessage('$#doctitle-' + document.title);}}, 500);"
javaScriptEnabled={true}
onMessage={this.handleMessage.bind(this)}
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
source={{
uri: 'https://website.com',
}}
/>
My message handler code:
handleMessage(event) {
if(event.nativeEvent.data && event.nativeEvent.data.indexOf("$#doctitle-")==0){
this.setState({
windowTitle: event.nativeEvent.data.split('-')[1]
});
}
}
answered Jan 4 at 12:10
MuthukrishnanMuthukrishnan
486411
486411
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51863829%2fhow-to-monitor-changes-in-webview-document-title-with-react-native%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown