Skip to content

Commit

Permalink
adding open 'typed' and InAppBrowser functions into '1.1' version
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaut committed Aug 4, 2021
1 parent be4285d commit 495295a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,29 @@ Cordova_in_app_browser.ai_clear_cache true] in
(* Opens in the Cordova WebView if the URL is in the white list *)
i#open_ "https://ocaml.org" (Cordova_in_app_browser.target_self) opt
```

The function "open_t" is the same as "open" but it return a "typed"
result: an object of type "inAppBrowser" that can be used later. An
"inAppBrowser" object is, for example, the only way to to call the
functions "addEventListener" and "close" of this library.

```OCaml
let popup =
Cordova_in_app_browser.open_t
~url:"https://www.google.com" ~tgt:Blank
~option:"location=yes"
in
Cordova_in_app_browser.addEventListener popup ~eventname:"loadstop"
~f:(fun evt ->
ignore (print_endline "Hello World");
Cordova_in_app_browser.close popup)
```

With this functions, you can access the 5 properties of the argument of
an added "EventListener":

° `Cordova_in_app_browser.type`: either "loadstart", "loadstop", "loaderror", "message", or "exit"
°`Cordova_in_app_browser.url`: not available for every event type (not for "exit").
° `Cordova_in_app_browser.code`: only available for event with an error code.
° `Cordova_in_app_browser.message`: only available for "loaderror" event type
° `Cordova_in_app_browser.data`: onlys available for "message" event type
10 changes: 7 additions & 3 deletions cordova-plugin-inappbrowser.opam
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
opam-version: "2.0"
version: "1.1"
maintainer: "Danny Willems <[email protected]>"
authors: "Danny Willems <[email protected]>"
homepage: "https://github.com/dannywillems/ocaml-cordova-plugin-inappbrowser"
bug-reports: "https://github.com/dannywillems/ocaml-cordova-plugin-inappbrowser/issues"
dev-repo: "https://github.com/dannywillems/ocaml-cordova-plugin-inappbrowser"
homepage: "git+https://github.com/dannywillems/ocaml-cordova-plugin-inappbrowser"
bug-reports: "git+https://github.com/dannywillems/ocaml-cordova-plugin-inappbrowser/issues"
dev-repo: "git+https://github.com/dannywillems/ocaml-cordova-plugin-inappbrowser"
license: "LGPL-3.0 with OCaml linking exception"
description: "Binding to the inappbrowser cordova plugin using gen_js_api"
synopsis: "Binding to the inappbrowser cordova plugin using gen_js_api"
build: [[ "dune" "build" "-j" jobs "-p" name "@install" ]]
depends: [
"ocaml" { >= "4.03.0" }
"dune" {>= "2.8"}
"gen_js_api"
"js_of_ocaml" {>= "3.8.0" & < "3.9.0"}
"js_of_ocaml-ppx" {>= "3.8.0" & < "3.9.0"}
]
52 changes: 52 additions & 0 deletions src/cordova_in_app_browser.mli
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,55 @@ val open_ :
(* Options for the InAppBrowser *)
unit
[@@js.global "cordova.InAppBrowser.open"]

(*Function that allow to use "InAppBrowser" object*)
type inAppBrowser

(*Same as "open_" but return a "typed" result: an "InAppBrowser" result*)
val open_t : url:string -> tgt:target -> option:string -> inAppBrowser
[@@js.global "cordova.InAppBrowser.open"]

type inAppBrowserEvent

val addEventListener :
inAppBrowser ->
(*Where to add the listener*)
eventname:string ->
(*The event name*)
f:(inAppBrowserEvent -> unit) ->
(*The call_back*)
unit
[@@js.call]

(*Getting access to the "InAppBrowserEvent" properties*)

val type_ : inAppBrowserEvent -> string [@@js.get "type"]

val url : inAppBrowserEvent -> string [@@js.get "url"]

val code : inAppBrowserEvent -> int [@@js.get "code"]

val message : inAppBrowserEvent -> string [@@js.get "message"]

val data : inAppBrowserEvent -> string [@@js.get "data"]

(*Indicate to close an open InAppBrowser object*)
val close : inAppBrowser -> unit [@@js.call]

[@@@js.stop]

(*Return the "data" propertie of an inAppBrowserEvent in the form of an json object*)
val data_json : inAppBrowserEvent -> Ojs.t

(*That function indicate if the plugin "cordova-InAppBrowser" is currently available*)
val plugin_available : unit -> bool

[@@@js.start]

[@@@js.implem
let data_json evt = Js_of_ocaml.Js.Unsafe.global##._JSON##stringify (data evt)]

[@@@js.implem
let plugin_available () =
Js_of_ocaml.Js.Optdef.test
Js_of_ocaml.Js.Unsafe.global##.cordova##._InAppBrowser]
4 changes: 2 additions & 2 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(public_name cordova-plugin-inappbrowser)
(name cordova_in_app_browser)
(modes byte)
(libraries gen_js_api)
; (preprocess (pps gen_js_api))
(libraries gen_js_api js_of_ocaml js_of_ocaml-ppx)
(preprocess (pps js_of_ocaml-ppx))
)

0 comments on commit 495295a

Please sign in to comment.