Is it possible to save HTML from the HTMLViewer?

I would like to know, thanks.

Not directly. You will need HTTPSocket or HTTPSecureSocket to get the url and save the content to a file. Then use HTMLViewer.LoadPage

or you use our MBS Plugins and use HTMLTextMBS / IEHTMLTextMBS methods.

1 Like

I forgot to add that I was talking about html rendered via Javascript, without a HTML File. Or would I have to use Javascript itself to save the file?

Thanks,
Shane.

This is how I get the source code from a HTMLViewer… I do this as a sub-class, but I am sure you can think of a way to do it without sub-classing if need be…

// computed property

[code]Source As String

Get
me.ExecuteJavaScript “window.status = document.getElementsByTagName(‘body’)[0].innerHTML;”
Return me.CallbackData
End Get
Set

End Set[/code]

// property

Private CallbackData As String

// event handler

Sub StatusChanged(newStatus as String) if (newStatus <> "") then me.CallbackData = newStatus end if End Sub

When you request the source computed property, it runs the javascript and sets the window.status property to the source… Once the window.status has been changed, the event handler is called and it stores the window.status in to the CallbackData property, which is then returned from the computed property… Yes, it does work, I use it a lot in my program…

[quote=40231:@shao sean]This is how I get the source code from a HTMLViewer… I do this as a sub-class, but I am sure you can think of a way to do it without sub-classing if need be…

// computed property

[code]Source As String

Get
me.ExecuteJavaScript “window.status = document.getElementsByTagName(‘body’)[0].innerHTML;”
Return me.CallbackData
End Get
Set

End Set[/code]

// property

Private CallbackData As String

// event handler

Sub StatusChanged(newStatus as String) if (newStatus <> "") then me.CallbackData = newStatus end if End Sub

When you request the source computed property, it runs the javascript and sets the window.status property to the source… Once the window.status has been changed, the event handler is called and it stores the window.status in to the CallbackData property, which is then returned from the computed property… Yes, it does work, I use it a lot in my program…[/quote]

Thanks.

I am having a problem with my code that writes to the document.

[code] Dim BtnDefault As String

If ColumnsToUse.ListIndex = 0 Then
BtnDefault = “document.write(”"<button class="“btn btn-default”">Button"");"
HTMLViewer1.ExecuteJavaScript(BtnDefault)
End If
[/code]

Yet, when I click the button, nothing happens and when I view the HTML source (right click>>view source), the html code hasn’t been written.

Any ideas?

You’re probably getting a JavaScript error. The code you’ve written will be evaluated as:

document.write("<button class="btn btn-default">Button</button>");

Notice the quotes? I suggest escaping the innermost ones or making them single quotes around the class.

[quote=40247:@Greg O’Lone]You’re probably getting a JavaScript error. The code you’ve written will be evaluated as:

document.write("<button class="btn btn-default">Button</button>");

Notice the quotes? I suggest escaping the innermost ones or making them single quotes around the class.[/quote]

Nope. Using single quotes has the javascript show as actual Xojo code. I thought that is what double quotes were for. Because you quote the html you want to output.

HTMLViewer1.ExecuteJavaScript "document.write(""<button class='btn btn-default'>Button</button>"");"

Well, after more than six years…

That @shao sean’s code is great for grabbing the content of the HTMLViewer, that goes without saying. Thanks to Shao.

On the other end, is there a way to grab the content when the HTMLViewer loads a pdf file?

In fact, the element passed in Shao’s code is ‘body’:

Get me.ExecuteJavaScript "window.status = document.getElementsByTagName('[b]body[/b]')[0].innerHTML;" Return me.CallbackData End Get

but when the HTMLViewer loads a pdf file, ‘body’ or ‘html’ do not contain any proper text. The pdf’s content is referred in
<embed… and name=“plugin”, as shown below.

Given these data (copied from a pdf file open in Safari > Developer > Web Inspector) is there any modification by which Shao’s original code could grab the text itself?
I guess the answer is “No”. But maybe some guru may prove me wrong.
Thank you.

<[b]embed[/b] width="100%" height="100%" name="[b]plugin[/b]" src="file:///somefile.pdf" type="application/pdf">
#annotationContainer { overflow: hidden; position: absolute; pointer-events: none; top: 0; left: 0; right: 0; bottom: 0; display: -webkit-box; -webkit-box-align: center; -webkit-box-pack: center; } .annotation { position: absolute; pointer-events: auto; } textarea.annotation { resize: none; } input.annotation[type='password'] { position: static; width: 200px; margin-top: 100px; }

6 years later, plus 7 months later…did you figure this out? I have some javascript that generates a pdf, and would like to save it locally.

For regular HTML you could execute some JavaScript like this using the newer functionality of the HTMLViewer:

window.executeInXojo(document.getElementsByTagName('html')[0].innerHTML);

I’m not sure how you’d do it with a PDF.

It’s quite frustrating, as this would be perfect. I have the pdf created and in a blob format, but can’t figure out how to get it out of the htmlviewer. LOL I can view it in an iframe, but the little save icon doesn’t do it’s job, it just clicks.

Then maybe:

window.executeInXojo(myPDFBlob);

As far as I can tell, you can only transmit strings and numbers with that. But maybe I can convert the blob to a string, get it into Xojo, and then convert it back somehow. I’ll have to play around.

You can use JavaScript’s btoa function to convert to BASE64 that you can then decode in Xojo.

Unfortunatelly I did not.

I couldn’t do it with btoa (invalid characters), but using FileReader to convert to Base64, then DecodeBase64ing it in Xojo seems to work. Holy cow!

Thanks…

Happy to help.

1 Like