themeablebrowser.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. var exec = require('cordova/exec');
  22. var channel = require('cordova/channel');
  23. var modulemapper = require('cordova/modulemapper');
  24. var urlutil = require('cordova/urlutil');
  25. function ThemeableBrowser() {
  26. this.channels = {};
  27. }
  28. ThemeableBrowser.prototype = {
  29. _eventHandler: function (event) {
  30. if (event && (event.type in this.channels)) {
  31. this.channels[event.type].fire(event);
  32. }
  33. },
  34. close: function (eventname) {
  35. exec(null, null, 'ThemeableBrowser', 'close', []);
  36. return this;
  37. },
  38. show: function (eventname) {
  39. exec(null, null, 'ThemeableBrowser', 'show', []);
  40. return this;
  41. },
  42. reload: function (eventname) {
  43. exec(null, null, 'ThemeableBrowser', 'reload', []);
  44. return this;
  45. },
  46. addEventListener: function (eventname,f) {
  47. if (!(eventname in this.channels)) {
  48. this.channels[eventname] = channel.create(eventname);
  49. }
  50. this.channels[eventname].subscribe(f);
  51. return this;
  52. },
  53. removeEventListener: function(eventname, f) {
  54. if (eventname in this.channels) {
  55. this.channels[eventname].unsubscribe(f);
  56. }
  57. return this;
  58. },
  59. executeScript: function(injectDetails, cb) {
  60. if (injectDetails.code) {
  61. exec(cb, null, 'ThemeableBrowser', 'injectScriptCode', [injectDetails.code, !!cb]);
  62. } else if (injectDetails.file) {
  63. exec(cb, null, 'ThemeableBrowser', 'injectScriptFile', [injectDetails.file, !!cb]);
  64. } else {
  65. throw new Error('executeScript requires exactly one of code or file to be specified');
  66. }
  67. return this;
  68. },
  69. insertCSS: function(injectDetails, cb) {
  70. if (injectDetails.code) {
  71. exec(cb, null, 'ThemeableBrowser', 'injectStyleCode', [injectDetails.code, !!cb]);
  72. } else if (injectDetails.file) {
  73. exec(cb, null, 'ThemeableBrowser', 'injectStyleFile', [injectDetails.file, !!cb]);
  74. } else {
  75. throw new Error('insertCSS requires exactly one of code or file to be specified');
  76. }
  77. return this;
  78. }
  79. };
  80. exports.open = function(strUrl, strWindowName, strWindowFeatures, callbacks) {
  81. // Don't catch calls that write to existing frames (e.g. named iframes).
  82. if (window.frames && window.frames[strWindowName]) {
  83. var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open');
  84. return origOpenFunc.apply(window, arguments);
  85. }
  86. strUrl = urlutil.makeAbsolute(strUrl);
  87. var iab = new ThemeableBrowser();
  88. callbacks = callbacks || {};
  89. for (var callbackName in callbacks) {
  90. iab.addEventListener(callbackName, callbacks[callbackName]);
  91. }
  92. var cb = function(eventname) {
  93. iab._eventHandler(eventname);
  94. };
  95. strWindowFeatures = strWindowFeatures && JSON.stringify(strWindowFeatures);
  96. // Slightly delay the actual native call to give the user a chance to
  97. // register event listeners first, otherwise some warnings or errors may be missed.
  98. setTimeout(function() {
  99. exec(cb, cb, 'ThemeableBrowser', 'open', [strUrl, strWindowName, strWindowFeatures || '']);
  100. }, 0);
  101. return iab;
  102. };
  103. exports.EVT_ERR = 'ThemeableBrowserError';
  104. exports.EVT_WRN = 'ThemeableBrowserWarning';
  105. exports.ERR_CRITICAL = 'critical';
  106. exports.ERR_LOADFAIL = 'loadfail';
  107. exports.WRN_UNEXPECTED = 'unexpected';
  108. exports.WRN_UNDEFINED = 'undefined';