What is the code to block a certain element in a page when using a WebView? I have a web site with ads that I've incorporated into a free app. I want to create a paid app that contains the same site, but blocks the ads from displaying. Thanks.
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
<?php
require_once 'path/to/a/php/script.php';
?>
<?php
if(isset($_GET['ads'])){
if($_GET['ads']!='noads'){
echo <<<ADSCODE
<script type="text/javascript">
ch_client = "cbanks";
ch_width = 320;
ch_height = 50;
ch_type = "mobile";
ch_sid = "LDS";
</script>
<script src="http://scripts.chitika.net/eminimalls/amm.js" type="text/javascript">
ADSCODE;
}
}
?>
<?php
require_once 'insert_ads.php';
?>
<?php
if(!isset($_GET['ads']) || $_GET['ads']!='no'){
// if the webpage has been loaded without an 'ads' key/value pair in the url or the key/value exists but the value is NOT 'no' then insert ad code
// see here for info on the heredoc syntax i've used: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
echo <<<ADSCODE
<script type="text/javascript">
ch_client = "cbanks";
ch_width = 320;
ch_height = 50;
ch_type = "mobile";
ch_sid = "LDS";
</script>
<script src="http://scripts.chitika.net/eminimalls/amm.js" type="text/javascript">
ADSCODE;
}
?>
var ch_client='cbanks',ch_width=320,ch_height=50,ch_type='mobile',ch_sid='LDS',script=document.createElement('script');
script.type='text/javascript';
script.src='http://scripts.chitika.net/eminimalls/amm.js';
document.getElementsByTagName('head')[0].appendChild(script);
var ch_client='cbanks',ch_width=320,ch_height=50,ch_type='mobile',ch_sid='LDS',script=document.createElement('script');script.type='text/javascript';script.src='http://scripts.chitika.net/eminimalls/amm.js';document.getElementsByTagName('head')[0].appendChild(script);
Sub Globals
Dim ShowAds As Boolean
End Sub
Sub Activity_Create(FirstTime As Boolean)
ShowAds=True
End Sub
Sub WebView1_PageFinished (Url As String)
If ShowAds Then
WebViewExtras1.executeJavascript(WebView1, "var ch_client='cbanks',ch_width=320,ch_height=50,ch_type='mobile',ch_sid='LDS',script=document.createElement('script');script.type='text/javascript';script.src='http://scripts.chitika.net/eminimalls/amm.js';document.body.appendChild(script);")
Log("Ads code added to webpage")
End If
End Sub
function getQueryObject(){
var query=location.search, args={};
query=query.length>0?query.substring(1):'';
var items=query.split('&'), item, name, value, i;
for(i=0; i<items.length; i++){
item=items[i].split('=');
name=decodeURIComponent(item[0]);
value=decodeURIComponent(item[1]);
args[name]=value;
}
return args;
}
// code to add the ads code based on the key/value ads=no
var query=getQueryObject();
if(!query.ads && query.ads!=='no'){
// insert the ads code
var ch_client='cbanks', ch_width=320, ch_height=50, ch_type='mobile', ch_sid='LDS', script=document.createElement('script');
script.type='text/javascript';
script.src='http://scripts.chitika.net/eminimalls/amm.js';
document.getElementsByTagName('head')[0].appendChild(script);
}
<html>
<head>
<script type="text/javascript" src="http://mm.chitika.net/minimall?&cid=default&screenres=313x483&winsize=480x724&canvas=297x2376&frm=false&history=1&impsrc=amm-1.10.0&url=file%3A///android_asset/my_webpage2.htm&cb=183&loc=8%2C8&snip_title=Toggle%20highlight%20HTML%20element%20on%20click&output=simplejs&callback=ch_ad_render_search"></script>
<script type="text/javascript" src="http://mm.chitika.net/minimall?&w=320&h=50&client=cbanks&sid=LDS&cid=LDS&type=mobile&previous_format=undefinedxundefined&screenres=313x483&winsize=480x724&canvas=313x483&frm=false&history=1&impsrc=amm-1.10.0&url=file%3A///android_asset/my_webpage2.htm&cb=794&loc=8%2C8&snip_title=Toggle%20highlight%20HTML%20element%20on%20click&output=simplejs&callback=ch_ad_render_search"></script>
</head>
<body>
<div id="chitikaAdBeacon-183"></div>
<iframe id="ch_ad183" name="ch_ad183" width="0" height="0" frameborder="0" src="about:blank" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no"></iframe>
<script type="text/javascript" src="http://scripts.chitika.net/eminimalls/amm.js"></script>
<div id="chitikaAdBeacon-794"></div>
<div></div>
<iframe id="ch_ad794" name="ch_ad794" width="0" height="0" frameborder="0" src="about:blank" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" style="display: none; "></iframe>
</body>
</html>
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim AdParam As String
Dim ModifiedWebPage, WebPageFilename As String
Dim UserPaid As Boolean
Dim WebView1 As WebView
Dim WebViewExtras1 As WebViewExtras
End Sub
Sub Activity_Create(FirstTime As Boolean)
ModifiedWebPage=""
UserPaid=True ' use this boolean to set whether or not ads should be displayed
WebPageFilename="my_webpage2.htm"
If UserPaid Then
AdParam="?ads=no"
Else
AdParam=""
End If
WebView1.Initialize("WebView1")
WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
WebViewExtras1.addWebChromeClient(WebView1)
Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
End Sub
Sub Activity_Resume
If File.Exists(File.DirInternalCache, WebPageFilename) Then
' GetText assumes the file is encoded using UTF8
' the cached webpage will contain no ad code and requires no 'ads' key/value
WebView1.LoadHtml(File.GetText(File.DirInternalCache, WebPageFilename))
File.Delete(File.DirInternalCache, WebPageFilename)
Log("Modified webpage loaded")
Else
WebView1.LoadUrl("file:///android_asset/"&WebPageFilename&AdParam)
Log("Original webpage loaded")
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If ModifiedWebPage="" Then
Log("The webpage contains no highlighted elements so there is no need to save it")
Else
Log("The webpage has been modified - cache it to the filesystem")
File.WriteString(File.DirInternalCache, WebPageFilename, ModifiedWebPage)
End If
End Sub
Sub SaveHtml(Html As String)
' this Sub is called only by the WebView1 click event listener
' Html will be either the entire modified webpage html or (if the webpage is NOT modified) an empty String
' Log(Html)
ModifiedWebPage=Html
End Sub
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Toggle highlight HTML element on click</title>
<style type="text/css">
.highlight{
background-color: black;
color: white;
}
</style>
<script type="text/javascript">
function createListener(){
// code to add the highlight on click event listener
document.addEventListener('click', function(event){
var element=event.target;
var tagName=element.tagName.toLowerCase();
// detect HTML tags that should NOT be highlighted
if(tagName!=='html' && tagName!=='body' && tagName!=='a'){
if(element.className===''){
element.className='highlight';
} else {
element.className='';
}
}
var modifiedElements=document.getElementsByClassName('highlight'), text;
if(modifiedElements.length>0){
// some elements have been highlighted so send the entire modofied page to a B4A Sub SaveHtml
text=document.documentElement.outerHTML;
} else {
// the page currently contains no highlighted elements send an empty string to the B4A Sub SaveHtml to indicate there is no need to save the page
text='';
}
B4A.CallSub('SaveHtml', true, text);
}, false);
}
</script>
</head>
<body onload="createListener()">
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed venenatis, leo et ornare tempor, est tellus suscipit mauris, et tincidunt lectus justo eu mauris. Vivamus sit amet est id lorem sodales semper. Integer odio nisi, aliquet in tincidunt vitae, auctor vel nisi. Phasellus lacinia velit eu ligula elementum commodo viverra sapien sodales. Duis tincidunt est arcu, ut facilisis arcu. Nullam pulvinar interdum aliquet. Suspendisse eleifend convallis lacinia. </p>
<p> Aenean sed mi justo, commodo imperdiet urna. Praesent adipiscing, nisl sit amet viverra tempor, arcu nisl imperdiet libero, ut vestibulum lorem nisl at erat. Aliquam tincidunt suscipit diam, sit amet blandit ligula ultricies eget. Suspendisse suscipit enim eget ante ultricies vel laoreet est commodo. Duis placerat sem id libero aliquam auctor. Vestibulum sed quam ipsum, ac volutpat ante. Cras fringilla elit in nunc lobortis bibendum varius dolor ultricies. Nullam est mi, bibendum vitae tempus vitae, aliquet non neque. Sed urna sapien, lobortis non molestie ac, gravida nec ligula. </p>
<script type="text/javascript">
function getQueryObject(){
var query=location.search, args={};
query=query.length>0?query.substring(1):'';
var items=query.split('&'), item, name, value, i;
for(i=0; i<items.length; i++){
item=items[i].split('=');
name=decodeURIComponent(item[0]);
value=decodeURIComponent(item[1]);
args[name]=value;
}
return args;
}
// code to add the ads code based on the key/value ads=no
var query=getQueryObject();
if(!query.ads || query.ads!=='no'){
// insert the ads code
query=null; // no longer need to keep a reference to this object
var ch_client='cbanks', ch_width=320, ch_height=50, ch_type='mobile', ch_sid='LDS';
document.write('<script type="text/javascript" src="http://scripts.chitika.net/eminimalls/amm.js"></sc'+'ript>');
console.log('ads code inserted');
} else {
console.log('ads code NOT inserted');
}
</script>
</body>
</html>
WebView1.LoadHtml(File.GetText(File.DirInternalCache, WebPageFilename))
WebView1.LoadUrl(File.DirInternalCache&WebPageFilename&AdParam)
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?