From 7c846b4811bdf5bd2cbccedae9dc41ca5df654c0 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Wed, 21 Apr 2021 11:18:15 +0200 Subject: [PATCH 1/2] adding open 'typed' and InAppBrowser functions into '1.1' version --- README.md | 26 ++++++++++++++++ cordova-plugin-inappbrowser.opam | 10 ++++-- src/cordova_in_app_browser.mli | 52 ++++++++++++++++++++++++++++++++ src/dune | 4 +-- 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5bf8c4d..18ee05a 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 returns 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)) ) From 5eeb07803f1e574a6b4bf9bfeda7984a9e74ce88 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Fri, 3 Sep 2021 17:27:12 +0200 Subject: [PATCH 2/2] relax opam dependences --- cordova-plugin-inappbrowser.opam | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cordova-plugin-inappbrowser.opam b/cordova-plugin-inappbrowser.opam index b94aca4..7773b13 100644 --- a/cordova-plugin-inappbrowser.opam +++ b/cordova-plugin-inappbrowser.opam @@ -13,6 +13,6 @@ 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"} + "js_of_ocaml" {>= "3.8.0"} + "js_of_ocaml-ppx" {>= "3.8.0"} ]