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.

216 lines
6.0 KiB

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. /* jshint esnext: true */
  17. /* global addMessageListener, removeMessageListener, sendAsyncMessage */
  18. // For non background pages
  19. 'use strict';
  20. /******************************************************************************/
  21. (function(self) {
  22. /******************************************************************************/
  23. // https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
  24. if ( self.vAPI === undefined || self.vAPI.uMatrix !== true ) {
  25. self.vAPI = { uMatrix: true };
  26. }
  27. var vAPI = self.vAPI;
  28. vAPI.firefox = true;
  29. vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) +
  30. Math.random().toString(36).slice(2);
  31. /******************************************************************************/
  32. vAPI.setTimeout = vAPI.setTimeout || function(callback, delay) {
  33. return setTimeout(function() { callback(); }, delay);
  34. };
  35. /******************************************************************************/
  36. vAPI.shutdown = (function() {
  37. var jobs = [];
  38. var add = function(job) {
  39. jobs.push(job);
  40. };
  41. var exec = function() {
  42. //console.debug('Shutting down...');
  43. var job;
  44. while ( (job = jobs.pop()) ) {
  45. job();
  46. }
  47. };
  48. return {
  49. add: add,
  50. exec: exec
  51. };
  52. })();
  53. /******************************************************************************/
  54. var messagingConnector = function(response) {
  55. if ( !response ) {
  56. return;
  57. }
  58. var channels = vAPI.messaging.channels;
  59. var channel, listener;
  60. if ( response.broadcast && !response.channelName ) {
  61. for ( channel in channels ) {
  62. if ( channels.hasOwnProperty(channel) === false ) {
  63. continue;
  64. }
  65. listener = channels[channel].listener;
  66. if ( typeof listener === 'function' ) {
  67. listener(response.msg);
  68. }
  69. }
  70. return;
  71. }
  72. if ( response.requestId ) {
  73. listener = vAPI.messaging.listeners[response.requestId];
  74. delete vAPI.messaging.listeners[response.requestId];
  75. delete response.requestId;
  76. }
  77. if ( !listener ) {
  78. channel = channels[response.channelName];
  79. listener = channel && channel.listener;
  80. }
  81. if ( typeof listener === 'function' ) {
  82. listener(response.msg);
  83. }
  84. };
  85. /******************************************************************************/
  86. vAPI.messaging = {
  87. channels: {},
  88. listeners: {},
  89. requestId: 1,
  90. setup: function() {
  91. this.connector = function(msg) {
  92. messagingConnector(JSON.parse(msg));
  93. };
  94. addMessageListener(this.connector);
  95. this.channels.vAPI = {
  96. listener: function(msg) {
  97. if ( typeof msg.cmd === 'string' && msg.cmd === 'injectScript' ) {
  98. var details = msg.details;
  99. if ( !details.allFrames && window !== window.top ) {
  100. return;
  101. }
  102. self.injectScript(details.file);
  103. }
  104. }
  105. };
  106. },
  107. close: function() {
  108. if ( !this.connector ) {
  109. return;
  110. }
  111. removeMessageListener();
  112. this.connector = null;
  113. this.channels = {};
  114. this.listeners = {};
  115. },
  116. channel: function(channelName, callback) {
  117. if ( !channelName ) {
  118. return;
  119. }
  120. this.channels[channelName] = {
  121. channelName: channelName,
  122. listener: typeof callback === 'function' ? callback : null,
  123. send: function(message, callback) {
  124. if ( !vAPI.messaging.connector ) {
  125. vAPI.messaging.setup();
  126. }
  127. message = {
  128. channelName: self._sandboxId_ + '|' + this.channelName,
  129. msg: message
  130. };
  131. if ( callback ) {
  132. message.requestId = vAPI.messaging.requestId++;
  133. vAPI.messaging.listeners[message.requestId] = callback;
  134. }
  135. sendAsyncMessage('umatrix:background', message);
  136. },
  137. close: function() {
  138. delete vAPI.messaging.channels[this.channelName];
  139. }
  140. };
  141. return this.channels[channelName];
  142. },
  143. toggleListener: function({type, persisted}) {
  144. if ( !vAPI.messaging.connector ) {
  145. return;
  146. }
  147. if ( type === 'pagehide' ) {
  148. removeMessageListener();
  149. return;
  150. }
  151. if ( persisted ) {
  152. addMessageListener(vAPI.messaging.connector);
  153. }
  154. }
  155. };
  156. window.addEventListener('pagehide', vAPI.messaging.toggleListener, true);
  157. window.addEventListener('pageshow', vAPI.messaging.toggleListener, true);
  158. /******************************************************************************/
  159. // No need to have vAPI client linger around after shutdown if
  160. // we are not a top window (because element picker can still
  161. // be injected in top window).
  162. if ( window !== window.top ) {
  163. // Can anything be done?
  164. }
  165. /******************************************************************************/
  166. })(this);
  167. /******************************************************************************/