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.

211 lines
5.9 KiB

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