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.

183 lines
5.1 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. /******************************************************************************/
  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. vAPI.shutdown = (function() {
  35. var jobs = [];
  36. var add = function(job) {
  37. jobs.push(job);
  38. };
  39. var exec = function() {
  40. //console.debug('Shutting down...');
  41. var job;
  42. while ( job = jobs.pop() ) {
  43. job();
  44. }
  45. };
  46. return {
  47. add: add,
  48. exec: exec
  49. };
  50. })();
  51. /******************************************************************************/
  52. var messagingConnector = function(response) {
  53. if ( !response ) {
  54. return;
  55. }
  56. var channels = vAPI.messaging.channels;
  57. var channel, listener;
  58. if ( response.broadcast === true && !response.channelName ) {
  59. for ( channel in channels ) {
  60. if ( channels.hasOwnProperty(channel) === false ) {
  61. continue;
  62. }
  63. listener = channels[channel].listener;
  64. if ( typeof listener === 'function' ) {
  65. listener(response.msg);
  66. }
  67. }
  68. return;
  69. }
  70. if ( response.requestId ) {
  71. listener = vAPI.messaging.listeners[response.requestId];
  72. delete vAPI.messaging.listeners[response.requestId];
  73. delete response.requestId;
  74. }
  75. if ( !listener ) {
  76. channel = channels[response.channelName];
  77. listener = channel && channel.listener;
  78. }
  79. if ( typeof listener === 'function' ) {
  80. listener(response.msg);
  81. }
  82. };
  83. /******************************************************************************/
  84. vAPI.messaging = {
  85. port: null,
  86. channels: {},
  87. listeners: {},
  88. requestId: 1,
  89. setup: function() {
  90. this.port = chrome.runtime.connect({name: vAPI.sessionId});
  91. this.port.onMessage.addListener(messagingConnector);
  92. },
  93. close: function() {
  94. if ( this.port === null ) {
  95. return;
  96. }
  97. this.port.disconnect();
  98. this.port.onMessage.removeListener(messagingConnector);
  99. this.port = null;
  100. this.channels = {};
  101. this.listeners = {};
  102. },
  103. channel: function(channelName, callback) {
  104. if ( !channelName ) {
  105. return;
  106. }
  107. this.channels[channelName] = {
  108. channelName: channelName,
  109. listener: typeof callback === 'function' ? callback : null,
  110. send: function(message, callback) {
  111. if ( vAPI.messaging.port === null ) {
  112. vAPI.messaging.setup();
  113. }
  114. message = {
  115. channelName: this.channelName,
  116. msg: message
  117. };
  118. if ( callback ) {
  119. message.requestId = vAPI.messaging.requestId++;
  120. vAPI.messaging.listeners[message.requestId] = callback;
  121. }
  122. vAPI.messaging.port.postMessage(message);
  123. },
  124. close: function() {
  125. delete vAPI.messaging.channels[this.channelName];
  126. if ( Object.keys(vAPI.messaging.channels).length === 0 ) {
  127. vAPI.messaging.close();
  128. }
  129. }
  130. };
  131. return this.channels[channelName];
  132. }
  133. };
  134. // No need to have vAPI client linger around after shutdown if
  135. // we are not a top window (because element picker can still
  136. // be injected in top window).
  137. if ( window !== window.top ) {
  138. vAPI.shutdown.add(function() {
  139. vAPI = null;
  140. });
  141. }
  142. /******************************************************************************/
  143. })(this);
  144. /******************************************************************************/