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