main-blocked.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2015-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/uBlock
  17. */
  18. /* global uDom */
  19. 'use strict';
  20. (function () {
  21. let details = {};
  22. (function () {
  23. let matches = /details=([^&]+)/.exec(window.location.search);
  24. if (matches === null) {
  25. return;
  26. }
  27. try {
  28. details = JSON.parse(atob(matches[1]));
  29. } catch(ex) {
  30. }
  31. })();
  32. uDom('.what').text(details.url);
  33. // uDom('#why').text(details.why.slice(3));
  34. // https://github.com/gorhill/uMatrix/issues/502
  35. // Code below originally imported from:
  36. // https://github.com/gorhill/uBlock/blob/master/src/js/document-blocked.js
  37. (function () {
  38. if (typeof URL !== 'function') {
  39. return;
  40. }
  41. const reURL = /^https?:\/\//;
  42. const liFromParam = function (name, value) {
  43. if (value === '') {
  44. value = name;
  45. name = '';
  46. }
  47. let li = document.createElement('li');
  48. let span = document.createElement('span');
  49. span.textContent = name;
  50. li.appendChild(span);
  51. if (name !== '' && value !== '') {
  52. li.appendChild(document.createTextNode(' = '));
  53. }
  54. span = document.createElement('span');
  55. if (reURL.test(value)) {
  56. let a = document.createElement('a');
  57. a.href = a.textContent = value;
  58. span.appendChild(a);
  59. } else {
  60. span.textContent = value;
  61. }
  62. li.appendChild(span);
  63. return li;
  64. };
  65. const safeDecodeURIComponent = function (s) {
  66. try {
  67. s = decodeURIComponent(s);
  68. } catch (ex) {
  69. }
  70. return s;
  71. };
  72. const renderParams = function (parentNode, rawURL, step) {
  73. if (0 === step) {
  74. // The URL is too nested, bail out (successfully) to
  75. // avoid denial of service attacks.
  76. return true;
  77. }
  78. let a = document.createElement('a');
  79. a.href = rawURL;
  80. if (a.search.length === 0) {
  81. return false;
  82. }
  83. let pos = rawURL.indexOf('?');
  84. let li = liFromParam(vAPI.i18n('docblockedNoParamsPrompt'),
  85. rawURL.slice(0, pos));
  86. parentNode.appendChild(li);
  87. let params = a.search.slice(1).split('&');
  88. for (let i=0; i<params.length; ++i) {
  89. let param = params[i];
  90. pos = param.indexOf('=');
  91. if (pos === -1) {
  92. pos = param.length;
  93. }
  94. let name = safeDecodeURIComponent(param.slice(0, pos));
  95. let value = safeDecodeURIComponent(param.slice(pos + 1));
  96. li = liFromParam(name, value);
  97. if (reURL.test(value)) {
  98. let ul = document.createElement('ul');
  99. renderParams(ul, value, step - 1);
  100. li.appendChild(ul);
  101. }
  102. parentNode.appendChild(li);
  103. }
  104. return true;
  105. };
  106. // The number of steps is arbitrary, but there's no point in
  107. // making it to large.
  108. if (renderParams(uDom.nodeFromId('parsed'), details.url, 3) === false) {
  109. return;
  110. }
  111. let toggler = document.createElement('span');
  112. toggler.className = 'fa';
  113. uDom('#theURL > p').append(toggler);
  114. uDom(toggler).on('click', function () {
  115. let collapsed = uDom
  116. .nodeFromId('theURL')
  117. .classList
  118. .toggle('collapsed');
  119. vAPI.localStorage.setItem('document-blocked-collapse-url',
  120. collapsed.toString());
  121. });
  122. let p = vAPI.localStorage.getItem('document-blocked-collapse-url');
  123. uDom.nodeFromId('theURL').classList.toggle('collapsed', p === 'true');
  124. })();
  125. if (window.history.length > 1) {
  126. uDom('#back').on('click', function () {
  127. window.history.back();
  128. });
  129. uDom('#bye').css('display', 'none');
  130. } else {
  131. uDom('#bye').on('click', function () {
  132. window.close();
  133. });
  134. uDom('#back').css('display', 'none');
  135. }
  136. // See if the target hostname is still blacklisted, and if not,
  137. // navigate to it.
  138. vAPI.messaging.send('main-blocked.js', {
  139. what: 'mustBlock',
  140. scope: details.hn,
  141. hostname: details.hn,
  142. type: 'doc'
  143. }, function (response) {
  144. if (response === false) {
  145. window.location.replace(details.url);
  146. }
  147. });
  148. })();