utils.js 3.3 KB

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