2013-05-17 04:58:48 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
(function () {
|
2016-01-26 17:06:49 +08:00
|
|
|
// special patch to correctly work on Ripple emulator (CB-9760)
|
|
|
|
if (window.parent && !!window.parent.ripple) { // https://gist.github.com/triceam/4658021
|
|
|
|
module.exports = window.open.bind(window); // fallback to default window.open behaviour
|
|
|
|
return;
|
|
|
|
}
|
2015-10-07 16:44:07 +08:00
|
|
|
|
2016-01-26 17:06:49 +08:00
|
|
|
var exec = require('cordova/exec');
|
|
|
|
var channel = require('cordova/channel');
|
|
|
|
var modulemapper = require('cordova/modulemapper');
|
|
|
|
var urlutil = require('cordova/urlutil');
|
2013-05-17 04:58:48 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
function InAppBrowser () {
|
|
|
|
this.channels = {
|
2018-07-14 04:14:01 +08:00
|
|
|
'beforeload': channel.create('beforeload'),
|
2016-01-26 17:06:49 +08:00
|
|
|
'loadstart': channel.create('loadstart'),
|
2017-06-13 01:34:08 +08:00
|
|
|
'loadstop': channel.create('loadstop'),
|
|
|
|
'loaderror': channel.create('loaderror'),
|
2018-04-06 03:45:02 +08:00
|
|
|
'exit': channel.create('exit'),
|
|
|
|
'customscheme': channel.create('customscheme')
|
2017-06-13 01:34:08 +08:00
|
|
|
};
|
2016-01-26 17:06:49 +08:00
|
|
|
}
|
2013-05-17 04:58:48 +08:00
|
|
|
|
2016-01-26 17:06:49 +08:00
|
|
|
InAppBrowser.prototype = {
|
|
|
|
_eventHandler: function (event) {
|
|
|
|
if (event && (event.type in this.channels)) {
|
2018-07-14 04:14:01 +08:00
|
|
|
if (event.type === 'beforeload') {
|
|
|
|
this.channels[event.type].fire(event, this._loadAfterBeforeload);
|
|
|
|
} else {
|
|
|
|
this.channels[event.type].fire(event);
|
|
|
|
}
|
2016-01-26 17:06:49 +08:00
|
|
|
}
|
|
|
|
},
|
2018-07-14 04:14:01 +08:00
|
|
|
_loadAfterBeforeload: function (strUrl) {
|
|
|
|
strUrl = urlutil.makeAbsolute(strUrl);
|
|
|
|
exec(null, null, 'InAppBrowser', 'loadAfterBeforeload', [strUrl]);
|
|
|
|
},
|
2016-01-26 17:06:49 +08:00
|
|
|
close: function (eventname) {
|
2017-06-13 01:34:08 +08:00
|
|
|
exec(null, null, 'InAppBrowser', 'close', []);
|
2016-01-26 17:06:49 +08:00
|
|
|
},
|
|
|
|
show: function (eventname) {
|
2017-06-13 01:34:08 +08:00
|
|
|
exec(null, null, 'InAppBrowser', 'show', []);
|
2015-02-05 20:20:57 +08:00
|
|
|
},
|
|
|
|
hide: function (eventname) {
|
2017-06-13 01:34:08 +08:00
|
|
|
exec(null, null, 'InAppBrowser', 'hide', []);
|
2016-01-26 17:06:49 +08:00
|
|
|
},
|
2017-06-13 01:34:08 +08:00
|
|
|
addEventListener: function (eventname, f) {
|
2016-01-26 17:06:49 +08:00
|
|
|
if (eventname in this.channels) {
|
|
|
|
this.channels[eventname].subscribe(f);
|
|
|
|
}
|
|
|
|
},
|
2017-06-13 01:34:08 +08:00
|
|
|
removeEventListener: function (eventname, f) {
|
2016-01-26 17:06:49 +08:00
|
|
|
if (eventname in this.channels) {
|
|
|
|
this.channels[eventname].unsubscribe(f);
|
|
|
|
}
|
|
|
|
},
|
2013-05-17 04:58:48 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
executeScript: function (injectDetails, cb) {
|
2016-01-26 17:06:49 +08:00
|
|
|
if (injectDetails.code) {
|
2017-06-13 01:34:08 +08:00
|
|
|
exec(cb, null, 'InAppBrowser', 'injectScriptCode', [injectDetails.code, !!cb]);
|
2016-01-26 17:06:49 +08:00
|
|
|
} else if (injectDetails.file) {
|
2017-06-13 01:34:08 +08:00
|
|
|
exec(cb, null, 'InAppBrowser', 'injectScriptFile', [injectDetails.file, !!cb]);
|
2016-01-26 17:06:49 +08:00
|
|
|
} else {
|
|
|
|
throw new Error('executeScript requires exactly one of code or file to be specified');
|
|
|
|
}
|
|
|
|
},
|
2013-05-17 04:58:48 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
insertCSS: function (injectDetails, cb) {
|
2016-01-26 17:06:49 +08:00
|
|
|
if (injectDetails.code) {
|
2017-06-13 01:34:08 +08:00
|
|
|
exec(cb, null, 'InAppBrowser', 'injectStyleCode', [injectDetails.code, !!cb]);
|
2016-01-26 17:06:49 +08:00
|
|
|
} else if (injectDetails.file) {
|
2017-06-13 01:34:08 +08:00
|
|
|
exec(cb, null, 'InAppBrowser', 'injectStyleFile', [injectDetails.file, !!cb]);
|
2016-01-26 17:06:49 +08:00
|
|
|
} else {
|
|
|
|
throw new Error('insertCSS requires exactly one of code or file to be specified');
|
|
|
|
}
|
2013-05-17 04:58:48 +08:00
|
|
|
}
|
2016-01-26 17:06:49 +08:00
|
|
|
};
|
2013-05-17 04:58:48 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
module.exports = function (strUrl, strWindowName, strWindowFeatures, callbacks) {
|
2016-01-26 17:06:49 +08:00
|
|
|
// 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);
|
|
|
|
}
|
2014-10-15 07:23:26 +08:00
|
|
|
|
2016-01-26 17:06:49 +08:00
|
|
|
strUrl = urlutil.makeAbsolute(strUrl);
|
|
|
|
var iab = new InAppBrowser();
|
2014-10-15 07:23:26 +08:00
|
|
|
|
2016-01-26 17:06:49 +08:00
|
|
|
callbacks = callbacks || {};
|
|
|
|
for (var callbackName in callbacks) {
|
|
|
|
iab.addEventListener(callbackName, callbacks[callbackName]);
|
|
|
|
}
|
2013-10-11 00:21:35 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
var cb = function (eventname) {
|
|
|
|
iab._eventHandler(eventname);
|
2016-01-26 17:06:49 +08:00
|
|
|
};
|
2014-04-10 03:26:47 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
strWindowFeatures = strWindowFeatures || '';
|
2013-05-17 04:58:48 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
exec(cb, cb, 'InAppBrowser', 'open', [strUrl, strWindowName, strWindowFeatures]);
|
2016-01-26 17:06:49 +08:00
|
|
|
return iab;
|
|
|
|
};
|
|
|
|
})();
|