Igor150195

Про-допработы

Mar 11th, 2020
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. (function(){
  4.     let configurator = document.getElementById('configurator_json');
  5.     let html = configurator.innerHTML;
  6.     let json = JSON.parse(html);
  7.     let all_pro_addons = 'all_pro_addons';
  8.     let pro_addons_array = [];
  9.  
  10.     if (location.search.match(/gr_settings/) && getCookie(all_pro_addons)=="") {
  11.         location.reload();
  12.     };
  13.    
  14.     if (location.search.match(/gr_full_version/)) {
  15.         let href = location.search.split("?&gr_full_version");
  16.        
  17.         setCookie('gr_full_version', 1, 7);
  18.         location.href = location.origin + href[0];
  19.     }; /*Ссылка версии со всеми допработами*/
  20.    
  21.     if (location.search.match(/gr_base_version/)) {
  22.         let href = location.search.split("?&gr_base_version");
  23.        
  24.         if (getCookie(all_pro_addons)!="") {
  25.             let json_cookie = getCookie(all_pro_addons);
  26.             let json_array = JSON.parse(json_cookie);
  27.            
  28.             for (let i = json_array.length - 1; i >= 0; i--) {
  29.                 deleteCookie(json_array[i]);
  30.             };
  31.         };
  32.        
  33.         deleteCookie('full_price');
  34.         deleteCookie('gr_full_version');
  35.         deleteCookie('all_pro_addons');
  36.        
  37.         if (location.search.match(/gr_base_version/)) {
  38.             location.href = location.origin + href[0];
  39.         };
  40.     }; /*Ссылка базовой версии без допработ*/
  41.    
  42.     if (getCookie(all_pro_addons)!="" && location.search.match(/gr_settings/)) {
  43.         let json_cookie = getCookie(all_pro_addons);
  44.         let json_array = JSON.parse(json_cookie);
  45.         let href = location.search.split("?&gr_settings=1");
  46.        
  47.         if (location.search.match(/gr_settings/)) {
  48.             for (let i = json_array.length - 1; i >= 0; i--) {
  49.                 deleteCookie(json_array[i]);
  50.             };
  51.         };
  52.        
  53.         deleteCookie('gr_full_version');
  54.        
  55.         if (location.search.match(/gr_settings/)) {
  56.             location.href = location.origin + href[0];
  57.         };
  58.     }; /*Ссылка с базовой версии с допработами*/
  59.    
  60.     getSettingsParse(json);
  61.  
  62.     function getSettingsParse(json) {
  63.         for (var key in json) {
  64.             let result = key.match(/pro_/);
  65.             let full_price = key.match(/full_price/);
  66.            
  67.             if (full_price) {
  68.                 let full_price_value = json[full_price];
  69.                 setCookie(full_price, full_price_value, 7);
  70.             };
  71.  
  72.             if (result) {
  73.                 pro_addons_array.push(key);
  74.                 setCookie(key, 1, 7);
  75.             };
  76.         };
  77.        
  78.         if (location.search.match(/gr_settings/)) {
  79.             let json_str = JSON.stringify(pro_addons_array);
  80.             setCookie(all_pro_addons, json_str, 7);
  81.         };
  82.     }; /*Запись допработ в куки*/
  83.    
  84.     function ready() {
  85.        
  86.             let target = document.querySelector('body');
  87.             let observer = new MutationObserver(function(mutations) {
  88.                 mutations.forEach(function(mutation) {
  89.                     if (document.querySelectorAll('.s3solution-panel-root').length>0) {
  90.                        
  91.                         if (getCookie('full_price') != "") {
  92.                             document.querySelector('.s3solution__item.s3solution__item--price strong').innerHTML = getCookie('full_price');
  93.                         };
  94.                        
  95.                         let site_id = configurator.getAttribute('data-site-id');
  96.                         let catalog_href = 'https://design.megagroup.ru/solution/' + site_id;
  97.                         let btn = '<a href="'+catalog_href+'" class="s3solution-panel__btn s3solution-panel__btn--order">Заказать сайт<div class="s3solution-panel-tooltip js-panel-tooltip">ID = '+site_id+'</div></a>';
  98.                        
  99.                         if (document.querySelectorAll('a.s3solution-panel__btn.s3solution-panel__btn--order').length<1) {
  100.                             document.querySelector('.s3solution-panel__btn.s3solution-panel__btn--order').insertAdjacentHTML('afterend', btn); 
  101.                         };
  102.                         if (document.querySelectorAll('div.s3solution-panel__btn.s3solution-panel__btn--order').length>0) {
  103.                             document.querySelector('div.s3solution-panel__btn.s3solution-panel__btn--order').remove(); 
  104.                         };
  105.  
  106.                         observer.disconnect();
  107.                     }
  108.                 });    
  109.             });
  110.             let config = {
  111.                 attributes: true,
  112.                 childList: true,
  113.                 characterData: true
  114.             };
  115.            
  116.             observer.observe(target, config);
  117.        
  118.     }; /*Отслеживание желтой панели и замена в ней цены с кнопкой*/
  119.    
  120.     document.addEventListener("DOMContentLoaded", ready);
  121.    
  122.    
  123.     function setCookie(name, value, days = 7, path = '/') {
  124.         const expires = new Date(Date.now() + days * 864e5).toUTCString()
  125.         document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=' + path
  126.     };
  127.    
  128.     function getCookie(name) {
  129.         return document.cookie.split('; ').reduce((r, v) => {
  130.             const parts = v.split('=')
  131.             return parts[0] === name ? decodeURIComponent(parts[1]) : r
  132.         }, '')
  133.     };
  134.    
  135.     function deleteCookie(name, path) {
  136.         setCookie(name, '', -1, path)
  137.     };
  138. })();
Add Comment
Please, Sign In to add comment