UNPKG

3.88 kBJavaScriptView Raw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22// special patch to correctly work on Ripple emulator (CB-9760)
23if (window.parent && !!window.parent.ripple) { // https://gist.github.com/triceam/4658021
24 module.exports = window.open.bind(window); // fallback to default window.open behaviour
25 return;
26}
27
28var exec = require('cordova/exec');
29var channel = require('cordova/channel');
30var modulemapper = require('cordova/modulemapper');
31var urlutil = require('cordova/urlutil');
32
33function InAppBrowser() {
34 this.channels = {
35 'loadstart': channel.create('loadstart'),
36 'loadstop' : channel.create('loadstop'),
37 'loaderror' : channel.create('loaderror'),
38 'exit' : channel.create('exit')
39 };
40}
41
42InAppBrowser.prototype = {
43 _eventHandler: function (event) {
44 if (event && (event.type in this.channels)) {
45 this.channels[event.type].fire(event);
46 }
47 },
48 close: function (eventname) {
49 exec(null, null, "InAppBrowser", "close", []);
50 },
51 show: function (eventname) {
52 exec(null, null, "InAppBrowser", "show", []);
53 },
54 addEventListener: function (eventname,f) {
55 if (eventname in this.channels) {
56 this.channels[eventname].subscribe(f);
57 }
58 },
59 removeEventListener: function(eventname, f) {
60 if (eventname in this.channels) {
61 this.channels[eventname].unsubscribe(f);
62 }
63 },
64
65 executeScript: function(injectDetails, cb) {
66 if (injectDetails.code) {
67 exec(cb, null, "InAppBrowser", "injectScriptCode", [injectDetails.code, !!cb]);
68 } else if (injectDetails.file) {
69 exec(cb, null, "InAppBrowser", "injectScriptFile", [injectDetails.file, !!cb]);
70 } else {
71 throw new Error('executeScript requires exactly one of code or file to be specified');
72 }
73 },
74
75 insertCSS: function(injectDetails, cb) {
76 if (injectDetails.code) {
77 exec(cb, null, "InAppBrowser", "injectStyleCode", [injectDetails.code, !!cb]);
78 } else if (injectDetails.file) {
79 exec(cb, null, "InAppBrowser", "injectStyleFile", [injectDetails.file, !!cb]);
80 } else {
81 throw new Error('insertCSS requires exactly one of code or file to be specified');
82 }
83 }
84};
85
86module.exports = function(strUrl, strWindowName, strWindowFeatures, callbacks) {
87 // Don't catch calls that write to existing frames (e.g. named iframes).
88 if (window.frames && window.frames[strWindowName]) {
89 var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open');
90 return origOpenFunc.apply(window, arguments);
91 }
92
93 strUrl = urlutil.makeAbsolute(strUrl);
94 var iab = new InAppBrowser();
95
96 callbacks = callbacks || {};
97 for (var callbackName in callbacks) {
98 iab.addEventListener(callbackName, callbacks[callbackName]);
99 }
100
101 var cb = function(eventname) {
102 iab._eventHandler(eventname);
103 };
104
105 strWindowFeatures = strWindowFeatures || "";
106
107 exec(cb, cb, "InAppBrowser", "open", [strUrl, strWindowName, strWindowFeatures]);
108 return iab;
109};
110