main-blocked.js 5.8 KB

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