This is an automated email from the ASF dual-hosted git repository. GitToTheHub pushed a commit to branch pr-options-as-object in repository https://gitbox.apache.org/repos/asf/cordova-plugin-inappbrowser.git
commit 1712f4c639cbffde2516ecb172928017c2f156f9 Author: Manuel Beck <[email protected]> AuthorDate: Thu Aug 13 23:51:18 2026 +0200 freat: support options as object - This makes them easier to define and read. - `yes` and `no` values can be set as primitve boolean values `true` and `false` - Deprecate the options as comma-separated string - Update README to use objects as options. Change `yes`/`no` values to `true`/`false` - The native side still works with a comma-separated string and `yes`/`no` values, which will be converted by JavaScript. In a future PR this can be changed to work with a JSON object. --- README.md | 104 ++++++++++++++++++++++++++-------------------------- types/index.d.ts | 10 +++-- www/inappbrowser.js | 15 ++++++-- 3 files changed, 72 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 9f0bd3f5..0c1ec35c 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ You can show helpful articles, videos, and web resources inside of your app. Use This plugin provides a web browser view that displays when calling `cordova.InAppBrowser.open()`. - var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', 'location=yes'); + var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', { location: true }); ### `window.open` @@ -102,63 +102,65 @@ instance, or the system browser. - `_blank`: Opens in the `InAppBrowser`. - `_system`: Opens in the system's web browser. -- __options__: Options for the `InAppBrowser`. Optional, defaulting to: `location=yes`. _(String)_ +- __options__: Options for the `InAppBrowser`. Optional, defaulting to: `{ location: true }`. _(Object)_ - The `options` string must not contain any blank space, and each feature's name/value pairs must be separated by a comma. Feature names are case insensitive. + Boolean values will internally conveted for the plugin to string values, to `yes` and `no`, which will be changed in the future. Feature names are case insensitive. + + On plugin version 7.0.0 and older, the options had to be passed in a comma-separated string format, which is deprecated and will be removed in a future major release. Existing strings remain supported for backward compatibility. All platforms support: - - __location__: Set to `yes` or `no` to turn the `InAppBrowser`'s location bar on or off. + - __location__: Set to `true` or `false` to turn the `InAppBrowser`'s location bar on or off. Android supports these additional options: - - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally. - - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests or `yes` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload=post` it will raise an error). - - __clearcache__: Set to `yes` to clear the shared browser's cookie cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. - - __clearsessioncache__: Set to `yes` to clear the shared session cookies from the browser's cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. + - __hidden__: set to `true` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `false` (default) to have the browser open and load normally. + - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests, or `true` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload` to `post` it will raise an error). + - __clearcache__: Set to `true` to clear the shared browser's cookie cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. + - __clearsessioncache__: Set to `true` to clear the shared session cookies from the browser's cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. - __closebuttoncaption__: set to a string to use as the close button's caption instead of a X. Note that you need to localize this value yourself. - __closebuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the - close button color from default, regardless of being a text or default X. Only has effect if user has location set to `yes`. - - __footer__: set to `yes` to show a close button in the footer similar to the iOS __Done__ button. + close button color from default, regardless of being a text or default X. Only has effect if user has location set to `true`. + - __footer__: set to `true` to show a close button in the footer similar to the iOS __Done__ button. The close button will appear the same as for the header hence use __closebuttoncaption__ and __closebuttoncolor__ to set its properties. - __footercolor__: set to a valid hex color string, for example `#00ff00` or `#CC00ff00` (`#aarrggbb`) , and it will change the footer color from default. - Only has effect if user has __footer__ set to `yes`. - - __hardwareback__: set to `yes` to use the hardware back button to navigate backwards through the `InAppBrowser`'s history. If there is no previous page, the `InAppBrowser` will close. The default value is `yes`, so you must set it to `no` if you want the back button to simply close the InAppBrowser. - - __hidenavigationbuttons__: set to `yes` to hide the navigation buttons on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`. - - __hideurlbar__: set to `yes` to hide the url bar on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`. - - __navigationbuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color of both navigation buttons from default. Only has effect if user has location set to `yes` and not hidenavigationbuttons set to `yes`. - - __toolbarcolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color the toolbar from default. Only has effect if user has location set to `yes`. - - __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, navigation buttons go to the right and close button to the left. Default value is `no`. - - __zoom__: Enables pinch-to-zoom gesture. Set to `no` to disable it. Default value is `yes`. - - __zoomcontrols__: set to `yes` to show Android browser's zoom controls, set to `no` to hide them. Default value is `yes`. The zoom controls are deprecated since Android API Level 26 (Android 8). Google recommends to disable them. - - __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`). - - __shouldPauseOnSuspend__: Set to `yes` to make InAppBrowser WebView to pause/resume with the app to stop background audio (this may be required to avoid Google Play issues like described in [CB-11013](https://issues.apache.org/jira/browse/CB-11013)). - - __useWideViewPort__: Sets whether the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is `no`, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is `yes` and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport wil [...] - - __fullscreen__: Sets whether the InappBrowser WebView is displayed fullscreen or not. In fullscreen mode, the status bar is hidden. Default value is `yes`. + Only has effect if user has __footer__ set to `true`. + - __hardwareback__: set to `true` to use the hardware back button to navigate backwards through the `InAppBrowser`'s history. If there is no previous page, the `InAppBrowser` will close. The default value is `true`, so set it to `false` if you want the back button to simply close the InAppBrowser. + - __hidenavigationbuttons__: set to `true` to hide the navigation buttons on the location toolbar, only has effect if user has location set to `true`. The default value is `false`. + - __hideurlbar__: set to `true` to hide the url bar on the location toolbar, only has effect if user has location set to `true`. The default value is `false`. + - __navigationbuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color of both navigation buttons from default. Only has effect if user has location set to `true` and not hidenavigationbuttons set to `true`. + - __toolbarcolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color the toolbar from default. Only has effect if user has location set to `true`. + - __lefttoright__: Set to `true` to swap positions of the navigation buttons and the close button. Specifically, navigation buttons go to the right and close button to the left. Default value is `false`. + - __zoom__: Enables pinch-to-zoom gesture. Set to `false` to disable it. Default value is `true`. + - __zoomcontrols__: set to `true` to show Android browser's zoom controls, set to `false` to hide them. Default value is `true`. The zoom controls are deprecated since Android API Level 26 (Android 8). Google recommends to disable them. + - __mediaPlaybackRequiresUserAction__: Set to `true` to prevent HTML5 audio or video from autoplaying (defaults to `false`). + - __shouldPauseOnSuspend__: Set to `true` to make InAppBrowser WebView to pause/resume with the app to stop background audio (this may be required to avoid Google Play issues like described in [CB-11013](https://issues.apache.org/jira/browse/CB-11013)). + - __useWideViewPort__: Sets whether the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is `false`, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is `true` and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport [...] + - __fullscreen__: Sets whether the InappBrowser WebView is displayed fullscreen or not. In fullscreen mode, the status bar is hidden. Default value is `true`. iOS supports these additional options: - - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally. - - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests or `yes` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload=post` it will raise an error). - - __clearcache__: Set to `yes` to clear the browser's cookie cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. - - __clearsessioncache__: Set to `yes` to clear the shared session cookies from the browser's cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. - - __cleardata__: Set to `yes` to clear the shared browser's entire local storage of your app, which means also the one of the Cordova WebView, like cookies, HTML5 local storage, IndexedDB, etc., before the in-app browser is opened. + - __hidden__: set to `true` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `false` (default) to have the browser open and load normally. + - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests, or `true` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload` to `post` it will raise an error). + - __clearcache__: Set to `true` to clear the browser's cookie cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. + - __clearsessioncache__: Set to `true` to clear the shared session cookies from the browser's cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. + - __cleardata__: Set to `true` to clear the shared browser's entire local storage of your app, which means also the one of the Cordova WebView, like cookies, HTML5 local storage, IndexedDB, etc., before the in-app browser is opened. - __closebuttoncolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default __Done__ button's color. Only applicable if toolbar is not disabled. - __closebuttoncaption__: set to a string to use as the __Done__ button's caption. Note that you need to localize this value yourself. - - __disallowoverscroll__: Set to `yes` or `no` (default is `no`). Turns on/off the the bounce of the WKWebView's UIScrollView. - - __hidenavigationbuttons__: set to `yes` or `no` to turn the toolbar navigation buttons on or off (defaults to `no`). Only applicable if toolbar is not disabled. + - __disallowoverscroll__: Set to `true` or `false` (default is `false`). Turns on/off the the bounce of the WKWebView's UIScrollView. + - __hidenavigationbuttons__: set to `true` or `false` to turn the toolbar navigation buttons on or off (defaults to `false`). Only applicable if toolbar is not disabled. - __navigationbuttoncolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default color. Only applicable if navigation buttons are visible. - - __toolbar__: set to `yes` or `no` to turn the toolbar on or off for the InAppBrowser (defaults to `yes`) + - __toolbar__: set to `true` or `false` to turn the toolbar on or off for the InAppBrowser (defaults to `true`) - __toolbarcolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default color of the toolbar. Only applicable if toolbar is not disabled. - - __toolbartranslucent__: set to `yes` or `no` to make the toolbar translucent(semi-transparent). Only applicable if toolbar is not disabled. The toolbar is semi-transparent by default. - - __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, close button goes to the right and navigation buttons to the left. - - __enableViewportScale__: Set to `yes` or `no` to prevent viewport scaling through a meta tag (defaults to `no`). - - __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`). - - __allowInlineMediaPlayback__: Set to `yes` or `no` to allow in-line HTML5 media playback, displaying within the browser window rather than a device-specific playback interface. The HTML's `video` element must also include the `webkit-playsinline` attribute (defaults to `no`). + - __toolbartranslucent__: set to `true` or `false` to make the toolbar translucent(semi-transparent). Only applicable if toolbar is not disabled. The toolbar is semi-transparent by default. + - __lefttoright__: Set to `true` to swap positions of the navigation buttons and the close button. Specifically, close button goes to the right and navigation buttons to the left. + - __enableViewportScale__: Set to `true` or `false` to prevent viewport scaling through a meta tag (defaults to `false`). + - __mediaPlaybackRequiresUserAction__: Set to `true` to prevent HTML5 audio or video from autoplaying (defaults to `false`). + - __allowInlineMediaPlayback__: Set to `true` or `false` to allow in-line HTML5 media playback, displaying within the browser window rather than a device-specific playback interface. The HTML's `video` element must also include the `webkit-playsinline` attribute (defaults to `false`). - __presentationstyle__: Set to `pagesheet`, `formsheet` or `fullscreen` to set the [presentation style](https://developer.apple.com/documentation/uikit/uimodalpresentationstyle) (defaults to `fullscreen`). - __transitionstyle__: Set to `fliphorizontal`, `crossdissolve` or `coververtical` to set the [transition style](https://developer.apple.com/documentation/uikit/uimodaltransitionstyle) (defaults to `coververtical`). - __toolbarposition__: Set to `top` or `bottom` (default is `bottom`). Causes the toolbar to be at the top or bottom of the window. - - __hidespinner__: Set to `yes` or `no` to change the visibility of the loading indicator (defaults to `no`). + - __hidespinner__: Set to `true` or `false` to change the visibility of the loading indicator (defaults to `false`). ### Supported Platforms @@ -169,8 +171,8 @@ instance, or the system browser. ### Example - var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', 'location=yes'); - var ref2 = cordova.InAppBrowser.open(encodeURI('http://ja.m.wikipedia.org/wiki/ハングル'), '_blank', 'location=yes'); + var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', { location: true }); + var ref2 = cordova.InAppBrowser.open(encodeURI('http://ja.m.wikipedia.org/wiki/ハングル'), '_blank', { location: true }); ### iOS Quirks @@ -231,7 +233,7 @@ The object returned from a call to `cordova.InAppBrowser.open` when the target i function showHelp(url) { - let inAppBrowserRef = cordova.InAppBrowser.open(url, "_blank", "location=yes,hidden=yes,beforeload=yes"); + let inAppBrowserRef = cordova.InAppBrowser.open(url, "_blank", { location: true, hidden: true, beforeload: true }); inAppBrowserRef.addEventListener('loadstart', (inAppBrowserEvent) => { document.getElementById("status-message").textContent = "loading please wait ..."; @@ -371,7 +373,7 @@ function downloadListener(params){ ### Quick Example - var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', 'location=yes'); + var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', { location: true }); ref.addEventListener('loadstart', function(event) { alert(event.url); }); ## InAppBrowser.removeEventListener @@ -403,7 +405,7 @@ The function is passed an `InAppBrowserEvent` object. ### Quick Example - var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', 'location=yes'); + var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', { location: true }); var myCallback = function(event) { alert(event.url); } ref.addEventListener('loadstart', myCallback); ref.removeEventListener('loadstart', myCallback); @@ -424,7 +426,7 @@ The function is passed an `InAppBrowserEvent` object. ### Quick Example - var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', 'location=yes'); + var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', { location: true }); ref.close(); ## InAppBrowser.show @@ -443,7 +445,7 @@ The function is passed an `InAppBrowserEvent` object. ### Quick Example - var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', 'hidden=yes'); + var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', { hidden: true }); // some time later... ref.show(); @@ -493,7 +495,7 @@ The function is passed an `InAppBrowserEvent` object. ### Quick Example - var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', 'location=yes'); + var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', { location: true }); ref.addEventListener('loadstop', function() { ref.executeScript({file: "myscript.js"}); }); @@ -523,7 +525,7 @@ The function is passed an `InAppBrowserEvent` object. ### Quick Example - var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', 'location=yes'); + var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', { location: true }); ref.addEventListener('loadstop', function() { ref.insertCSS({file: "mystyles.css"}); }); @@ -588,7 +590,7 @@ $('#help-select').on('change', function (e) { ### <a id="load"></a>Load a help page -We'll use the ``open`` function to load the help page. We're setting the ``hidden`` property to ``yes`` so that we can show the browser only after the page content has loaded. That way, users don't see a blank browser while they wait for content to appear. When the ``loadstop`` event is raised, we'll know when the content has loaded. We'll handle that event shortly. +We'll use the ``open`` function to load the help page. We're setting the ``hidden`` property to ``true`` so that we can show the browser only after the page content has loaded. That way, users don't see a blank browser while they wait for content to appear. When the ``loadstop`` event is raised, we'll know when the content has loaded. We'll handle that event shortly. ```javascript @@ -596,7 +598,7 @@ function showHelp(url) { var target = "_blank"; - var options = "location=yes,hidden=yes"; + var options = { location: true, hidden: true }; inAppBrowserRef = cordova.InAppBrowser.open(url, target, options); @@ -694,7 +696,7 @@ iab.open('local-url.html', '_self'); // loads in the Cordova WebView iab.open('local-url.html', '_system'); // Security error: system browser, but url will not load (iOS) iab.open('local-url.html', '_blank'); // loads in the InAppBrowser iab.open('local-url.html', 'random_string'); // loads in the InAppBrowser -iab.open('local-url.html', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar +iab.open('local-url.html', 'random_string', { location: false }); // loads in the InAppBrowser, no location bar ``` @@ -711,7 +713,7 @@ iab.open('https://whitelisted-url.com', '_system'); // loads in the system iab.open('https://whitelisted-url.com', '_blank'); // loads in the InAppBrowser iab.open('https://whitelisted-url.com', 'random_string'); // loads in the InAppBrowser -iab.open('https://whitelisted-url.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar +iab.open('https://whitelisted-url.com', 'random_string', { location: false }); // loads in the InAppBrowser, no location bar ``` @@ -727,6 +729,6 @@ iab.open('https://url-that-fails-whitelist.com', '_self'); // Android: l iab.open('https://url-that-fails-whitelist.com', '_system'); // loads in the system browser iab.open('https://url-that-fails-whitelist.com', '_blank'); // loads in the InAppBrowser iab.open('https://url-that-fails-whitelist.com', 'random_string'); // loads in the InAppBrowser -iab.open('https://url-that-fails-whitelist.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar +iab.open('https://url-that-fails-whitelist.com', 'random_string', { location: false }); // loads in the InAppBrowser, no location bar ``` diff --git a/types/index.d.ts b/types/index.d.ts index 6c2f95be..99e23824 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -8,6 +8,8 @@ // TypeScript Version: 2.3 type channel = "loadstart" | "loadstop" | "loaderror" | "exit" | "message" | "customscheme" | "beforeload"; +type InAppBrowserOptions = Record<string, string | number | boolean>; + /** * The object returned from a call to cordova.InAppBrowser.open. * NOTE: The InAppBrowser window behaves like a standard web browser, and can't access Cordova APIs. @@ -18,10 +20,12 @@ interface InAppBrowser { * Opens a URL in a new InAppBrowser instance, the current browser instance, or the system browser. * @param url The URL to load. * @param target The target in which to load the URL, an optional parameter that defaults to _self. - * @param options Options for the InAppBrowser. Optional, defaulting to: location=yes. - * The options string must not contain any blank space, and each feature's - * name/value pairs must be separated by a comma. Feature names are case insensitive. + * @param options Options for the InAppBrowser. Optional, defaulting to: { location: true }. */ + open(url: string, target?: string, options?: InAppBrowserOptions): InAppBrowser; + /** + * @deprecated Pass an options object instead. This string format will be removed in a future major release. + */ open(url: string, target?: string, options?: string): InAppBrowser; onloadstart(type: Event): void; diff --git a/www/inappbrowser.js b/www/inappbrowser.js index 5aa3aa90..173d0a0d 100644 --- a/www/inappbrowser.js +++ b/www/inappbrowser.js @@ -95,7 +95,7 @@ } }; - module.exports = function (strUrl, strWindowName, strWindowFeatures, callbacks) { + module.exports = function (strUrl, strWindowName, options, callbacks) { // Don't catch calls that write to existing frames (e.g. named iframes). if (window.frames && window.frames[strWindowName]) { const origOpenFunc = modulemapper.getOriginalSymbol(window, 'open'); @@ -114,9 +114,18 @@ iab._eventHandler(eventname); }; - strWindowFeatures = strWindowFeatures || ''; + if (typeof options === 'object' && options !== null) { + options = Object.keys(options) + .map(function (name) { + const value = options[name]; + return name + '=' + (typeof value === 'boolean' ? (value ? 'yes' : 'no') : value); + }) + .join(','); + } + + options = options || ''; - exec(cb, cb, 'InAppBrowser', 'open', [strUrl, strWindowName, strWindowFeatures]); + exec(cb, cb, 'InAppBrowser', 'open', [strUrl, strWindowName, options]); return iab; }; })(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
