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.

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