Android Question change/delete html code in Webwiew

Tayfur

Well-Known Member
Licensed User
Longtime User
Hello;
I want to change or delete some lines in web pages in WebWiew.
is it possible? how can i it.

For exp.

www.webname.com

and it has a some googleads lines under below. I want to delete this lines.

HTML:
<a id="aw0" target="_blank" href="http://www.googleadservices.com/pagead/aclk?sa=L&amp;ai=C-EDuzTMOVv6CHM2oiQ&amp;num=1&amp;cid=5Ghya2MW70puPP0riGn7Nu6t&amp;sig=AOD64_2S-_YkM2XNLs3MjZNS0ArdAdNWDA&amp;client=ca-pub-7384807473857921&amp;nm=2&amp;nx=89&amp;ny=300&amp;mb=2&amp;adurl=http://oggi.com.tr" data-original-click-url="http://www.googleadservices.com/pagead/aclk?sa=L&amp;ai=C-EDuzTMOVv6CHM2oigHAQ&amp;num=1&amp;cid=5Ghya2MW70puPP0riGn7Nu6t&amp;sig=AOD64_2S-_YkM2XNLs3MjZNS0ArdAdNWDA&amp;client=ca-pub-7384807473857921&amp;adurl=http://oggi.com.tr"><img src="https://tpc.googlesyndication.com/simgad/10631622196306904107" border="0" width="160" class="img_ad" onload=""></a>
 

warwound

Expert
Licensed User
Longtime User
how is delete with out js in webview

That's not possible.
The WebView can load an HTML document.
If you have control over the HTML document then you can obviously make changes to the HTML document.
Otherwise your only option is to load the document and then manipulate it using javascript.
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
That's not possible.
The WebView can load an HTML document.
If you have control over the HTML document then you can obviously make changes to the HTML document.
Otherwise your only option is to load the document and then manipulate it using javascript.

I serached in forum.
I found some codes

is it good idea for you?

https://www.b4x.com/android/forum/threads/how-to-block-page-element-in-webview.14139/#post-82734


B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim webView1 As WebView
   webView1.Initialize("webView1")
   Dim webViewExtras1 As WebViewExtras
   webViewExtras1.addJavascriptInterface(webView1,"b4a")
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.AddView(webView1,0,0,100%x,100%y)
   webView1.LoadUrl("http://alumnihighschool.net/scriptures/hymns/1.htm")
   webViewExtras1.addJavascriptInterface(webView1,"B4A")
End Sub

Sub Activity_Resume

End Sub

Sub webView1_OverrideUrl (Url As String) As Boolean
   ProgressDialogShow("Loading...")
End Sub

Sub webView1_PageFinished (Url As String)
   ProgressDialogShow("Loading...")
   webViewExtras1.executeJavascript(webView1,"B4A.CallSub('processHTML',true, document.documentElement.outerHTML)")
End Sub

Sub processHTML(html As String)
   If html.IndexOf("iframe")>-1 Then
      Dim a As Int
      Dim b As Int
      Do While html.IndexOf("iframe") <> -1
         a = html.IndexOf("<iframe")
         b = html.IndexOf2("</iframe>",a)
         html = html.Replace(html.SubString2(a,b+9),"")
      Loop
      If html.IndexOf("<br><br><br><br>") > -1 Then
         a = html.IndexOf("<br><br><br><br>")
         b = html.IndexOf2("</body>",a)
         html = html.Replace(html.SubString2(a,b),"")
      End If
      webView1.LoadHtml(html)
   End If
   ProgressDialogHide
End Sub
[/QUOTE]
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I serached in forum.
I found some codes

is it good idea for you?

https://www.b4x.com/android/forum/threads/how-to-block-page-element-in-webview.14139/#post-82734


B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim webView1 As WebView
   webView1.Initialize("webView1")
   Dim webViewExtras1 As WebViewExtras
   webViewExtras1.addJavascriptInterface(webView1,"b4a")
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.AddView(webView1,0,0,100%x,100%y)
   webView1.LoadUrl("http://alumnihighschool.net/scriptures/hymns/1.htm")
   webViewExtras1.addJavascriptInterface(webView1,"B4A")
End Sub

Sub Activity_Resume

End Sub

Sub webView1_OverrideUrl (Url As String) As Boolean
   ProgressDialogShow("Loading...")
End Sub

Sub webView1_PageFinished (Url As String)
   ProgressDialogShow("Loading...")
   webViewExtras1.executeJavascript(webView1,"B4A.CallSub('processHTML',true, document.documentElement.outerHTML)")
End Sub

Sub processHTML(html As String)
   If html.IndexOf("iframe")>-1 Then
      Dim a As Int
      Dim b As Int
      Do While html.IndexOf("iframe") <> -1
         a = html.IndexOf("<iframe")
         b = html.IndexOf2("</iframe>",a)
         html = html.Replace(html.SubString2(a,b+9),"")
      Loop
      If html.IndexOf("<br><br><br><br>") > -1 Then
         a = html.IndexOf("<br><br><br><br>")
         b = html.IndexOf2("</body>",a)
         html = html.Replace(html.SubString2(a,b),"")
      End If
      webView1.LoadHtml(html)
   End If
   ProgressDialogHide
End Sub

Not really!
It loads a webpage then passes the webpage HTML to b4a where b4a modifies and reloads it.
It'll probably work most of the time but is liable to break some webpages.

Imagine the user clicks a link on the modified webpage and the link's target is a relative URL (not an absolute URL).
The modified webpage has been loaded using the WebView's LoadHtml method - it has not been loaded from the URL that the relative URL relates to.
So that link to a relative URL will likely fail with a 404 page not found error.

Ideally you will wait for the webpage to load - wait for the WebView PageFinished event to be raised.
Then execute javascript that will directly manipulate the already loaded HTML using javascript DOM methods.

Google is your friend: https://www.google.co.uk/search?q=javascript+modify+dom&ie=UTF-8&oe=UTF-8&gws_rd=ssl
 
Upvote 0
Top