Android Question Best way to minimize HTML + Javascript

max123

Well-Known Member
Licensed User
Longtime User
Hi all,

sometimes we use a WebView to load a local (or non local) HTML file.

To decrease the page overhead and speed up the page loading is good practice to minimize the size,
so remove all informations really not needed for code interpretation, but useful on development
process about user interation, like unused white spaces, comments, end of lines, tabulators etc.
See https://en.wikipedia.org/wiki/Minification_(programming)

This can be different but similar to when we compile eg. Java to bytecode, C++ to a binary file etc....

On this direction I've searched on the forum if someone already wrote some code to just pass
the HTML string and return a minified working string decreased on size.

I've searched it but seem no one have already wrote this, or not posted to the forum, so I tried to write it myself.

I wrote this, it works and return the full HTML as just one full long line decreasing by 15-20% factor the size.

It is a bit spartan and probably only works in my use case but not with all HTML + CSS + JS strings.

My question is.... There are better ways to do this ?

Thanks
B4X:
Private Sub MinifyHTMLFile(Original As StringBuilder) As ResumableSub
 
    Dim isStyle As Boolean = False
    Dim isScript As Boolean = False
    Dim isMultilineComment As Boolean = False
 
    Dim Minified, FinalMinified, Styles, Scripts As StringBuilder
 
    Minified.Initialize
    FinalMinified.Initialize
    Styles.Initialize
    Scripts.Initialize
 
    Dim lines() As String = Regex.Split(CRLF, Original.ToString)
    Log("NUMBER OF LINES: " & lines.Length)
 
    For i = 0 To lines.Length-1
        Dim line As String = lines(i).Trim
 
        If line.Length = 0 Then
            Log("Found void string on line " & (i+1))
        Else
            If line.StartsWith("<style>") Then
                isStyle = True
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [START STYLE]")
            Else If line.StartsWith("</style>") Then
                isStyle = False
                Minified.Append(Styles.ToString).Append(CRLF)
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [END STYLE]")
                Log("FULL STYLE: [" & Styles.ToString & "]")
            Else If line.StartsWith("<script>") Then
                isScript = True
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [START SCRIPT]")
            Else If line.StartsWith("</script>") Then
                isScript = False
                Minified.Append(Scripts.ToString).Append(CRLF)
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [END SCRIPT]")
                Log("FULL Script: [" & Scripts.ToString & "]")
            Else If line.StartsWith("<") And line.EndsWith(">") Then
                Minified.Append(line).Append(CRLF)
                Log("LINE [ " & (i+1) & "]: " & line & " [HTML TAG]")
            Else
                If line.StartsWith("/*") Then isMultilineComment = True  'MULTILINE COMMENT
 
                If isStyle Then              
                    If line.StartsWith("//") = True Then
                        Log("Found Full Line [STYLE] comment on line " & (i+1) & " -> [" & line & "]")
                    Else
                        If line.Contains("//") = False Then
                            If isMultilineComment = False Then
                                Styles.Append(line)
                                Log("LINE [ " & (i+1) & "]: " & line & " [STYLE]")
                            Else
                                Log("LINE [ " & (i+1) & "]: " & line & " [STYLE MULTILINE COMMENT]")
                            End If
                        Else
                            Dim n As Int = line.IndexOf("//")
                            Dim comment As String = line.SubString(n).Trim
                            Dim tmp As String = line.SubString2(0, n).Trim
                            Styles.Append(tmp)
                            Log("Found Partial Line [STYLE] comment on line " & (i+1) & " -> [" & comment & "]    LINE WITHOUT COMMENTS -> [" & tmp & "]")
                        End If
                    End If
                Else If isScript Then
                    If line.StartsWith("//") = True Then
                        Log("Found Full Line [SCRIPT] comment on line " & (i+1) & " -> [" & line & "]")
                    Else
                        If line.Contains("//") = False Or line.Contains("\/") = True Then  ' We add Escape
                            If isMultilineComment = False Then
                                Scripts.Append(line)
                                Log("LINE [ " & (i+1) & "]: " & line & " [SCRIPT]")
                            Else
                                Log("LINE [ " & (i+1) & "]: " & line & " [SCRIPT MULTILINE COMMENT]")
                            End If
                        Else
                            Dim n As Int = line.IndexOf("//")
                            Dim comment As String = line.SubString(n).Trim
                            Dim tmp As String = line.SubString2(0, n).Trim
                            Scripts.Append(tmp)
                            Log("Found Partial Line [SCRIPT] comment on line " & (i+1) & " -> [" & comment & "]    LINE WITHOUT COMMENTS -> [" & tmp & "]")
                        End If
                    End If
                End If
 
                If line.EndsWith("*/") Then isMultilineComment = False
            End If

'            Sleep(0)
        End If
    Next
 
    LogColor("MINIFIED MULTILINE:", xui.Color_Green)
 
    '''''' Log it line by line
    Dim lines() As String = Regex.Split(CRLF, Minified.ToString)
    For Each line As String In lines
        LogColor(line, xui.Color_Yellow)
    Next
 
    FinalMinified.append(Minified.ToString.Replace(CRLF, ""))  ' Make it one line
 
    LogColor("NO MINIFIED Size: " & Original.Length & " Bytes", xui.Color_Red)
    LogColor("MINIFIED Size: " & FinalMinified.Length & " Bytes", xui.Color_Red)
 
    LogColor("Size decreased by a factor of " & NumberFormat(100 - MapFloat(FinalMinified.Length, 0, Original.Length, 0, 100), 0, 2) & " percent", xui.Color_Green)

    Return FinalMinified
End Sub

'Re-maps a Float number from one range to another.
Sub MapFloat(Value As Float, fromLow As Float, fromHigh As Float, toLow As Float, toHigh As Float) As Float
    Return ( (Value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow )
End Sub

I use it that way:
B4X:
    CreateHTMLString
    Wait For (MinifyHTMLFile(mFullHTML)) Complete (Minified As StringBuilder)
    SaveHTMLFile(Minified.ToString)
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
Here is ChatGPTs try at optimizing your routine utilizing Regex heavily...

B4X:
Private Sub MinifyHTML(html As String) As String
    ' Remove HTML comments
    Dim noHTMLComments As String = Regex.Replace("<!--.*?-->", "", html)
   
    ' Handle script and style blocks separately to remove both single-line and multiline comments
    Dim noScriptStyleComments As String = Regex.Replace("<script[^>]*>[\s\S]*?</script>|<style[^>]*>[\s\S]*?</style>", _
        Sub(match As Object) As String
            Dim content As String = match.ToString
            ' Remove multiline comments from scripts and styles
            content = Regex.Replace(content, "/\*[\s\S]*?\*/", "")
            ' Remove single-line comments from scripts and styles
            Return Regex.Replace(content, "//.*", "")
        End Sub, noHTMLComments)
   
    ' Remove multiple spaces and redundant newlines
    Dim noExtraSpaces As String = Regex.Replace(noScriptStyleComments, "\s{2,}", " ")

    ' Remove spaces around tags to reduce size
    Dim noSpacesAroundTags As String = Regex.Replace(noExtraSpaces, "\s*(<[^>]+>)\s*", "$1")
   
    ' Remove line breaks to make it one long line, further minimizing size
    Return Regex.Replace(noSpacesAroundTags, "\s+", " ")
End Sub
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Many Thanks @JohnC I will try it. ?
I even ask to Gemini. Eventually I post here.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I just noticed the "Sub(match As Object) As String" line in the code, and asked ChatGPT about it and here is its response:

In the context of the B4X programming environment, using Sub(match As Object) As String directly within a call to Regex.Replace as I demonstrated isn't valid. B4X, which encompasses languages like B4A (Basic4android), B4i, and B4J, doesn't support anonymous functions or lambdas in the same way that languages like JavaScript or C# do. This was an oversight in my previous response, and I apologize for the confusion.

In B4X, you need to define your functions explicitly and cannot pass them directly as arguments. To correctly adapt the method to remove comments and unnecessary spaces in HTML content, you would need to adjust the approach to comply with B4X capabilities, which include handling regular expressions and string operations in a more procedural manner.

Here's how you can modify the HTML minification subroutine without trying to use an anonymous function, sticking to B4X's capabilities:


B4X:
Sub MinifyHTML(html As String) As String
    ' Remove HTML comments
    Dim noHTMLComments As String = Regex.Replace("<!--.*?-->", "", html)
  
    ' Minify <script> blocks
    Dim scriptMatches As List
    scriptMatches = Regex.Split("<script[^>]*>[\s\S]*?</script>", noHTMLComments)
    Dim minifiedScripts As String = ""
    For Each match As String In scriptMatches
        minifiedScripts = minifiedScripts & MinifyScript(match)
    Next
  
    ' Minify <style> blocks
    Dim styleMatches As List
    styleMatches = Regex.Split("<style[^>]*>[\s\S]*?</style>", minifiedScripts)
    Dim minifiedStyles As String = ""
    For Each match As String In styleMatches
        minifiedStyles = minifiedStyles & MinifyStyle(match)
    Next
  
    ' Further remove spaces and line breaks
    Dim noExtraSpaces As String = Regex.Replace(minifiedStyles, "\s{2,}", " ")
    Dim noSpacesAroundTags As String = Regex.Replace(noExtraSpaces, "\s*(<[^>]+>)\s*", "$1")
  
    ' Remove line breaks and make it one long line
    Return Regex.Replace(noSpacesAroundTags, "\s+", " ")
End Sub

Sub MinifyScript(scriptContent As String) As String
    ' Remove multiline comments
    scriptContent = Regex.Replace(scriptContent, "/\*[\s\S]*?\*/", "")
    ' Remove single-line comments
    Return Regex.Replace(scriptContent, "//.*", "")
End Sub

Sub MinifyStyle(styleContent As String) As String
    ' Similar to MinifyScript, remove comments
    styleContent = Regex.Replace(styleContent, "/\*[\s\S]*?\*/", "")
    Return Regex.Replace(styleContent, "//.*", "")
End Sub

Key Adjustments:

  • Explicit Function Definitions: MinifyScript and MinifyStyle are defined as separate subs to handle the specifics of minifying JavaScript and CSS respectively. This approach aligns with B4X's design and syntax requirements.
  • Regex Usage: The Regex.Split is used to break down the HTML into script and style blocks. However, this example might need further refinement to correctly capture and replace content because Regex.Split might not return the content as intended for reassembly. You might need to adjust this part to correctly parse and reinsert the minified scripts and styles into the HTML content.
This code is more conceptual and would likely require adjustments for precise functionality, especially in ensuring that script and style blocks are correctly identified, minified, and reinserted into the overall HTML structure.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
@JohnC this syntax is not recognized by the IDE. Because a Sub inside another Sub ?
B4X:
  ' Handle script and style blocks separately to remove both single-line and multiline comments
    Dim noScriptStyleComments As String = Regex.Replace("<script[^>]*>[\s\S]*?</script>|<style[^>]*>[\s\S]*?</style>", _
        Sub(match As Object) As String
            Dim content As String = match.ToString
            ' Remove multiline comments from scripts and styles
            content = Regex.Replace(content, "/\*[\s\S]*?\*/", "")
            ' Remove single-line comments from scripts and styles
            Return Regex.Replace(content, "//.*", "")
        End Sub, noHTMLComments)
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Oh sorry, I posted then I read your last message
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
I tried it just now, but something is wrong, it just returns one line from the HTML string.

The strange thig is that it return just \S+ on this single line.

I just replaced the Sub with my actual that is working in my case just for this html.
If you want I can reproduce it in a new project and put the same html string and both subs, mine and this one.
I attached a screenshot, here my last code:
B4X:
Sub MinifyHTML(html As String) As String
    ' Remove HTML comments
    Dim noHTMLComments As String = Regex.Replace("<!--.*?-->", "", html)
 
    ' Minify <script> blocks
    Dim scriptMatches As List
    scriptMatches = Regex.Split("<script[^>]*>[\s\S]*?</script>", noHTMLComments)
    Dim minifiedScripts As String = ""
    For Each match As String In scriptMatches
        minifiedScripts = minifiedScripts & MinifyScript(match)
    Next
 
    ' Minify <style> blocks
    Dim styleMatches As List
    styleMatches = Regex.Split("<style[^>]*>[\s\S]*?</style>", minifiedScripts)
    Dim minifiedStyles As String = ""
    For Each match As String In styleMatches
        minifiedStyles = minifiedStyles & MinifyStyle(match)
    Next
 
    ' Further remove spaces and line breaks
    Dim noExtraSpaces As String = Regex.Replace(minifiedStyles, "\s{2,}", " ")
    Dim noSpacesAroundTags As String = Regex.Replace(noExtraSpaces, "\s*(<[^>]+>)\s*", "$1")
   
    ' Remove line breaks and make it one long line
''''''''    Return Regex.Replace(noSpacesAroundTags, "\s+", " ")
 
    Dim finalhtml As String = Regex.Replace(noSpacesAroundTags, "\s+", " ")
 
    LogColor("NO MINIFIED Size: " & html.Length & " Bytes", xui.Color_Red)
    LogColor("MINIFIED Size: " & finalhtml.Length & " Bytes", xui.Color_Red)
 
    LogColor("Size decreased by a factor of " & NumberFormat(100 - MapFloat(finalhtml.Length, 0, html.Length, 0, 100), 0, 2) & " percent", xui.Color_Green)
 
    LogColor("FINAL MINIFIED ONE LINE: ", xui.Color_Green)
    '    myLog.PrintColor(Minified.ToString, xui.Color_Magenta)
    LogColor(finalhtml, xui.Color_Yellow)
 
    Return finalhtml
End Sub

Sub MinifyScript(scriptContent As String) As String
    ' Remove multiline comments
    scriptContent = Regex.Replace(scriptContent, "/\*[\s\S]*?\*/", "")
    ' Remove single-line comments
    Return Regex.Replace(scriptContent, "//.*", "")
End Sub

Sub MinifyStyle(styleContent As String) As String
    ' Similar to MinifyScript, remove comments
    styleContent = Regex.Replace(styleContent, "/\*[\s\S]*?\*/", "")
    Return Regex.Replace(styleContent, "//.*", "")
End Sub

'Re-maps a Float number from one range to another. 
Sub MapFloat(Value As Float, fromLow As Float, fromHigh As Float, toLow As Float, toHigh As Float) As Float
    Return ( (Value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow )
End Sub
 

Attachments

  • Screenshot 2024-04-21 170714.png
    Screenshot 2024-04-21 170714.png
    201.7 KB · Views: 86
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, here is ChatGPT's response - if this doesn't work, then ChatGPT gives up :)

It seems there was an issue with the implementation I suggested, particularly with how the regular expressions were used and potentially the handling of the Regex.Split function in B4X. This function, as it's commonly used in other programming languages, would split the string but also remove the matching parts, which isn't what we want here if we're trying to minify and reassemble the entire HTML content.

Let's refine the approach, focusing on directly substituting within the HTML content while preserving the blocks that need special handling (like <script> and <style>). Here, I'll adjust the code to ensure it correctly targets and replaces content without splitting and losing parts of the HTML. This adjustment also assumes that we don't want to lose the <script> and <style> tags themselves during the process:


B4X:
Sub MinifyHTML(html As String) As String
    ' Remove HTML comments
    Dim noHTMLComments As String = Regex.Replace(html, "<!--.*?-->", "")
    
    ' Minify <script> blocks
    noHTMLComments = Regex.Replace(noHTMLComments, "<script[^>]*>([\s\S]*?)</script>", "Sub(Match As Matcher) As String Return \"<script>\" & MinifyScript(Match.Group(1)) & \"</script>\" End Sub", True)
    
    ' Minify <style> blocks
    noHTMLComments = Regex.Replace(noHTMLComments, "<style[^>]*>([\s\S]*?)</style>", "Sub(Match As Matcher) As String Return \"<style>\" & MinifyStyle(Match.Group(1)) & \"</style>\" End Sub", True)
    
    ' Remove redundant spaces and line breaks globally
    Dim noExtraSpaces As String = Regex.Replace(noHTMLComments, "\s{2,}", " ")
    Dim noSpacesAroundTags As String = Regex.Replace(noExtraSpaces, "\s*(<[^>]+>)\s*", "$1")
    
    ' Remove line breaks and make it one long line
    Return Regex.Replace(noSpacesAroundTags, "\r\n|\n", "")
End Sub

Sub MinifyScript(scriptContent As String) As String
    ' Remove multiline comments
    scriptContent = Regex.Replace(scriptContent, "/\*[\s\S]*?\*/", "")
    ' Remove single-line comments
    scriptContent = Regex.Replace(scriptContent, "//.*", "")
    ' Remove excess whitespaces
    Return Regex.Replace(scriptContent, "\s+", " ")
End Sub

Sub MinifyStyle(styleContent As String) As String
    ' Remove comments and excess whitespaces, similar to scripts
    styleContent = Regex.Replace(styleContent, "/\*[\s\S]*?\*/", "")
    styleContent = Regex.Replace(styleContent, "//.*", "")
    Return Regex.Replace(styleContent, "\s+", " ")
End Sub

Key Changes:

  1. Regex Callbacks: I attempted to use a callback style which might not be correct for B4X. Instead, you should manually handle these replacements with a different method if B4X doesn't support callbacks in Regex.Replace. You might need to loop through matches manually and replace them.
  2. Preserving <script> and <style> Tags: This version attempts to keep the script and style tags in place and only replaces the content inside them. If the callback mechanism is not valid, you will need to do a find-and-replace in a more manual manner.
  3. Handling Newlines and Spaces: The final line breaks are removed, and excessive spaces are compacted to a single space where appropriate.

Testing and Debugging:

You should test this code and debug if there are still issues. If B4X does not support inline substitution functions (like lambdas or anonymous subs), you'll need to extract matches manually, process them, and then reinsert them into the original HTML, which can be quite complex and might necessitate maintaining indices or using placeholders. Always verify the final HTML output to ensure no functionalities are broken after minification.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Thanks for replies, I apprecciate your help, but it's really surprising that a brilliant mind like you would ask ChatGPT how to do these things.
Furthermore, the answers come close to the right ones but are always wrong, the same thing as with Gemini.

For this reason I wanted to ask Gemini but then I decided to post on the B4X forum where there are real people with brains who respond and not the AI.

But I keep stumbling upon AI, now it seems like the only thing that exists..... o_O

In my opinion, relying only on AI is a big mistake that the human race is making, it's the same thing as when mathematical operations were first done in mind, then written down, then using calculators. Now we are no longer able to do the math in our heads if we don't have a calculator........

Ultimately I'm trying the first code provided by ChatGPT and everything is wrong, this from the first line,
even the order of the arguments inside the Regexes are swapped positions.....

B4X:
Sub MinifyScript(scriptContent As String) As String
    Log("SCRIPT CONTENT: " & scriptContent)
 
    ' Remove multiline comments
'    scriptContent = Regex.Replace(scriptContent, "/\*[\s\S]*?\*/", "") ' original
    scriptContent = Regex.Replace("/\*[\s\S]*?\*/", scriptContent, "")
    Log("SCRIPT CONTENT WITHOUT MULTILINE COMMENTS: " & scriptContent)
 
    ' Remove single-line comments
'    scriptContent = Regex.Replace(scriptContent, "//.*", "") ' original
    scriptContent = Regex.Replace( "//.*", scriptContent, "")
    Log("SCRIPT CONTENT WITHOUT SINGLELINE COMMENTS: " & scriptContent)
 
    Return scriptContent
End Sub

Sub MinifyStyle(styleContent As String) As String
    Log("STYLE CONTENT: " & styleContent)
 
    ' Similar to MinifyScript, remove comments
'    styleContent = Regex.Replace(styleContent, "/\*[\s\S]*?\*/", "") ' original
    styleContent = Regex.Replace("/\*[\s\S]*?\*/", styleContent, "")
    Log("STYLE CONTENT WITHOUT MULTILINE COMMENTS: " & styleContent)
 
'    styleContent = Regex.Replace(styleContent, "//.*", "") ' original
    styleContent = Regex.Replace("//.*", styleContent, "")
    Log("STYLE CONTENT WITHOUT SINGLELINE COMMENTS: " & styleContent)
 
    Return styleContent
End Sub

even this is wrong:
B4X:
scriptMatches = Regex.Split("<script[^>]*>[\s\S]*?</script>", noHTMLComments)
and this....
B4X:
styleMatches = Regex.Split("<style[^>]*>[\s\S]*?</style>", minifiedScripts)

All patterns seem wrong.... Script are not recognized, syles not recognized, the code do not works.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I've had very good results in ChatGPT helping me with coding for many months, so I am surprised it was off-mark for this task.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Yes, I even use Gemini to help me write code for B4X, Javascript, C++ for microcontrollers.....
It helped, but always with not working code, without my brain the code will not works.
So now I will ask you.... I see you want help me.... Because you are more expert than me, may if we apply we obtain right results.

IMHO this should be a really cool and useful code for any user want to decrease HTML and/or JS files size, to have less overhead when load pages in the browser, to send over the network less bytes.... with same results but less overhead.... and finally it is multiplatform, can be used on B4A, B4J, B4I, may B4R.

There are online tools but doing this by code permits to do it dinamically on code generated HTML that is really useful.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I personally don't have a need to minimize HTML code, I just wanted to see if ChatGPT could optimize your code, but it appears it could not easily without more couching.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
So there are some other solution here ?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I had ChatGPT do a web search for any libraries/code (that is not an online service) to minimize code and it replied with these.

These obviously would need work to use in a B4A app, but it's better than trying to reinvent the wheel. I'm sure it would be helpful to have a lib to use in B4x apps, but I don't have time to work on one at this time.

When looking to minimize HTML and JavaScript code directly within your applications, there are several libraries you can use that fit your criteria of not being online services. Here are some popular options for JavaScript and Java:

JavaScript Libraries

  1. UglifyJS
    • About: UglifyJS is a popular JavaScript parser, minifier, compressor, or beautifier toolkit.
    • Usage: It can be used to compress and minify JavaScript files in your projects.
    • Installation: Available via npm. You can install it using npm install uglify-js.
  2. Terser
    • About: Terser is a fork of UglifyJS. It generally provides better support for modern JavaScript (ES6+).
    • Usage: Terser is useful for minifying ES6+ code which UglifyJS doesn't support.
    • Installation: Install via npm using npm install terser.
  3. clean-css
    • About: For CSS minification, clean-css is a powerful and fast library.
    • Usage: It's capable of various levels of compression, optimizations, and tweaks.
    • Installation: Install it through npm with npm install clean-css.

Java Libraries

  1. Google Closure Compiler
    • About: While it's most often used for JavaScript, Google Closure Compiler can be used in Java environments (via its Java-based tooling).
    • Usage: It compiles JavaScript into smaller, faster, and more robust code.
    • Installation: Available as a jar file or you can integrate it via Maven or Gradle.
  2. YUI Compressor
    • About: This is a Java tool that can minify both JavaScript and CSS files.
    • Usage: It's effective for projects where you need to integrate minification directly into your Java applications.
    • Installation: It can be integrated as a jar in your build process or used via command line.
  3. htmlcompressor
    • About: For HTML, this tool offers compression capabilities and can also minify embedded JavaScript and CSS.
    • Usage: It is designed to handle well with various web technologies and reduce the size of HTML files.
    • Installation: Available to be included in Java projects directly.
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
I wish I could have done it in B4X without having to wrap some existing java libraries.
I could try to do this but my code already works for my application.
I would have liked to look for someone to help me write this useful code that can be useful to many users on many occasions,
but unfortunately it seems that everyone is busy, even I am.

If it had been a community work, I would have done my part, but I see that this is not possible so I will leave and just use my code for my use case.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I want to try something....

Please post the HTML file that you have been using in your tests as an HTML file (please do not post the html contents as text of the post because it will be too long and make the post too long) .
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Hi @JohnC the problem here is that I save it on DirInternal and Android do not allow me to use DirRootExternal to pick the file.
I even cannot copy/paste it from B4A log because is too long and truncated to 4000 characters (the string is long around 11300 characters and is dinamically created, so I cannot copy/paste from IDE.

To do this I have to implement External Storage an require permission I think, and this is a bit difficult... do you have others ideas how I can get the html file from DirInternal ?

I have a question here about log limit.... and I want to start another thread....
I just created last week a small library to avoid log truncation on B4A (I see B4J is not affected to this and can log longer strings).
My library use StringBuilder and Regex to split very long strings and log line by line.....
It worked, but after this I compared my library with Log command.
Here B4A default Log show a warning of 4000 characters long truncation, but strangely the long string seem all printed out to the log.

Please, do you know if something has changed in Android or B4A ? Because now it can log string long 20000 characters and more ?

I can copy/paste html string from here, but I'm not sure if some parts are truncated... or may I do it by using my library.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Hi @JohnC the problem here is that I save it on DirInternal and Android do not allow me to use DirRootExternal to pick the file.
I even cannot copy/paste it from B4A log because is too long and truncated to 4000 characters (the string is long around 11300 characters and is dinamically created, so I cannot copy/paste from IDE.

To do this I have to implement External Storage an require permission I think, and this is a bit difficult... do you have others ideas how I can get the html file from DirInternal ?
Can't you simply use a desktop browser and navigate to the public webpage and then do a view source and save it into a txt file and then post the txt file?
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Can't you simply use a desktop browser and navigate to the public webpage and then do a view source and save it into a txt file and then post the txt file?
I have to do it directly on Android, and I only load it in a WebView, is there a way to launch Chrome with html file by code ?

Eventually I will try now to copy/paste from B4A log.
 
Upvote 0
Top