diff --git a/README.md b/README.md index 5bf8c4d..ef6b890 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cordova-plugin-inappbrowser.opam b/cordova-plugin-inappbrowser.opam index 24009aa..b94aca4 100644 --- a/cordova-plugin-inappbrowser.opam +++ b/cordova-plugin-inappbrowser.opam @@ -1,14 +1,18 @@ opam-version: "2.0" +version: "1.1" maintainer: "Danny Willems " authors: "Danny Willems " -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"} ] diff --git a/src/cordova_in_app_browser.mli b/src/cordova_in_app_browser.mli index 8e43b27..2627abf 100644 --- a/src/cordova_in_app_browser.mli +++ b/src/cordova_in_app_browser.mli @@ -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] diff --git a/src/dune b/src/dune index 03b9000..e489c08 100644 --- a/src/dune +++ b/src/dune @@ -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)) )