Android Question [ Solved ] Get a variable from a webview page

Mark Read

Well-Known Member
Licensed User
Longtime User
I am trying to get the style value of a <div id> tag in this page: http://routino.grade.de/

On loading the page, the results tab shows "Status: Router not run" (as detailed on line 323 of the html source). The variable I need is <div id="result_status_complete" .... where the style would be display: inline, instead of display: none.

I have tried this code

B4X:
'Dim Javascript As String="B4A.CallSub('GetVarFromWebview', true, document.getElementById('result_status_complete').getAttribute('style'))"
Dim Javascript As String="B4A.CallSub('GetVarFromWebview', true, document.getElementById('div id='result''))"
   
WebViewExtras1.executeJavascript(WebView1, Javascript)

Sub GetVarFromWebview(WebVar As String)
   'Called when the javascript is finished         
  Log("Web variable has the value => " & WebVar)
   Activity.Finish
End Sub

but the value comes back as empty???? Could this be correct or have I made a mistake. Javascript is not my strength.
 

sorex

Expert
Licensed User
Longtime User
can you read out the full source? then I would go for a simple regex lookup.

by the way you probably have issues with the singe quotes there and getElementByID will only look for element IDs not for what you entered there.

it should be document.getElementById('result_status_complete') and you need addiional checks.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
div id='result''
i believe there is no object with the id div id='result''
You should try
B4X:
document.getElementById('result')
or something similar
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
can you read out the full source?

Yes, the page is a mixture of html and javascript. The code in question is here:

B4X:
 <div class="tab_content" id="tab_results_div" style="display: none;">

    <div class="hideshow_box">
      <span class="hideshow_title">Status</span>
      <div id="result_status">
        <div id="result_status_not_run">
          <b><i>Router not run</i></b>
        </div>
        <div id="result_status_running"  style="display: none;">
          <b>Router running...</b>
        </div>
        <div id="result_status_complete" style="display: none;">
          <b>Routing completed</b>
          <br>
          <a id="router_log_complete" target="router_log" href="#">View Details</a>
        </div>
        <div id="result_status_error"    style="display: none;">
          <b>Router error</b>
          <br>
          <a id="router_log_error" target="router_log" href="#">View Details</a>
        </div>
        <div id="result_status_failed"   style="display: none;">
          <b>Router failed to run</b>
        </div>
      </div>
    </div>

The style of the tag is initially "display: none" ie. invisible and changes accordingly. The tag "div id="result_status_complete"" is the one I want and only the style attribute thereof.

i believe there is no object with the id div id='result''
Thanks DonManfred, I know but I was trying to get an array with all the tags. I have tried so many ways. It must be something simple.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Sometimes sleeping on a problem helps! My idea to get all the results was not a good one as there as many in the page. It is easier to get the few as required. This code does the job.

B4X:
Sub GetVariable
    'Get the variable required
    Dim Javascript As String="B4A.CallSub('GetVarFromWebview', true, document.getElementById('result_status_complete').getAttribute('style'))"
    WebViewExtras1.executeJavascript(WebView1, Javascript)
   
End Sub

Sub GetVarFromWebview(WebVar As String)
    'Called when the javascript is finished               
    Log("Web variable has the value => " & WebVar)
    Activity.Finish
End Sub

The DIV ID tag "result_status_complete" before and after running has the value "display: none" and "". This is all that I need, combined with the tag
"result_status_error" for error checking.

Many thanks.
 
Upvote 0
Top