script.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. document.querySelectorAll('.accordion-header').forEach(button => {
  2. button.addEventListener('click', () => {
  3. const accordionContent = button.nextElementSibling;
  4. button.classList.toggle('active');
  5. if (button.classList.contains('active')) {
  6. accordionContent.style.maxHeight = accordionContent.scrollHeight + 'px';
  7. } else {
  8. accordionContent.style.maxHeight = 0;
  9. }
  10. // Close other open accordion items
  11. document.querySelectorAll('.accordion-header').forEach(otherButton => {
  12. if (otherButton !== button) {
  13. otherButton.classList.remove('active');
  14. otherButton.nextElementSibling.style.maxHeight = 0;
  15. }
  16. });
  17. });
  18. });
  19. // Меню-------------------------
  20. document.addEventListener('DOMContentLoaded', function () {
  21. const mobileMenuButton = document.querySelector('.header__mobile-menu');
  22. const menu = document.querySelector('.header__menu');
  23. const burger = document.querySelector('.burger');
  24. mobileMenuButton.addEventListener('click', function () {
  25. menu.classList.toggle('active');
  26. burger.classList.toggle('active');
  27. });
  28. });
  29. // Redirect
  30. function redirectJs(e) {
  31. e.preventDefault();
  32. let idp = this.getAttribute('data-idp');
  33. let label = this.getAttribute('data-label');
  34. let customUrl = this.getAttribute('data-url');
  35. let page = window.location.href;
  36. let redirectForm = document.createElement('form');
  37. redirectForm.target = '_blank';
  38. redirectForm.method = 'POST';
  39. redirectForm.action = 'redirecting.php';
  40. let redirectInput2 = document.createElement('input');
  41. redirectInput2.type = 'hidden';
  42. redirectInput2.name = 'idpage';
  43. redirectInput2.value = idp;
  44. redirectForm.appendChild(redirectInput2);
  45. let redirectInput3 = document.createElement('input');
  46. redirectInput3.type = 'hidden';
  47. redirectInput3.name = 'page';
  48. redirectInput3.value = page;
  49. redirectForm.appendChild(redirectInput3);
  50. let redirectInput4 = document.createElement('input');
  51. redirectInput4.type = 'hidden';
  52. redirectInput4.name = 'label';
  53. redirectInput4.value = label;
  54. redirectForm.appendChild(redirectInput4);
  55. // Добавляем custom_url если есть
  56. if (customUrl) {
  57. let redirectInput5 = document.createElement('input');
  58. redirectInput5.type = 'hidden';
  59. redirectInput5.name = 'custom_url';
  60. redirectInput5.value = customUrl;
  61. redirectForm.appendChild(redirectInput5);
  62. }
  63. document.body.appendChild(redirectForm);
  64. redirectForm.submit();
  65. }
  66. let redirectButtons = document.querySelectorAll('.redirect-js');
  67. for (let i = 0; i < redirectButtons.length; i++) {
  68. redirectButtons[i].addEventListener('click', redirectJs);
  69. }
  70. document.addEventListener('DOMContentLoaded', function () {
  71. var bLazy = new Blazy({
  72. selector: '.lazy', // Класс для ленивой загрузки
  73. });
  74. });
  75. // Автоматическое оборачивание всех таблиц в table-wrap
  76. (function() {
  77. function wrapTables(container) {
  78. if (!container || !container.querySelectorAll) {
  79. return;
  80. }
  81. const tables = container.querySelectorAll('table:not(.table-wrapped)');
  82. tables.forEach(function(table) {
  83. // Проверяем, не обёрнута ли уже таблица
  84. if (table.parentElement && table.parentElement.classList.contains('table-wrap')) {
  85. table.classList.add('table-wrapped');
  86. return;
  87. }
  88. // Создаём обёртку
  89. const wrapper = document.createElement('div');
  90. wrapper.className = 'table-wrap';
  91. // Вставляем обёртку перед таблицей
  92. table.parentNode.insertBefore(wrapper, table);
  93. // Перемещаем таблицу внутрь обёртки
  94. wrapper.appendChild(table);
  95. // Помечаем таблицу как обёрнутую
  96. table.classList.add('table-wrapped');
  97. });
  98. }
  99. function initTableWrapping() {
  100. // Оборачиваем все существующие таблицы
  101. wrapTables(document.body);
  102. // Повторно проверяем через небольшую задержку (для динамического контента)
  103. setTimeout(function() {
  104. wrapTables(document.body);
  105. }, 500);
  106. setTimeout(function() {
  107. wrapTables(document.body);
  108. }, 1500);
  109. }
  110. // Запускаем при загрузке
  111. if (document.readyState === 'loading') {
  112. document.addEventListener('DOMContentLoaded', initTableWrapping);
  113. } else {
  114. initTableWrapping();
  115. }
  116. // Наблюдаем за изменениями DOM (для динамического контента из TinyMCE)
  117. const observer = new MutationObserver(function(mutations) {
  118. let shouldProcess = false;
  119. mutations.forEach(function(mutation) {
  120. if (mutation.addedNodes.length) {
  121. mutation.addedNodes.forEach(function(node) {
  122. if (node.nodeType === 1) {
  123. // Если добавлена таблица или контейнер с таблицей
  124. if (node.tagName === 'TABLE' || (node.querySelectorAll && node.querySelectorAll('table').length > 0)) {
  125. shouldProcess = true;
  126. }
  127. }
  128. });
  129. }
  130. });
  131. if (shouldProcess) {
  132. wrapTables(document.body);
  133. }
  134. });
  135. // Запускаем наблюдатель
  136. function startObserver() {
  137. if (document.body) {
  138. observer.observe(document.body, {
  139. childList: true,
  140. subtree: true
  141. });
  142. }
  143. }
  144. if (document.body) {
  145. startObserver();
  146. } else {
  147. document.addEventListener('DOMContentLoaded', startObserver);
  148. }
  149. })();
  150. // Автоматическое добавление rel="nofollow noopener" target="_blank" ко всем внешним ссылкам
  151. (function() {
  152. const currentHost = window.location.hostname;
  153. function processExternalLinks(container) {
  154. if (!container || !container.querySelectorAll) {
  155. return;
  156. }
  157. // Находим все ссылки в контейнере
  158. const allLinks = container.querySelectorAll('a[href]');
  159. allLinks.forEach(function(link) {
  160. // Пропускаем уже обработанные ссылки
  161. if (link.hasAttribute('data-external-processed')) {
  162. return;
  163. }
  164. const href = link.getAttribute('href');
  165. // Пропускаем пустые, якорные ссылки, относительные пути и специальные протоколы
  166. if (!href || href.startsWith('#') || href.startsWith('javascript:') ||
  167. href.startsWith('mailto:') || href.startsWith('tel:') || href.startsWith('/')) {
  168. return;
  169. }
  170. try {
  171. // Проверяем, является ли ссылка внешней
  172. let isExternal = false;
  173. // Если ссылка начинается с http:// или https://
  174. if (href.startsWith('http://') || href.startsWith('https://')) {
  175. const linkUrl = new URL(href);
  176. isExternal = (linkUrl.hostname !== currentHost);
  177. }
  178. // Если это внешняя ссылка, добавляем атрибуты
  179. if (isExternal) {
  180. link.setAttribute('rel', 'nofollow noopener');
  181. link.setAttribute('target', '_blank');
  182. link.setAttribute('data-external-processed', 'true');
  183. }
  184. } catch (e) {
  185. // Если возникла ошибка при парсинге URL, игнорируем
  186. console.warn('Unable to parse URL:', href, e);
  187. }
  188. });
  189. }
  190. function initExternalLinks() {
  191. // Обрабатываем все ссылки на странице
  192. processExternalLinks(document.body);
  193. // Повторно обрабатываем через небольшую задержку (для динамического контента)
  194. setTimeout(function() {
  195. processExternalLinks(document.body);
  196. }, 500);
  197. setTimeout(function() {
  198. processExternalLinks(document.body);
  199. }, 1500);
  200. }
  201. // Обрабатываем существующие ссылки при загрузке
  202. if (document.readyState === 'loading') {
  203. document.addEventListener('DOMContentLoaded', initExternalLinks);
  204. } else {
  205. // DOM уже загружен
  206. initExternalLinks();
  207. }
  208. // Наблюдаем за изменениями DOM (для динамического контента, включая TinyMCE)
  209. const observer = new MutationObserver(function(mutations) {
  210. let shouldProcess = false;
  211. mutations.forEach(function(mutation) {
  212. if (mutation.addedNodes.length) {
  213. mutation.addedNodes.forEach(function(node) {
  214. if (node.nodeType === 1) { // Element node
  215. shouldProcess = true;
  216. }
  217. });
  218. }
  219. });
  220. if (shouldProcess) {
  221. processExternalLinks(document.body);
  222. }
  223. });
  224. // Запускаем наблюдатель после загрузки DOM
  225. function startObserver() {
  226. if (document.body) {
  227. observer.observe(document.body, {
  228. childList: true,
  229. subtree: true
  230. });
  231. }
  232. }
  233. if (document.body) {
  234. startObserver();
  235. } else {
  236. document.addEventListener('DOMContentLoaded', startObserver);
  237. }
  238. })();