vapi-core.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 'use strict';
  19. /******************************************************************************/
  20. (function (self) {
  21. vAPI.modernFirefox =
  22. Services.appinfo.ID === '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}'
  23. && Services.vc.compare(Services.appinfo.version, '44') > 0;
  24. vAPI.app = {
  25. name: 'eMatrix',
  26. version: location.hash.slice(1),
  27. start: function () {
  28. return;
  29. },
  30. stop: function () {
  31. return;
  32. },
  33. restart: function () {
  34. Cc['@mozilla.org/childprocessmessagemanager;1']
  35. .getService(Ci.nsIMessageSender)
  36. .sendAsyncMessage(location.host + '-restart');
  37. },
  38. };
  39. // List of things that needs to be destroyed when disabling the extension
  40. // Only functions should be added to it
  41. // eMatrix: taken care by vAPI.addCleanUpTask --- use that function
  42. let cleanupTasks = [];
  43. // This must be updated manually, every time a new task is added/removed
  44. // eMatrix: do we?
  45. let expectedNumberOfCleanups = 7;
  46. vAPI.addCleanUpTask = function (task) {
  47. if (typeof task !== 'function') {
  48. return;
  49. }
  50. cleanupTasks.push(task);
  51. };
  52. vAPI.deferUntil = function (testFn, mainFn, details) {
  53. let dtls = (typeof details !== 'object') ? {} : details;
  54. let now = 0;
  55. let next = dtls.next || 200;
  56. let until = dtls.until || 2000;
  57. let check = function () {
  58. if (testFn() === true || now >= until) {
  59. mainFn();
  60. return;
  61. }
  62. now += next;
  63. vAPI.setTimeout(check, next);
  64. };
  65. if ('sync' in dtls && dtls.sync === true) {
  66. check();
  67. } else {
  68. vAPI.setTimeout(check, 1);
  69. }
  70. };
  71. window.addEventListener('unload', function () {
  72. // if (typeof vAPI.app.onShutdown === 'function') {
  73. // vAPI.app.onShutdown();
  74. // }
  75. // IMPORTANT: cleanup tasks must be executed using LIFO order.
  76. for (let i=cleanupTasks.length-1; i>=0; --i) {
  77. try {
  78. cleanupTasks[i]();
  79. } catch (e) {
  80. // Just in case a clean up task ends up throwing for
  81. // no reason
  82. console.error(e);
  83. }
  84. }
  85. // eMatrix: temporarily disabled
  86. // if (cleanupTasks.length < expectedNumberOfCleanups) {
  87. // console.error
  88. // ('eMatrix> Cleanup tasks performed: %s (out of %s)',
  89. // cleanupTasks.length,
  90. // expectedNumberOfCleanups);
  91. // }
  92. // frameModule needs to be cleared too
  93. Cu.import('chrome://ematrix/content/lib/FrameModule.jsm');
  94. contentObserver.unregister();
  95. Cu.unload('chrome://ematrix/content/lib/FrameModule.jsm');
  96. });
  97. vAPI.noTabId = '-1';
  98. vAPI.isBehindTheSceneTabId = function (tabId) {
  99. return tabId.toString() === '-1';
  100. };
  101. vAPI.lastError = function () {
  102. return null;
  103. };
  104. })(this);