vapi-client.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors
  4. Copyright (C) 2019-2022 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uMatrix
  17. */
  18. // For non background pages
  19. 'use strict';
  20. (function (self) {
  21. if (self.vAPI === undefined) {
  22. self.vAPI = {};
  23. }
  24. let vAPI = self.vAPI;
  25. vAPI.setTimeout = vAPI.setTimeout || function (callback, delay, extra) {
  26. return setTimeout(function (a) {
  27. callback(a);
  28. }, delay, extra);
  29. };
  30. vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) +
  31. Math.random().toString(36).slice(2);
  32. vAPI.shutdown = (function () {
  33. let jobs = [];
  34. let add = function (job) {
  35. jobs.push(job);
  36. };
  37. let exec = function () {
  38. //console.debug('Shutting down...');
  39. let job;
  40. while ((job = jobs.pop())) {
  41. job();
  42. }
  43. };
  44. return {
  45. add: add,
  46. exec: exec
  47. };
  48. })();
  49. vAPI.messaging = {
  50. listeners: new Set(),
  51. pending: new Map(),
  52. requestId: 1,
  53. connected: false,
  54. messageListenerCallback: null,
  55. toggleListenerCallback: null,
  56. start: function () {
  57. this.addListener(this.builtinListener);
  58. if (this.toggleListenerCallback === null) {
  59. this.toggleListenerCallback = this.toggleListener.bind(this);
  60. }
  61. window.addEventListener('pagehide',
  62. this.toggleListenerCallback, true);
  63. window.addEventListener('pageshow',
  64. this.toggleListenerCallback, true);
  65. },
  66. shutdown: function () {
  67. if (this.toggleListenerCallback !== null) {
  68. window.removeEventListener('pagehide',
  69. this.toggleListenerCallback, true);
  70. window.removeEventListener('pageshow',
  71. this.toggleListenerCallback, true);
  72. }
  73. this.removeAllListeners();
  74. //service pending callbacks
  75. var pending = this.pending;
  76. this.pending.clear();
  77. for (let callback of pending.values()) {
  78. if (typeof callback === 'function') {
  79. callback(null);
  80. }
  81. }
  82. },
  83. connect: function () {
  84. if (!this.connected) {
  85. if (this.messageListenerCallback === null) {
  86. this.messageListenerCallback =
  87. this.messageListener.bind(this);
  88. }
  89. addMessageListener(this.messageListenerCallback);
  90. this.connected = true;
  91. }
  92. },
  93. disconnect: function () {
  94. if (this.connected) {
  95. removeMessageListener();
  96. this.connected = false;
  97. }
  98. },
  99. messageListener: function (msg) {
  100. let details = JSON.parse(msg);
  101. if (!details) {
  102. return;
  103. }
  104. if (details.broadcast) {
  105. this.sendToListeners(details.msg);
  106. return;
  107. }
  108. if (details.requestId) {
  109. let listener = this.pending.get(details.requestId);
  110. if (listener !== undefined) {
  111. this.pending.delete(details.requestId);
  112. listener(details.msg);
  113. return;
  114. }
  115. }
  116. },
  117. builtinListener: function (msg) {
  118. if (typeof msg.cmd === 'string' && msg.cmd === 'injectScript') {
  119. let details = msg.details;
  120. if (!details.allFrames && window !== window.top) {
  121. return;
  122. }
  123. self.injectScript(details.file);
  124. }
  125. },
  126. send: function (channelName, message, callback) {
  127. this.connect()
  128. message = {
  129. channelName: self._sandboxId_ + '|' + channelName,
  130. msg: message
  131. };
  132. if (callback) {
  133. message.requestId = this.requestId++;
  134. this.pending.set(message.requestId, callback);
  135. }
  136. sendAsyncMessage('ematrix:background', message);
  137. },
  138. toggleListener: function ({type, persisted}) {
  139. if (type === 'pagehide' && !persisted) {
  140. vAPI.shutdown.exec();
  141. this.shutdown();
  142. return;
  143. }
  144. if (type === 'pagehide') {
  145. this.disconnect();
  146. } else {
  147. this.connect();
  148. }
  149. },
  150. sendToListeners: function (msg) {
  151. for (let listener of this.listeners) {
  152. listener(msg);
  153. }
  154. },
  155. addListener: function (listener) {
  156. this.listeners.add(listener);
  157. this.connect()
  158. },
  159. removeListener: function (listener) {
  160. this.listeners.delete(listener);
  161. },
  162. removeAllListeners: function () {
  163. this.disconnect();
  164. this.listeners.clear();
  165. }
  166. };
  167. vAPI.messaging.start()
  168. // No need to have vAPI client linger around after shutdown if
  169. // we are not a top window (because element picker can still
  170. // be injected in top window).
  171. // Needs more investigating
  172. // if ( window !== window.top ) {
  173. // vAPI.shutdown.add(function() {
  174. // vAPI = null;
  175. // });
  176. // }
  177. })(this);