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.

150 lines
4.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  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. /* global self, chrome, vAPI */
  18. /******************************************************************************/
  19. (function() {
  20. 'use strict';
  21. /******************************************************************************/
  22. self.vAPI = self.vAPI || {};
  23. self.vAPI.chrome = true;
  24. /******************************************************************************/
  25. var messagingConnector = function(response) {
  26. if ( !response ) {
  27. return;
  28. }
  29. var channels = vAPI.messaging.channels;
  30. var channel, listener;
  31. if ( response.broadcast === true ) {
  32. for ( channel in channels ) {
  33. if ( channels.hasOwnProperty(channel) === false ) {
  34. continue;
  35. }
  36. listener = channels[channel].listener;
  37. if ( typeof listener === 'function' ) {
  38. listener(response.msg);
  39. }
  40. }
  41. return;
  42. }
  43. if ( response.requestId ) {
  44. listener = vAPI.messaging.listeners[response.requestId];
  45. delete vAPI.messaging.listeners[response.requestId];
  46. delete response.requestId;
  47. }
  48. if ( !listener ) {
  49. channel = channels[response.portName];
  50. listener = channel && channel.listener;
  51. }
  52. if ( typeof listener === 'function' ) {
  53. listener(response.msg);
  54. }
  55. };
  56. /******************************************************************************/
  57. var uniqueId = function() {
  58. return parseInt(Math.random() * 1e10, 10).toString(36);
  59. };
  60. /******************************************************************************/
  61. self.vAPI.messaging = {
  62. port: null,
  63. channels: {},
  64. listeners: {},
  65. requestId: 1,
  66. connectorId: uniqueId(),
  67. setup: function() {
  68. this.port = chrome.runtime.connect({name: this.connectorId});
  69. this.port.onMessage.addListener(messagingConnector);
  70. },
  71. close: function() {
  72. if ( this.port === null ) {
  73. return;
  74. }
  75. this.port.disconnect();
  76. this.port.onMessage.removeListener(messagingConnector);
  77. this.port = null;
  78. this.channels = {};
  79. this.listeners = {};
  80. },
  81. channel: function(channelName, callback) {
  82. if ( !channelName ) {
  83. return;
  84. }
  85. this.channels[channelName] = {
  86. portName: channelName,
  87. listener: typeof callback === 'function' ? callback : null,
  88. send: function(message, callback) {
  89. if ( vAPI.messaging.port === null ) {
  90. vAPI.messaging.setup();
  91. }
  92. message = {
  93. portName: this.portName,
  94. msg: message
  95. };
  96. if ( callback ) {
  97. message.requestId = vAPI.messaging.requestId++;
  98. vAPI.messaging.listeners[message.requestId] = callback;
  99. }
  100. vAPI.messaging.port.postMessage(message);
  101. },
  102. close: function() {
  103. delete vAPI.messaging.channels[this.portName];
  104. }
  105. };
  106. return this.channels[channelName];
  107. }
  108. };
  109. /******************************************************************************/
  110. self.vAPI.canExecuteContentScript = function() {
  111. return true;
  112. };
  113. /******************************************************************************/
  114. })();
  115. /******************************************************************************/