Platforms to show: All Mac Windows Linux Cross-Platform

/Mac64bit/WebKit2/WebView


Required plugins for this example: MBS MacBase Plugin, MBS Mac64bit Plugin, MBS Main Plugin, MBS MacCocoa Plugin, MBS MacControls Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Mac64bit/WebKit2/WebView

This example is the version from Wed, 28th Jun 2022.

Project "WebView.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control browser Inherits WKWebViewControlMBS
ControlInstance browser Inherits WKWebViewControlMBS
EventHandler Sub JavaScriptEvaluated(JavaScript as String, Result as Variant, Error as NSErrorMBS, Tag as String) List.AddRow CurrentMethodName List.AddRow "JavaScript", JavaScript if Error = nil then List.AddRow "Error", "none" else List.AddRow "Error", Error.LocalizedDescription end if List.AddRow "Tag", Tag List.AddRow "" List.ScrollPosition = List.ListCount MsgBox "JavaScript result: "+result.StringValue End EventHandler
EventHandler Sub Open() Dim f As FolderItem = FindFile("test.html") dim b as BinaryStream = BinaryStream.Open(f) dim s as string = b.Read(B.Length, encodings.UTF8) me.LoadHTML s End EventHandler
EventHandler Sub decidePolicyForNavigationAction(navigationAction as WKNavigationActionMBS, decisionHandler as WKPolicyForNavigationDecisionHandlerMBS) List.AddRow CurrentMethodName List.AddRow "URL", navigationAction.request.URL List.AddRow "NavigationType", navigationAction.navigationType.ToString List.AddRow "modifierFlags", navigationAction.modifierFlags.ToString List.AddRow "buttonNumber", navigationAction.buttonNumber.ToString List.AddRow "newWindow", navigationAction.newWindow.ToString List.AddRow "shouldPerformDownload", navigationAction.shouldPerformDownload.ToString List.AddRow "" List.ScrollPosition = List.ListCount // either cancel, download or allow decisionHandler.Allow End EventHandler
EventHandler Sub didCloseContextualMenu(menu as NSMenuMBS, NSEvent as NSEventMBS) List.AddRow CurrentMethodName End EventHandler
EventHandler Sub didCommitNavigation(navigation as WKNavigationMBS) List.AddRow CurrentMethodName End EventHandler
EventHandler Sub didFailNavigation(navigation as WKNavigationMBS, Error as NSErrorMBS) List.AddRow CurrentMethodName if error <> nil then List.AddRow "error", Error.LocalizedDescription end if End EventHandler
EventHandler Sub didFailProvisionalNavigation(navigation as WKNavigationMBS, Error as NSErrorMBS) List.AddRow CurrentMethodName if error <> nil then List.AddRow "error", Error.LocalizedDescription end if End EventHandler
EventHandler Sub didFinishNavigation(navigation as WKNavigationMBS) List.AddRow CurrentMethodName End EventHandler
EventHandler Sub didReceiveScriptMessage(Body as Variant, name as String) MsgBox name+EndOfLine+body.StringValue End EventHandler
EventHandler Sub didReceiveServerRedirectForProvisionalNavigation(navigation as WKNavigationMBS) List.AddRow CurrentMethodName End EventHandler
EventHandler Sub didStartProvisionalNavigation(navigation as WKNavigationMBS) List.AddRow CurrentMethodName End EventHandler
EventHandler Sub runJavaScriptAlertPanel(initiatedByFrame as WKFrameInfoMBS, message as String) List.AddRow CurrentMethodName List.AddRow "Message", Message List.AddRow "" List.ScrollPosition = List.ListCount MsgBox message End EventHandler
EventHandler Function runJavaScriptConfirmPanel(initiatedByFrame as WKFrameInfoMBS, message as String) As boolean List.AddRow CurrentMethodName List.AddRow "Message", Message List.AddRow "" List.ScrollPosition = List.ListCount Dim d as New MessageDialog //declare the MessageDialog object d.icon=MessageDialog.GraphicCaution //display warning icon d.ActionButton.Caption="Confirm" d.CancelButton.Visible=True //show the Cancel button d.Message=message Dim b as MessageDialogButton = d.ShowModal //display the dialog Select Case b //determine which button was pressed. Case d.ActionButton //user pressed Confirm Return true End select End EventHandler
EventHandler Function runJavaScriptTextInputPanel(initiatedByFrame as WKFrameInfoMBS, prompt as String, defaultText as String) As String List.AddRow CurrentMethodName List.AddRow "prompt", prompt List.AddRow "defaultText", defaultText List.AddRow "" List.ScrollPosition = List.ListCount dim r as string dim d as new InputDialog d.Message.Text = prompt d.InputField.Text = defaultText d.ShowModal if d.ok then r = d.InputField.Text end if d.close Return r End EventHandler
EventHandler Sub willShowContextualMenu(menu as NSMenuMBS, NSEvent as NSEventMBS) List.AddRow CurrentMethodName // remove all 'menu.removeAllItems // subclass to catch event (or use addHandler) Dim m As New MyNSMenuItemMBS m.CreateMenuItem m.Title = "Hello World" // no subclass for separator needed Dim s As New NSMenuItemMBS s.CreateSeparator // add our items menu.addItem s menu.addItem m // make sure Xojo objects stay alive for some time LastContextMenuItem = m LastContextMenu = menu End EventHandler
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control TryButton Inherits BevelButton
ControlInstance TryButton Inherits BevelButton
EventHandler Sub Action() browser.EvaluateJavaScript("window.webkit.messageHandlers.test.postMessage('Hello');") End EventHandler
End Control
EventHandler Sub Open() #if Target64Bit then Title = "WebKit 2 with 64-bit" #else Title = "WebKit 1 with 32-bit" #endif #if Target64Bit then browser.addScriptMessageHandler "test" #endif End EventHandler
Function FindFile(name as string) As FolderItem // Look for file in parent folders from executable on dim parent as FolderItem = app.ExecutableFile.Parent while parent<>Nil dim file as FolderItem = parent.Child(name) if file<>Nil and file.Exists then Return file end if parent = parent.Parent wend End Function
Property LastContextMenu As NSMenuMBS
Property LastContextMenuItem As MyNSMenuItemMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class InputDialog Inherits Window
Control Message Inherits Label
ControlInstance Message Inherits Label
End Control
Control InputField Inherits TextArea
ControlInstance InputField Inherits TextArea
End Control
Control CancelButton Inherits PushButton
ControlInstance CancelButton Inherits PushButton
EventHandler Sub Action() ok = false hide End EventHandler
End Control
Control OKButton Inherits PushButton
ControlInstance OKButton Inherits PushButton
EventHandler Sub Action() ok = true hide End EventHandler
End Control
Property ok As Boolean
End Class
Class MyNSMenuItemMBS Inherits NSMenuItemMBS
EventHandler Sub Action() MainWindow.List.AddRow CurrentMethodName MsgBox "Context menu entry clicked." End EventHandler
End Class
End Project

See also:

The items on this page are in the following plugins: MBS Mac64bit Plugin.


The biggest plugin in space...