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.

160 lines
4.4 KiB

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