Tools.jsm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 Raymond Hill
  4. Copyright (C) 2019-2020-2021 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. var EXPORTED_SYMBOLS = ['Tools'];
  20. var LineIterator = function (text, offset) {
  21. this.text = text;
  22. this.textLen = this.text.length;
  23. this.offset = offset || 0;
  24. };
  25. LineIterator.prototype = {
  26. next: function () {
  27. let end = this.text.indexOf('\n', this.offset);
  28. if (end === -1) {
  29. end = this.text.indexOf('\r', this.offset);
  30. if (end === -1) {
  31. end = this.textLen;
  32. }
  33. }
  34. let line = this.text.slice(this.offset, end);
  35. this.offset = end + 1;
  36. return line;
  37. },
  38. rewind: function () {
  39. if (this.offset <= 1) {
  40. this.offset = 0;
  41. }
  42. let end = this.text.lastIndexOf('\n', this.offset - 2);
  43. if (end === -1) {
  44. this.offset = end + 1;
  45. } else {
  46. end = this.text.lastIndexOf('\r', this.offset - 2);
  47. this.offset = end !== -1 ? end + 1 : 0;
  48. }
  49. },
  50. eot: function () {
  51. return this.offset >= this.textLen;
  52. },
  53. };
  54. var setToArray;
  55. if (typeof Array.from === 'function') {
  56. setToArray = Array.from;
  57. } else {
  58. setToArray = function (set) {
  59. let out = [];
  60. let entries = set.values();
  61. for (let entry=entries.next();
  62. !entry||!entry.done; entry=entries.next()) {
  63. out.push(entry.value);
  64. }
  65. return out;
  66. };
  67. }
  68. var Tools = {
  69. LineIterator: LineIterator,
  70. setToArray: setToArray,
  71. gotoURL: function (context) {
  72. if (typeof context !== 'object') {
  73. throw new TypeError(context);
  74. }
  75. context.api.tabs.open(context.details);
  76. },
  77. gotoExtensionURL: function (context) {
  78. if (typeof context !== 'object') {
  79. throw new TypeError(context);
  80. }
  81. let details = context.details;
  82. let api = context.api;
  83. let matrix = context.matrix;
  84. if (details.url.startsWith('logger-ui.html')) {
  85. if (details.shiftKey) {
  86. let setting = matrix.userSettings.alwaysDetachLogger;
  87. matrix.changeUserSettings('alwaysDetachLogger', !setting);
  88. }
  89. details.popup = matrix.userSettings.alwaysDetachLogger;
  90. }
  91. details.select = true;
  92. api.tabs.open(details);
  93. },
  94. setFromArray: function (a) {
  95. return new Set(a);
  96. },
  97. };