Android Question Question and help with a javascript in webview

endbyte

Member
what is it or how do I correct this javascript please, I'll explain: in a webview what I need to do is replace part of a text within a paragraph among several, I need to do it in the DOM on the fly and respecting the attributes of the paragraph that only change the internal part of the text that I want, the paragraph is identified with its respective Id, I have tried many scripts in many ways and I cannot get the result that I need, the script that I currently publish replaces the entire page and not only the text of the paragraph that I'm telling you, thank you very much in advance
first execute this script to go to the location of the paragraph on which I want to make the change, this script works perfectly for me:
WebViewExtras1.ExecuteJavascript("var e = document.getElementById("&Starter.ll&");e.scrollIntoView({behavior:'smooth', block:'start'});")
this is the script that doesn't work for me or it works but incorrectly:
WebViewExtras1.ExecuteJavascript("document.getElementById("&Starter.ll&").innerText.replace('"&Starter.mtext&"','"&linmod&"');")
 

drgottjr

Expert
Licensed User
Longtime User
WebViewExtras1.ExecuteJavascript("document.getElementById("&Starter.ll&").innerText.replace('"&Starter.mtext&"','"&linmod&"');")

you're replacing, but you're not assigning the replacement to anything. you need:
JavaScript:
var e = WebViewExtras1.ExecuteJavascript("document.getElementById("&Starter.ll&");
e.innerText = e.innerText.replace('"&Starter.mtext&"','"&linmod&"');"

look at the documentation for replace().
 

Attachments

  • endbyte.png
    endbyte.png
    41.9 KB · Views: 86
Upvote 0

endbyte

Member
you're replacing, but you're not assigning the replacement to anything. you need:
JavaScript:
var e = WebViewExtras1.ExecuteJavascript("document.getElementById("&Starter.ll&");
e.innerText = e.innerText.replace('"&Starter.mtext&"','"&linmod&"');"

look at the documentation for replace().
thanks for your answer, I didn't understand very well... ...
do you mean this??:
WebViewExtras1.ExecuteJavascript("var e=document.getElementById("&Starter.ll&");e.innertText=e.innerText.replace('"&Starter.mtext&"','"&linmod&"');")
I already tried this before and now again and it does exactly the same thing, apparently the complete DOM replaces me, here I show you in a general way the structure
general structure of the DOM within which I want to replace that part of text for example;:
<html>
<body>
<hr color=#070707>
<!-- before and after the paragraph there are more elements -->
<div id='d1'>
<p id='1' aligne='justify' style='color:#9B9B9B'>
TEXT O THE PARAGRAPH WHERE I NEED TO DO THE REPLACE EXACTLY FOR EXAMPLE THE WORD REPLACEME
</p>
</div>
<!-- before and after the paragraph there are more elements -->
</body>
</html>
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
yes, that is what i meant, EXCEPT your version has an error: innertText.

e.innertText=e.innerText.replace('"&Starter.mtext&"','"&linmod&"');") it's "innerText", not "innertText"

i am looking at your example html. innerText may not be the appropriate property. i have to look a little.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
my code works fine. i took your example, made a web page, loaded it and replaced "REPLACEDME" with "endbyte" using the code i suggested to you
(see image #3)
 

Attachments

  • 1.png
    1.png
    8.6 KB · Views: 110
  • 2.png
    2.png
    8.5 KB · Views: 97
  • 3.png
    3.png
    22.6 KB · Views: 123
Upvote 0

endbyte

Member
Sí, eso es lo que quise decir, EXCEPTO que su versión tiene un error: innertText.

e.innertText=e.innerText.replace('"&Starter.mtext&"','"&linmod&"');") es "innerText", no "innertText"

Estoy mirando tu ejemplo HTML. innerText puede no ser la propiedad adecuada. Tengo que mir

my code works fine. i took your example, made a web page, loaded it and replaced "REPLACEDME" with "endbyte" using the code i suggested to you
(see image #3)
I'm testing and reviewing in detail because although everything here looks good and it doesn't work for me either the way you are doing it or in other ways, the strange thing is that this is the only script with which I have had problems in all my experiences, Beware, a detail that I half commented on my question is that I need to do it on the fly, that is, without reloading the page, I am checking because even with your example, reloading the page does not work for me, I must have misplaced something here, REVIEWING...
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
either you will solve the problem by yourself, or you'll post the project and let others try.
based on your explanation, it is possible to do a search/replace with javascript injection.
if there are other things that we do not know about, it will be difficult to help you.
 
Upvote 0

endbyte

Member
O resolverás el problema por ti mismo, o publicarás el proyecto y dejarás que otros lo intenten.
Según su explicación, es posible hacer una búsqueda / reemplazo con inyección de JavaScript.
Si hay otras cosas que no conocemos, será difícil ayudarte.
either you will solve the problem by yourself, or you'll post the project and let others try.
based on your explanation, it is possible to do a search/replace with javascript injection.
if there are other things that we do not know about, it will be difficult to help you.
I have not solved it, I have an html that I load to a webview and I simply need to change a text of a certain paragraph and that the change is reflected immediately without having to reload the page, that simple but it seems that in webview it is not so easy or something is missing in the script to reflect the change to the DOM, thanks for your collaboration, if I manage to find the solution myself, of course I will publish it to help others who may need the same or something similar
 
Upvote 0

endbyte

Member
SOLVED : there was no way to make the change only on the specific text I wanted so I had to make the change externally on the entire paragraph and then replace it using innerHTML, no way that's weird webview

script ok:
WebViewExtras1.ExecuteJavascript("var p=document.getElementById("&Starter.ll&").innerHTML='"&linep&"';")
 
Last edited:
Upvote 0
Top