Android Question How to get plain text in Informatix's ePubReader + WebReader?

Syd Wright

Well-Known Member
Licensed User
Informatix's ePub reader is great! Is there a way to get the plain text of each opened bookpage?
I would like to use this ePub reader in my app for blind users, i.e. use TTS to read a book aloud.
Thank you in advance.
 

Syd Wright

Well-Known Member
Licensed User
I managed to solve this problem myself, using WebViewExtras version 1.42.
Here is the code (based on Informatix's sample project) for anyone who is interested:
B4X:
#Region  Project Attributes
    #ApplicationLabel: ePUB demo
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #IgnoreWarnings: 15, 16
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim Reader As ePubReader
    Dim Navigator As ePubNavigator
End Sub

Sub Globals
    Private WbRd As WebReader
    Private btnPrevious As Button
    Private btnNext As Button
    Private Label1 As Label
    Private btnDecrease As Button
    Private btnIncrease As Button
    Private btnMode As Button

    Dim BaseURL As String
    Dim WebViewMode As Boolean
    Dim TextZoom As Int = 120 * Density

    Dim WebViewExtras1 As WebViewExtras

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main.bal")

    File.Copy(File.DirAssets, "testbook1.epub", File.DirInternal, "testbook.epub")

    WebViewExtras1.AddJavascriptInterface(WbRd, "B4A")
    WebViewExtras1.addWebChromeClient(WbRd,"WebViewExtras1")   'Niet echt nodig

    WebViewMode = True
    SwitchMode 'Switches to the WebReader mode

    Reader.Initialize
    OpenFile(File.Combine(File.DirInternal, "testbook.epub"))
End Sub

Sub OpenFile(ePubFile As String)
    Dim UnZipFolder As String = File.Combine(File.DirInternalCache, "epub")
    Reader.UnZip(ePubFile, UnZipFolder, "Unzip")

    Dim Book As ePubBook = Reader.ReadWithoutLoading(ePubFile, "UTF-8")
    Activity.Title = Book.Title

    Dim OpfResource As String = Book.OpfResource.Href
    BaseURL = "file://" & File.Combine(UnZipFolder, OpfResource.SubString2(0, Max(0, OpfResource.LastIndexOf("/"))))
    Log("BaseURL=" & BaseURL)

    Navigator.Initialize(Book, "Navig")
    Navigator.GoToFirstSpineSection
    UpdateViews
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        'Cleans the cache folder
        Dim CacheDir As String = BaseURL.Replace("file://", "")
        Dim LstFiles As List = File.ListFiles(CacheDir)
        For i = 0 To LstFiles.Size - 1
            File.Delete(CacheDir, LstFiles.Get(i))
        Next
    End If
End Sub

Sub UpdateViews
    btnPrevious.Enabled = Navigator.HasPreviousSpineSection
    btnNext.Enabled = Navigator.HasNextSpineSection
    Label1.Text = Navigator.CurrentSpinePos & " | " & Navigator.CurrentResource.Id
    WbRd.LoadUrl(File.Combine(BaseURL, Navigator.CurrentResource.Href))
End Sub

Sub btnPrevious_Click
    Navigator.GoToPreviousSpineSection
    UpdateViews
End Sub

Sub btnNext_Click
    Navigator.GoToNextSpineSection
    UpdateViews
End Sub

Sub Unzip_Progression(NbOfUnzippedFiles As Int, UnzippedFile As String)
    'This event is raised after each file is unzipped by Reader.UnZip
    Log("Unzip: " & NbOfUnzippedFiles & " " & UnzippedFile)
End Sub

Sub Navig_AfterJump(Event As ePubNavigationEvent)
    'This event is raised when the GoTo functions of the ePUB navigator are called
    Log("Jump: " & Event.OldSpinePos & " -> " & Event.CurrentSpinePos)
    Log("      " & Event.OldResource.Id & " -> " & Event.CurrentResource.Id)
End Sub

Sub WbRd_OverrideUrl (Url As String) As Boolean
    'This event is raised when a link is clicked in the WebView
    Log("Link: " & Url)
    Dim Href As String = Url.Replace(BaseURL, "")
    If Href.StartsWith("/") Then Href = Href.SubString(1)
    Log("Href: " & Href)
    If Navigator.GoToResHref(Href) = -1 Then
      'Resource not found (it's probably an external link)
      Return False
    End If
    UpdateViews
    Return True 'The change is delegated to the ePUB navigator
End Sub

Sub WbRd_PageFinished (Url As String) 'Event
    Dim Javascript As String   
     'Javascript="B4A.CallSub('HTML_EPub1', true, document.activeElement.outerHTML)"   'ActiveElement werkt wel!
     Javascript="B4A.CallSub('Read_EPub1', true, document.documentElement.innerText)"   'ActiveElement werkt wel!
    WebViewExtras1.executeJavascript(WbRd, Javascript)   
End Sub

Sub Read_Epub1(Read1 As String)   
    Log(Read1)
End Sub

Sub btnIncrease_Click
    If WebViewMode Then Return
    WbRd.TextZoom = WbRd.TextZoom + 10
    TextZoom = WbRd.TextZoom
End Sub

Sub btnDecrease_Click
    If WebViewMode Then Return
    WbRd.TextZoom = Max(50, WbRd.TextZoom - 10)
    TextZoom = WbRd.TextZoom
End Sub

Sub SwitchMode
    If WebViewMode Then
        'Switches to the WebReader mode
        WbRd.LoadWithOverviewMode = True
        WbRd.UseViewportMetaTag = True
        WbRd.TextZoom = TextZoom
        btnMode.Text = "WReader"
    Else
        'Switches to the WebView mode (WebReader with default settings)
        WbRd.LoadWithOverviewMode = False
        WbRd.UseViewportMetaTag = False
        WbRd.TextZoom = 100
        btnMode.Text = "WebView"
    End If
    WebViewMode = Not(WebViewMode)
End Sub

Sub btnMode_Click
    SwitchMode
End Sub
 
Top