inappbrowser.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. (function () {
  22. // special patch to correctly work on Ripple emulator (CB-9760)
  23. if (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. var exec = require('cordova/exec');
  28. var channel = require('cordova/channel');
  29. var modulemapper = require('cordova/modulemapper');
  30. var urlutil = require('cordova/urlutil');
  31. function InAppBrowser () {
  32. this.channels = {
  33. 'loadstart': channel.create('loadstart'),
  34. 'loadstop': channel.create('loadstop'),
  35. 'loaderror': channel.create('loaderror'),
  36. 'exit': channel.create('exit'),
  37. 'customscheme': channel.create('customscheme')
  38. };
  39. }
  40. InAppBrowser.prototype = {
  41. _eventHandler: function (event) {
  42. if (event && (event.type in this.channels)) {
  43. this.channels[event.type].fire(event);
  44. }
  45. },
  46. close: function (eventname) {
  47. exec(null, null, 'InAppBrowser', 'close', []);
  48. },
  49. show: function (eventname) {
  50. exec(null, null, 'InAppBrowser', 'show', []);
  51. },
  52. hide: function (eventname) {
  53. exec(null, null, 'InAppBrowser', 'hide', []);
  54. },
  55. addEventListener: function (eventname, f) {
  56. if (eventname in this.channels) {
  57. this.channels[eventname].subscribe(f);
  58. }
  59. },
  60. removeEventListener: function (eventname, f) {
  61. if (eventname in this.channels) {
  62. this.channels[eventname].unsubscribe(f);
  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. insertCSS: function (injectDetails, cb) {
  75. if (injectDetails.code) {
  76. exec(cb, null, 'InAppBrowser', 'injectStyleCode', [injectDetails.code, !!cb]);
  77. } else if (injectDetails.file) {
  78. exec(cb, null, 'InAppBrowser', 'injectStyleFile', [injectDetails.file, !!cb]);
  79. } else {
  80. throw new Error('insertCSS requires exactly one of code or file to be specified');
  81. }
  82. }
  83. };
  84. module.exports = function (strUrl, strWindowName, strWindowFeatures, callbacks) {
  85. // Don't catch calls that write to existing frames (e.g. named iframes).
  86. if (window.frames && window.frames[strWindowName]) {
  87. var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open');
  88. return origOpenFunc.apply(window, arguments);
  89. }
  90. strUrl = urlutil.makeAbsolute(strUrl);
  91. var iab = new InAppBrowser();
  92. callbacks = callbacks || {};
  93. for (var callbackName in callbacks) {
  94. iab.addEventListener(callbackName, callbacks[callbackName]);
  95. }
  96. var cb = function (eventname) {
  97. iab._eventHandler(eventname);
  98. };
  99. strWindowFeatures = strWindowFeatures || '';
  100. exec(cb, cb, 'InAppBrowser', 'open', [strUrl, strWindowName, strWindowFeatures]);
  101. return iab;
  102. };
  103. })();