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.

196 lines
5.5 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
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a browser extension to block requests.
  3. Copyright (C) 2014-2017 The uMatrix/uBlock Origin 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/uMatrix
  15. */
  16. // For non background pages
  17. 'use strict';
  18. /******************************************************************************/
  19. (function(self) {
  20. /******************************************************************************/
  21. // https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
  22. if ( self.vAPI === undefined || self.vAPI.uMatrix !== true ) {
  23. self.vAPI = { uMatrix: true };
  24. }
  25. var vAPI = self.vAPI;
  26. var chrome = self.chrome;
  27. // https://github.com/chrisaljoudi/uBlock/issues/456
  28. // Already injected?
  29. if ( vAPI.vapiClientInjected ) {
  30. //console.debug('vapi-client.js already injected: skipping.');
  31. return;
  32. }
  33. vAPI.vapiClientInjected = true;
  34. vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) +
  35. Math.random().toString(36).slice(2);
  36. vAPI.chrome = true;
  37. /******************************************************************************/
  38. vAPI.shutdown = (function() {
  39. var jobs = [];
  40. var add = function(job) {
  41. jobs.push(job);
  42. };
  43. var exec = function() {
  44. //console.debug('Shutting down...');
  45. var job;
  46. while ( (job = jobs.pop()) ) {
  47. job();
  48. }
  49. };
  50. return {
  51. add: add,
  52. exec: exec
  53. };
  54. })();
  55. /******************************************************************************/
  56. var messagingConnector = function(response) {
  57. if ( !response ) {
  58. return;
  59. }
  60. var channels = vAPI.messaging.channels;
  61. var channel, listener;
  62. if ( response.broadcast === true && !response.channelName ) {
  63. for ( channel in channels ) {
  64. if ( channels.hasOwnProperty(channel) === false ) {
  65. continue;
  66. }
  67. listener = channels[channel].listener;
  68. if ( typeof listener === 'function' ) {
  69. listener(response.msg);
  70. }
  71. }
  72. return;
  73. }
  74. if ( response.requestId ) {
  75. listener = vAPI.messaging.listeners[response.requestId];
  76. delete vAPI.messaging.listeners[response.requestId];
  77. delete response.requestId;
  78. }
  79. if ( !listener ) {
  80. channel = channels[response.channelName];
  81. listener = channel && channel.listener;
  82. }
  83. if ( typeof listener === 'function' ) {
  84. listener(response.msg);
  85. }
  86. };
  87. /******************************************************************************/
  88. vAPI.messaging = {
  89. port: null,
  90. channels: {},
  91. listeners: {},
  92. requestId: 1,
  93. setup: function() {
  94. this.port = chrome.runtime.connect({name: vAPI.sessionId});
  95. this.port.onMessage.addListener(messagingConnector);
  96. },
  97. close: function() {
  98. if ( this.port === null ) {
  99. return;
  100. }
  101. this.port.disconnect();
  102. this.port.onMessage.removeListener(messagingConnector);
  103. this.port = null;
  104. this.channels = {};
  105. this.listeners = {};
  106. },
  107. channel: function(channelName, callback) {
  108. if ( !channelName ) {
  109. return;
  110. }
  111. this.channels[channelName] = {
  112. channelName: channelName,
  113. listener: typeof callback === 'function' ? callback : null,
  114. send: function(message, callback) {
  115. if ( vAPI.messaging.port === null ) {
  116. vAPI.messaging.setup();
  117. }
  118. message = {
  119. channelName: this.channelName,
  120. msg: message
  121. };
  122. if ( callback ) {
  123. message.requestId = vAPI.messaging.requestId++;
  124. vAPI.messaging.listeners[message.requestId] = callback;
  125. }
  126. vAPI.messaging.port.postMessage(message);
  127. },
  128. close: function() {
  129. delete vAPI.messaging.channels[this.channelName];
  130. if ( Object.keys(vAPI.messaging.channels).length === 0 ) {
  131. vAPI.messaging.close();
  132. }
  133. }
  134. };
  135. return this.channels[channelName];
  136. }
  137. };
  138. /******************************************************************************/
  139. // No need to have vAPI client linger around after shutdown if
  140. // we are not a top window (because element picker can still
  141. // be injected in top window).
  142. if ( window !== window.top ) {
  143. vAPI.shutdown.add(function() {
  144. vAPI = null;
  145. });
  146. }
  147. /******************************************************************************/
  148. vAPI.setTimeout = vAPI.setTimeout || function(callback, delay) {
  149. setTimeout(function() { callback(); }, delay);
  150. };
  151. /******************************************************************************/
  152. })(this);
  153. /******************************************************************************/