janpio closed pull request #213: CB-12468 added ability to set|get default options; URL: https://github.com/apache/cordova-plugin-inappbrowser/pull/213
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/.gitignore b/.gitignore index f63dbbf71..ebfceb4b9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,7 @@ Thumbs.db *.idea node_modules - +.idea/ diff --git a/README.md b/README.md index eade58a37..00140e627 100644 --- a/README.md +++ b/README.md @@ -81,12 +81,12 @@ Report issues with this plugin on the [Apache Cordova issue tracker](https://iss If you want all page loads in your app to go through the InAppBrowser, you can simply hook `window.open` during initialization. For example: - +```javascript document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { window.open = cordova.InAppBrowser.open; } - +``` ## cordova.InAppBrowser.open Opens a URL in a new `InAppBrowser` instance, the current browser @@ -203,6 +203,27 @@ The object returned from a call to `cordova.InAppBrowser.open` when the target i - executeScript - insertCSS +## cordova.InAppBrowser.setDefaultOptions + +> Set default options for all instances of InAppBrowser; + + cordova.InAppBrowser.setDefaultOptions(options); + +## Example +All options that have required attrinbute will be used even if another options will be provided in window.open. +In other cases new and default will merge together + +```javascript + cordova.InAppBrowser.setDefaultOptions('location=no(required),toolbar=yes(required),closebuttoncaption=Close'); +``` + +## cordova.InAppBrowser.setDefaultOptions + +> Get default options for all instances of InAppBrowser; + + cordova.InAppBrowser.getDefaultOptions(); + + ## InAppBrowser.addEventListener > Adds a listener for an event from the `InAppBrowser`. (Only available when > the target is set to `'_blank'`) diff --git a/plugin.xml b/plugin.xml index 4a820ddc4..bdde94a83 100644 --- a/plugin.xml +++ b/plugin.xml @@ -30,15 +30,21 @@ <issue>https://issues.apache.org/jira/browse/CB/component/12320641</issue> <engines> - <engine name="cordova" version=">=3.1.0" /><!-- Needs cordova/urlutil --> + <engine name="cordova" version=">=3.1.0" /><!-- Needs cordova/urlutil --> </engines> <!-- android --> <platform name="android"> <js-module src="www/inappbrowser.js" name="inappbrowser"> + <runs /> + </js-module> + <js-module src="www/open.js" name="open"> <clobbers target="cordova.InAppBrowser.open" /> <clobbers target="window.open" /> </js-module> + <js-module src="www/options.js" name="options"> + <merges target="cordova.InAppBrowser"/> + </js-module> <config-file target="res/xml/config.xml" parent="/*"> <feature name="InAppBrowser"> <param name="android-package" value="org.apache.cordova.inappbrowser.InAppBrowser"/> @@ -70,9 +76,15 @@ <!-- ios --> <platform name="ios"> <js-module src="www/inappbrowser.js" name="inappbrowser"> + <runs /> + </js-module> + <js-module src="www/open.js" name="open"> <clobbers target="cordova.InAppBrowser.open" /> <clobbers target="window.open" /> </js-module> + <js-module src="www/options.js" name="options"> + <merges target="cordova.InAppBrowser"/> + </js-module> <config-file target="config.xml" parent="/*"> <feature name="InAppBrowser"> <param name="ios-package" value="CDVInAppBrowser" /> @@ -80,9 +92,9 @@ </config-file> <header-file src="src/ios/CDVInAppBrowser.h" /> - <source-file src="src/ios/CDVInAppBrowser.m" /> + <source-file src="src/ios/CDVInAppBrowser.m" /> - <framework src="CoreGraphics.framework" /> + <framework src="CoreGraphics.framework" /> </platform> <!-- osx --> @@ -107,6 +119,13 @@ <clobbers target="cordova.InAppBrowser.open" /> <clobbers target="window.open" /> </js-module> + <js-module src="www/open.js" name="open"> + <clobbers target="cordova.InAppBrowser.open" /> + <clobbers target="window.open" /> + </js-module> + <js-module src="www/options.js" name="options"> + <merges target="cordova.InAppBrowser"/> + </js-module> <js-module src="src/windows/InAppBrowserProxy.js" name="InAppBrowserProxy"> <runs /> </js-module> @@ -116,9 +135,15 @@ <!-- browser --> <platform name="browser"> <js-module src="www/inappbrowser.js" name="inappbrowser"> + <runs /> + </js-module> + <js-module src="www/open.js" name="open"> <clobbers target="cordova.InAppBrowser.open" /> <clobbers target="window.open" /> </js-module> + <js-module src="www/options.js" name="options"> + <merges target="cordova.InAppBrowser"/> + </js-module> <js-module src="src/browser/InAppBrowserProxy.js" name="InAppBrowserProxy"> <runs /> </js-module> diff --git a/types/index.d.ts b/types/index.d.ts index 3bac6707b..ea428a0dc 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -25,6 +25,11 @@ interface Window { * NOTE: The InAppBrowser window behaves like a standard web browser, and can't access Cordova APIs. */ interface InAppBrowser extends Window { + + options: string; + setDefaultOptions(opts: string): void; + getDefaultOptions(): string; + onloadstart(type: Event): void; onloadstop(type: InAppBrowserEvent): void; onloaderror(type: InAppBrowserEvent): void; diff --git a/www/inappbrowser.js b/www/inappbrowser.js index 3619f173f..512e03242 100644 --- a/www/inappbrowser.js +++ b/www/inappbrowser.js @@ -28,8 +28,6 @@ var exec = require('cordova/exec'); var channel = require('cordova/channel'); - var modulemapper = require('cordova/modulemapper'); - var urlutil = require('cordova/urlutil'); function InAppBrowser () { this.channels = { @@ -41,6 +39,16 @@ }; } + InAppBrowser.options = ''; + + InAppBrowser.setDefaultOptions = function (str) { + InAppBrowser.options = str || ''; + }; + + InAppBrowser.getDefaultOptions = function () { + return InAppBrowser.options || ''; + }; + InAppBrowser.prototype = { _eventHandler: function (event) { if (event && (event.type in this.channels)) { @@ -88,28 +96,7 @@ } }; - module.exports = function (strUrl, strWindowName, strWindowFeatures, callbacks) { - // Don't catch calls that write to existing frames (e.g. named iframes). - if (window.frames && window.frames[strWindowName]) { - var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open'); - return origOpenFunc.apply(window, arguments); - } - - strUrl = urlutil.makeAbsolute(strUrl); - var iab = new InAppBrowser(); + InAppBrowser.prototype.constructor = InAppBrowser; - callbacks = callbacks || {}; - for (var callbackName in callbacks) { - iab.addEventListener(callbackName, callbacks[callbackName]); - } - - var cb = function (eventname) { - iab._eventHandler(eventname); - }; - - strWindowFeatures = strWindowFeatures || ''; - - exec(cb, cb, 'InAppBrowser', 'open', [strUrl, strWindowName, strWindowFeatures]); - return iab; - }; + module.exports = InAppBrowser; })(); diff --git a/www/open.js b/www/open.js new file mode 100644 index 000000000..7bbf4b795 --- /dev/null +++ b/www/open.js @@ -0,0 +1,61 @@ +/** + * Created by SinceTV on 13.02.17. + */ +var modulemapper = require('cordova/modulemapper'); +var exec = require('cordova/exec'); +var urlutil = require('cordova/urlutil'); + +var InAppBrowser = require('cordova-plugin-inappbrowser.inappbrowser'); + +function getOpts (oldOpts, newOpts) { + var res = ''; + var keys; + var priorityBasedOpts; + + newOpts = newOpts.match(/([^,]+)/g); + + if (newOpts) { + priorityBasedOpts = oldOpts.split(',') + .concat(newOpts) + .reduce(function (acum, elem) { + var splitted = elem.split('='); + if (!acum[splitted[0]] || acum[splitted[0]].indexOf('(required)') === -1) { + acum[splitted[0]] = splitted[1]; + } + return acum; + }, {}); + keys = Object.keys(priorityBasedOpts); + res = keys.map(function (key) { + return key + '=' + priorityBasedOpts[key]; + }).join(',').replace(/\(required\)/g, ''); + } else { + res = oldOpts; + } + return res; +} + +module.exports = function (strUrl, strWindowName, strWindowFeatures, callbacks) { + // Don't catch calls that write to existing frames (e.g. named iframes). + var instance = new InAppBrowser(); + + if (window.frames && window.frames[strWindowName]) { + var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open'); + return origOpenFunc.apply(window, arguments); + } + + strUrl = urlutil.makeAbsolute(strUrl); + + callbacks = callbacks || {}; + for (var callbackName in callbacks) { + instance.addEventListener(callbackName, callbacks[callbackName]); + } + + var cb = function (eventname) { + instance._eventHandler(eventname); + }; + + strWindowFeatures = strWindowFeatures || ''; + + exec(cb, cb, 'InAppBrowser', 'open', [strUrl, strWindowName, getOpts(instance.constructor.getDefaultOptions(), strWindowFeatures)]); + return instance; +}; diff --git a/www/options.js b/www/options.js new file mode 100644 index 000000000..a81fd0551 --- /dev/null +++ b/www/options.js @@ -0,0 +1,13 @@ +/** + * Created by SinceTV on 13.02.17. + */ +var InAppBrowser = require('cordova-plugin-inappbrowser.inappbrowser'); +module.exports = { + setDefaultOptions: function (options) { + InAppBrowser.setDefaultOptions(options); + return InAppBrowser.getDefaultOptions(); + }, + getDefaultOptions: function () { + return InAppBrowser.getDefaultOptions(); + } +}; ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
