You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.3 KiB

  1. /*******************************************************************************
  2. µBlock - a Chromium browser extension to block requests.
  3. Copyright (C) 2014 The µBlock authors
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see {http://www.gnu.org/licenses/}.
  14. Home: https://github.com/gorhill/uBlock
  15. */
  16. // For non background pages
  17. /******************************************************************************/
  18. (function() {
  19. 'use strict';
  20. /******************************************************************************/
  21. self.vAPI = self.vAPI || {};
  22. vAPI.firefox = true;
  23. /******************************************************************************/
  24. var messagingConnector = function(response) {
  25. if ( !response ) {
  26. return;
  27. }
  28. var channels = vAPI.messaging.channels;
  29. var channel, listener;
  30. if ( response.broadcast === true ) {
  31. for ( channel in channels ) {
  32. if ( channels.hasOwnProperty(channel) === false ) {
  33. continue;
  34. }
  35. listener = channels[channel].listener;
  36. if ( typeof listener === 'function' ) {
  37. listener(response.msg);
  38. }
  39. }
  40. return;
  41. }
  42. if ( response.requestId ) {
  43. listener = vAPI.messaging.listeners[response.requestId];
  44. delete vAPI.messaging.listeners[response.requestId];
  45. delete response.requestId;
  46. }
  47. if ( !listener ) {
  48. channel = channels[response.portName];
  49. listener = channel && channel.listener;
  50. }
  51. if ( typeof listener === 'function' ) {
  52. listener(response.msg);
  53. }
  54. };
  55. /******************************************************************************/
  56. var uniqueId = function() {
  57. return parseInt(Math.random() * 1e10, 10).toString(36);
  58. };
  59. /******************************************************************************/
  60. vAPI.messaging = {
  61. channels: {},
  62. listeners: {},
  63. requestId: 1,
  64. connectorId: uniqueId(),
  65. bgMessageName: 'µBlock:background',
  66. setup: function() {
  67. this.connector = function(msg) {
  68. messagingConnector(JSON.parse(msg));
  69. };
  70. addMessageListener(this.connectorId, this.connector);
  71. },
  72. close: function() {
  73. if (this.connector) {
  74. removeMessageListener(this.connectorId, this.connector);
  75. this.connector = null;
  76. this.channels = {};
  77. this.listeners = {};
  78. }
  79. },
  80. channel: function(channelName, callback) {
  81. if ( !channelName ) {
  82. return;
  83. }
  84. this.channels[channelName] = {
  85. portName: channelName,
  86. listener: typeof callback === 'function' ? callback : null,
  87. send: function(message, callback) {
  88. if ( !vAPI.messaging.connector ) {
  89. vAPI.messaging.setup();
  90. }
  91. message = {
  92. portName: vAPI.messaging.connectorId + '|' + this.portName,
  93. msg: message
  94. };
  95. if ( callback ) {
  96. message.requestId = vAPI.messaging.requestId++;
  97. vAPI.messaging.listeners[message.requestId] = callback;
  98. }
  99. sendAsyncMessage(vAPI.messaging.bgMessageName, message);
  100. },
  101. close: function() {
  102. delete vAPI.messaging.channels[this.portName];
  103. }
  104. };
  105. return this.channels[channelName];
  106. }
  107. };
  108. /******************************************************************************/
  109. vAPI.canExecuteContentScript = function() {
  110. return true;
  111. };
  112. /******************************************************************************/
  113. })();
  114. /******************************************************************************/