Hmmm looks like the post you made yesterday at 10:40PM has been deleted...
I'll post this anyway.
'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 Domain, ModifiedWebPage, WebPageFilename As String
Dim WebView1 As WebView
Dim WebViewExtras1 As WebViewExtras
End Sub
Sub Activity_Create(FirstTime As Boolean)
Domain="http://code.martinpearman.co.uk/deleteme/cbanks/"
ModifiedWebPage=""
WebPageFilename="highlight_on_click.php"
WebView1.Initialize("WebView1")
WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
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("Cached webpage loaded")
Else
WebView1.LoadUrl(Domain&WebPageFilename&"?ads=no")
Log("webpage loaded from internet")
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
ModifiedWebPage=Html
End Sub
I've updated the webpage's click event listener:
function createListener(){
document.addEventListener('click', function(event){
var element=event.target;
var tagName=element.tagName.toLowerCase();
if(tagName!=='html' && tagName!=='body'){
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);
}
Each time the click event listener modifies the page it sends a String to the B4A Sub
SaveHtml.
That String is empty if the page is currently NOT modified otherwise the String is the entire modified page.
The String is stored in a global variable named
ModifiedWebPage.
When your activity is paused the String is written to the cache - if the String is not empty.
And when your activity is resumed the code checks for a cached page and loads that cached page if it exists, otherwise it loads the original page from the internet.
If you try it on an emulator you may notice it doesn't always work properly - that is a known bug with the emulator on orientation change:
Android emulator bug while changing orientation | Vikas Patel's Blog.
On a real device it works perfectly.
Now i'm wondering if you want to hardcode that javascript into all of your pages?
You could use a method similar to your other thread - put the javascript into a PHP include file and include that file only if the page is being viewed within your B4A application.
If you add that javascript to a page that is not being viewed within your B4A application - a page viewed with a desktop browser - then it will generate an error each time the user clicks the page.
The page will be trying to execute
B4A.CallSub('SaveHtml', true, text); and that function is only part of your B4A WebView as it's been added with the javascript interface.
Martin.