1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | if (window.parent && !!window.parent.ripple) {
|
24 | module.exports = window.open.bind(window);
|
25 | return;
|
26 | }
|
27 |
|
28 | var exec = require('cordova/exec');
|
29 | var channel = require('cordova/channel');
|
30 | var modulemapper = require('cordova/modulemapper');
|
31 | var urlutil = require('cordova/urlutil');
|
32 |
|
33 | function 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 |
|
42 | InAppBrowser.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 |
|
86 | module.exports = function(strUrl, strWindowName, strWindowFeatures, callbacks) {
|
87 |
|
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 |
|