Page 1 of 1
WebGadget does not print (or how to enable JavaScript?)
Posted: Tue Oct 02, 2012 3:06 pm
by Kukulkan
Hi,
I'm using such code to print the content of a WebGadget in my PureBasic application:
Code: Select all
File.s = "c:\temp\test.htm" ; Adapt to OS
If OpenFile(1, File.s)
WriteString(1, "<html><body><p>This is a simple test text</p><script>window.print();</script></body></html>")
CloseFile(1)
EndIf
If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(0, 10, 10, 580, 280, "file://" + File.s)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
It works fine on Windows and Linux, but sadly on MacOS simply nothing happens. To test use this path for MacOS:
How to make the WegGadget print? Is JavaScript disabled on MacOS?
Kukulkan
Re: WebGadget does not print
Posted: Tue Oct 02, 2012 3:16 pm
by Kukulkan
Hi,
I just found out that JavaScript is simply disabled:
Code: Select all
File.s = "/tmp/test.htm" ; Adapt to OS
If FileSize(File.s) > -1: DeleteFile(File.s): EndIf
If OpenFile(1, File.s)
WriteString(1, "<html><body><p>This is a simple test text</p><script>alert('test');</script></body></html>")
CloseFile(1)
EndIf
If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(0, 10, 10, 580, 280, "file://" + File.s)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
How to enable?
Kukulkan
Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Tue Oct 02, 2012 4:52 pm
by wilbert
JavaScript itself seems to be working.
Try this
Code: Select all
If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(0, 10, 10, 580, 240, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body><p>This is a simple test text</body></html>")
ButtonGadget(1, 10, 260, 200, 30, "Evaluate script")
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case 1
ScriptObject = CocoaMessage(0, GadgetID(0), "windowScriptObject")
CocoaMessage(0, ScriptObject, "evaluateWebScript:$", @"location.href='www.purebasic.com'")
EndSelect
EndIf
Until EventID = #PB_Event_CloseWindow
EndIf
You will see it loads the PureBasic site when the script is evaluated.
Maybe print and alert aren't supported from within a WebGadget.
If you want to print, you can do it like this
Code: Select all
If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(0, 10, 10, 580, 240, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body><p>This is a simple test text</body></html>")
ButtonGadget(1, 10, 260, 200, 30, "Print")
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case 1
PrintInfo = CocoaMessage(0, 0, "NSPrintInfo sharedPrintInfo")
FrameView = CocoaMessage(0, CocoaMessage(0, GadgetID(0), "mainFrame"), "frameView")
Operation = CocoaMessage(0, FrameView, "printOperationWithPrintInfo:", PrintInfo)
CocoaMessage(0, Operation, "setShowsPrintPanel:", #True)
CocoaMessage(0, Operation, "runOperation")
EndSelect
EndIf
Until EventID = #PB_Event_CloseWindow
EndIf
Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Tue Oct 02, 2012 5:19 pm
by wilbert
After some more testing ...
There does seem to be an option to turn JavaScript on and off and it seems to remember the last setting and not being enabled or disabled by PureBasic.
Maybe Fred can enable it just to be sure
Code: Select all
If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(0, 10, 10, 580, 240, "")
Pref = CocoaMessage(0, GadgetID(0), "preferences")
CocoaMessage(0, Pref, "setJavaScriptEnabled:", #True)
CocoaMessage(0, GadgetID(0), "setPreferences:", Pref)
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body><p>This is a <a href='javascript:location.href="+Chr(34)+"www.purebasic.com" + Chr(34)+ "'>simple</a> test text</body></html>")
Repeat
EventID = WaitWindowEvent()
Until EventID = #PB_Event_CloseWindow
EndIf
Try to set enabled to false and the javascript won't work anymore.
Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Wed Oct 03, 2012 10:42 am
by Kukulkan
Hi,
JavaScript itself seems to be working.
No. Even if I replace the print script code with some alert('test'); message, it does not work. But it works if I open the html file in normal webbrowser.
There does seem to be an option to turn JavaScript on and off and it seems to remember the last setting and not being enabled or disabled by PureBasic.
This explains it much better. So I have to ensure that JS is enabled, otherwise my cross plattform print option does not work. I will try the outlined option and will report here tomorrow.
Thank you!
Kukulkan
Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Wed Oct 03, 2012 11:30 am
by wilbert
Unfortunately print and alert seem to be special javascript cases.
A little explanation ...
The PureBasic WebGadget is a subclass of the cocoa WebView class.
The interface of a WebView is handled by a delegate that needs to conform to the WebUIDelegate protocol.
There are two methods of the delegate that control print and alert
webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:
webView:printFrameView:
When those methods are not implemented, nothing happens.
It does make sense since it gives the application that embeds the WebView control of what should happen.
It could for example block printing of some sites, or allow alerts only from a specific site.
So basically javascript print and alert will only work if Fred implements those delegate methods or if you create a delegate class with PureBasic itself which is not very convenient.
Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Wed Oct 03, 2012 12:50 pm
by jesperbrannmark
The windows version of Purebasic supports the
call. It would make sense the Mac OS one also supports this...
(the pc one also supports <a target=_new> which the mac one doesnt)
Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Thu Oct 04, 2012 9:23 am
by Kukulkan
Hi,
Printing using enabled JavaScript does not work. Even with wilbert's tip.
Printing using
printOperationWithPrintInfo: function works. It is still not good to miss a plattform independent print option but for now it's ok.
Kukulkan
Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Sun Jul 27, 2014 1:32 am
by spacebuddy
If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(0, 10, 10, 580, 240, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body><p>This is a simple test text</body></html>")
ButtonGadget(1, 10, 260, 200, 30, "Print")
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case 1
PrintInfo = CocoaMessage(0, 0, "NSPrintInfo sharedPrintInfo")
FrameView = CocoaMessage(0, CocoaMessage(0, GadgetID(0), "mainFrame"), "frameView")
Operation = CocoaMessage(0, FrameView, "printOperationWithPrintInfo:", PrintInfo)
CocoaMessage(0, Operation, "setShowsPrintPanel:", #True)
CocoaMessage(0, Operation, "runOperation")
EndSelect
EndIf
Until EventID = #PB_Event_CloseWindow
EndIf
Can this be adapted to work with the EditorGadGet or does it only work on a WebGadGet?
Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Sun Jul 27, 2014 2:13 am
by Danilo
spacebuddy wrote:Can this be adapted to work with the EditorGadGet or does it only work on a WebGadGet?
Code: Select all
CocoaMessage(0,GadgetID(editorGadget),"print:",0) ; NSView Class

Re: WebGadget does not print (or how to enable JavaScript?)
Posted: Sun Jul 27, 2014 2:22 am
by spacebuddy
Thank you Danilo
