Advertisement
Guest User

Untitled

a guest
Nov 13th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.wpsr_helpers = {
  2.     addClass: function( ele, className ){
  3.         if ( ele.classList )
  4.           ele.classList.add( className );
  5.         else
  6.           ele.className += ' ' + className;
  7.     },
  8.    
  9.     removeClass: function( ele, className ){
  10.         if (ele.classList)
  11.             ele.classList.remove(className);
  12.         else
  13.             ele.className = ele.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
  14.     },
  15.    
  16.     popup_window: function( url, target, w, h ){
  17.         var left = ( screen.width/2 )-( w/2 );
  18.         var top = ( screen.height/2 )-( h/2 );
  19.         return window.open( url, target, 'toolbar=no,location=no,menubar=no,scrollbars=yes,width='+w+',height='+h+',top='+top+',left='+left );
  20.     },
  21.    
  22.     offset: function( el ){
  23.         var rect = el.getBoundingClientRect();
  24.         return {
  25.             top: rect.top + document.body.scrollTop,
  26.             left: rect.left + document.body.scrollLeft
  27.         }
  28.     },
  29.  
  30.     ajax: function( ajax_url, method, send, callback, props ){
  31.        
  32.         var request = new XMLHttpRequest();
  33.         request.props = props;
  34.         request.open( method, ajax_url, true );
  35.        
  36.         if ( method == 'POST' )
  37.             request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' );
  38.        
  39.         request.onreadystatechange = function(){
  40.             if ( request.readyState == 4 && request.status == 200 ){
  41.                 return callback( request );
  42.             }
  43.         };
  44.        
  45.         request.send( send );
  46.        
  47.     },
  48.    
  49.     format_num: function( num ){
  50.        
  51.         if( num < 1000 )
  52.             return num;
  53.        
  54.         var suffixes = ['k', 'm', 'b', 't' ];
  55.         var final_no = num;
  56.  
  57.         for( var i=0; i< suffixes.length; i++ ){
  58.             num = num/1000;
  59.            
  60.             if( num > 1000 ){
  61.                 continue;
  62.             }else{
  63.                 final_no = (Math.round( num*100 )/100) + suffixes[i];
  64.                 break;
  65.             }
  66.         }
  67.        
  68.         return final_no;
  69.        
  70.     },
  71.    
  72.     is_mobile: function(){
  73.         return /Mobi|Android/i.test(navigator.userAgent);
  74.     },
  75.  
  76.     open_popup: function(id){
  77.         var pp_overlay = document.getElementById(id);
  78.         if( !pp_overlay ){
  79.             return;
  80.         }
  81.         var that = this;
  82.         this.removeClass(pp_overlay, 'wpsr-pp-closed');
  83.         this.addClass(pp_overlay, 'wpsr-pp-opened');
  84.         this.removeClass(pp_overlay, 'wpsr-pp-slide-up');
  85.         setTimeout(function(){
  86.             that.addClass(pp_overlay, 'wpsr-pp-slide-down');
  87.         }, 10);
  88.     },
  89.  
  90.     close_popup: function(id){
  91.         var pp_overlay = document.getElementById(id);
  92.         if( !pp_overlay ){
  93.             return;
  94.         }
  95.         var that = this;
  96.         this.removeClass(pp_overlay, 'wpsr-pp-slide-down');
  97.         this.addClass(pp_overlay, 'wpsr-pp-slide-up');
  98.         setTimeout(function(){
  99.             that.removeClass(pp_overlay, 'wpsr-pp-opened');
  100.             that.addClass(pp_overlay, 'wpsr-pp-closed');
  101.         }, 250);
  102.     },
  103.  
  104.     copy: function(str){
  105.         var el = document.createElement('textarea');
  106.         el.value = str;
  107.         el.setAttribute('readonly', '');
  108.         el.style.position = 'absolute';
  109.         el.style.left = '-9999px';
  110.         document.body.appendChild(el);
  111.         el.select();
  112.         document.execCommand('copy');
  113.         document.body.removeChild(el);
  114.     }
  115.  
  116. };
  117.  
  118. document.addEventListener( 'DOMContentLoaded', function(){
  119.    
  120.     // Class names
  121.     var hide_class = 'wpsr-hide';
  122.     var closed_class = 'wpsr-closed';
  123.  
  124.     // Socializer links
  125.     var scr_links = document.querySelectorAll( '.socializer.sr-popup a' );
  126.  
  127.     for( i = 0; i < scr_links.length; i++ ){
  128.         var link = scr_links[i];
  129.         link.addEventListener( 'click', function(e){
  130.             var href = this.getAttribute( 'href' );
  131.             if( !( href == '#' || this.hasAttribute( 'onclick' ) || href == null ) ){
  132.                 wpsr_helpers.popup_window( href, '_blank', 800, 500 );
  133.             }
  134.             e.preventDefault();
  135.         });
  136.     }
  137.    
  138.     // Change share URL if device is mobile
  139.     if(wpsr_helpers.is_mobile()){
  140.         var mobile_links = document.querySelectorAll( '.socializer a[data-mobile]' );
  141.         for( i = 0; i < mobile_links.length; i++ ){
  142.             var link = mobile_links[i];
  143.             var mobile_url = link.getAttribute( 'data-mobile' );
  144.             link.setAttribute( 'href', mobile_url );
  145.         }
  146.     }
  147.  
  148.     // Sharebar
  149.     var the_sb = document.querySelector( '.wpsr-sharebar' );
  150.    
  151.     if( the_sb ){
  152.  
  153.         var icons = the_sb.querySelector( '.socializer' );
  154.  
  155.         the_sb.sm_action_call = function(ele){
  156.             wpsr_helpers.removeClass( this, 'wpsr-sb-vl' );
  157.             wpsr_helpers.addClass( this, 'wpsr-sb-hl' );
  158.            
  159.             wpsr_helpers.removeClass( icons, 'sr-vertical' );
  160.             wpsr_helpers.addClass( icons, 'sr-horizontal' );
  161.             wpsr_helpers.addClass( icons, 'sr-fluid' );
  162.         }
  163.  
  164.         the_sb.lg_action_call = function(){
  165.             wpsr_helpers.addClass( this, 'wpsr-sb-vl' );
  166.             wpsr_helpers.removeClass( this, 'wpsr-sb-hl' );
  167.            
  168.             wpsr_helpers.addClass( icons, 'sr-vertical' );
  169.             wpsr_helpers.removeClass( icons, 'sr-horizontal' );
  170.             wpsr_helpers.removeClass( icons, 'sr-fluid' );
  171.         }
  172.  
  173.         var sb_resize = function(){
  174.             stick_sb = document.querySelector( '.wpsr-sb-vl-scontent' );
  175.             if( stick_sb ){
  176.                 stick = stick_sb.getAttribute( 'data-stick-to' );
  177.                 stick_ele = document.querySelector( stick );
  178.                 if( stick_ele ){
  179.                     stick_offset = wpsr_helpers.offset( stick_ele );
  180.                     stick_sb.style.left = stick_offset.left + 'px';
  181.                 }
  182.             }
  183.  
  184.         }
  185.        
  186.         sb_resize();
  187.         window.addEventListener( 'resize', sb_resize );
  188.  
  189.     }
  190.    
  191.     // Text sharebar
  192.     tsb = document.querySelector( '.wpsr-text-sb' );
  193.    
  194.     if( tsb ){
  195.        
  196.         window.wpsr_tsb = {
  197.             stext: '',
  198.             startx: 0,
  199.             starty: 0
  200.         };
  201.        
  202.         var tsb_attr = {
  203.             ptitle: tsb.getAttribute( 'data-title' ),
  204.             purl: tsb.getAttribute( 'data-url' ),
  205.             psurl: tsb.getAttribute( 'data-surl' ),
  206.             ptuname: tsb.getAttribute( 'data-tuname' ),
  207.             cnt_sel: tsb.getAttribute( 'data-content' ),
  208.             word_count: tsb.getAttribute( 'data-tcount' )
  209.         };
  210.        
  211.         var get_selection_text = function() {
  212.             var text = '';
  213.             if( window.getSelection ){
  214.                 text = window.getSelection().toString();
  215.             }else if( document.selection && document.selection.type != 'Control' ){
  216.                 text = document.selection.createRange().text;
  217.             }
  218.             return text;
  219.         };
  220.        
  221.         var tsb_show = function( x, y ){
  222.             tsb.style.left = x + 'px';
  223.             tsb.style.top = y + 'px';
  224.             wpsr_helpers.addClass( tsb, 'wpsr-tsb-active' );
  225.         };
  226.        
  227.         var tsb_hide = function(){
  228.             wpsr_helpers.removeClass( tsb, 'wpsr-tsb-active' );
  229.         };
  230.        
  231.         var sel_link_text = function(){
  232.             var sel_text = wpsr_tsb.stext;
  233.             var wcount = parseInt( tsb_attr.word_count );
  234.             sel_text = sel_text.replace(/[^\x00-\x7F]/g, '');
  235.  
  236.             if( wcount == 0 ){
  237.                 return sel_text;
  238.             }else{
  239.                 return sel_text.split( ' ' ).slice( 0, wcount ).join( ' ' );
  240.             }
  241.         };
  242.        
  243.         var replace_link = function( link ){
  244.             var to_replace = {
  245.                 '{title}': escape(tsb_attr.ptitle),
  246.                 '{url}': tsb_attr.purl,
  247.                 '{s-url}': tsb_attr.psurl,
  248.                 '{twitter-username}': tsb_attr.ptuname,
  249.                 '{excerpt}': escape(sel_link_text())
  250.             };
  251.            
  252.             for( var key in to_replace ){
  253.                 if( to_replace.hasOwnProperty( key ) ){
  254.                     link = link.replace( RegExp( key, "g" ), to_replace[ key ] );
  255.                 }
  256.             }
  257.            
  258.             return link;
  259.            
  260.         }
  261.        
  262.         if( tsb_attr.cnt_sel != '' ){
  263.            
  264.             var tsb_cnt_sel = tsb_attr.cnt_sel.replace( /[\[\]<>"'/\\=&%]/g,'' );
  265.            var tsb_content = document.querySelectorAll( tsb_cnt_sel );
  266.            
  267.            for( var i = 0; i < tsb_content.length; i++ ){
  268.                
  269.                var content = tsb_content[i];
  270.                
  271.                content.addEventListener( 'mousedown', function(e){
  272.                    wpsr_tsb.startx = e.pageX;
  273.                    wpsr_tsb.starty = e.pageY;
  274.                });
  275.                
  276.                content.addEventListener( 'mouseup', function(e){
  277.                    var sel_text = get_selection_text();
  278.                    
  279.                    if( sel_text != '' ){
  280.                        
  281.                        tsb_x = ( e.pageX + parseInt( wpsr_tsb.startx ) )/2;
  282.                        tsb_y = Math.min( wpsr_tsb.starty, e.pageY );
  283.                        
  284.                        if( sel_text != wpsr_tsb.stext ){
  285.                            tsb_show( tsb_x, tsb_y );
  286.                            wpsr_tsb.stext = sel_text;
  287.                        }else{
  288.                            tsb_hide();
  289.                        }
  290.                        
  291.                    }else{
  292.                        
  293.                        tsb_hide();
  294.                        
  295.                    }
  296.                });
  297.            }
  298.        }
  299.        
  300.        document.body.addEventListener( 'mousedown', function(e){
  301.            tsb_hide();
  302.        });
  303.        
  304.        tsb.addEventListener( 'mousedown', function(e){
  305.            e.stopPropagation();
  306.        });
  307.        
  308.        var atags = tsb.querySelectorAll( 'a' );
  309.        for( var i = 0; i < atags.length; i++ ){
  310.            var atag = atags[i];
  311.            atag.addEventListener( 'click', function(e){
  312.                var alink = this.getAttribute( 'data-link' );
  313.                
  314.                if( alink != '#' ){
  315.                    rep_link = replace_link( alink );
  316.                    wpsr_helpers.popup_window( rep_link, '_blank', 800, 500 );
  317.                }
  318.                
  319.                e.preventDefault();
  320.            });
  321.        }
  322.        
  323.    }
  324.  
  325.    // Respond to screen size
  326.    var wpsr = document.querySelectorAll( '.wp-socializer' );
  327.    if( wpsr.length > 0 ){
  328.        
  329.        [ 'resize', 'load' ].forEach(function(e){
  330.            window.addEventListener(e, function(){
  331.                for( var i = 0; i < wpsr.length; i++ ){
  332.  
  333.                    var wpsr_ele = wpsr[ i ];
  334.                    var lg_action = wpsr_ele.getAttribute( 'data-lg-action' );
  335.                    var sm_action = wpsr_ele.getAttribute( 'data-sm-action' );
  336.                    var sm_width = wpsr_ele.getAttribute( 'data-sm-width' );
  337.                    var current_action = (window.innerWidth <= sm_width) ? sm_action : lg_action;
  338.  
  339.                    if(current_action == 'close'){
  340.                        wpsr_helpers.addClass(wpsr_ele, closed_class);
  341.                    }else{
  342.                        wpsr_helpers.removeClass(wpsr_ele, closed_class);
  343.                    }
  344.                    if(current_action == 'hide'){
  345.                        wpsr_helpers.addClass(wpsr_ele, hide_class);
  346.                    }else{
  347.                        wpsr_helpers.removeClass(wpsr_ele, hide_class);
  348.                    }
  349.  
  350.                    if(typeof wpsr_ele.sm_action_call === 'function' && current_action == sm_action){
  351.                        wpsr_ele.sm_action_call();
  352.                    }
  353.  
  354.                    if(typeof wpsr_ele.lg_action_call === 'function' && current_action == lg_action){
  355.                        wpsr_ele.lg_action_call();
  356.                    }
  357.  
  358.                }
  359.            });
  360.        });
  361.        
  362.    }
  363.    
  364.    // Close button event
  365.    var close_btns = document.querySelectorAll( '.wpsr-close-btn' );
  366.    if( close_btns.length > 0 ){
  367.        for( i = 0; i < close_btns.length; i++ ){
  368.            var close_btn = close_btns[i];
  369.  
  370.            close_btn.addEventListener( 'click', function(){
  371.                var parent = this.parentNode;
  372.                if( parent.classList.contains( closed_class ) ){
  373.                    wpsr_helpers.removeClass( parent, closed_class );
  374.                }else{
  375.                    wpsr_helpers.addClass( parent, closed_class );
  376.                }
  377.            });
  378.  
  379.        }
  380.  
  381.    }
  382.  
  383.    // Ajax share count
  384.    if( typeof wp_socializer !== 'undefined' ){
  385.        
  386.        var share_count = document.querySelectorAll( '[data-wpsrs]' );
  387.        
  388.        if( share_count.length > 0 ){
  389.            
  390.            var data = {};
  391.            var ajax_url = wp_socializer.ajax_url + '?action=wpsr_share_count';
  392.            
  393.            for( i = 0; i < share_count.length; i++ ){
  394.                var sEle = share_count[ i ];
  395.                var url = sEle.getAttribute( 'data-wpsrs' );
  396.                var services = sEle.getAttribute( 'data-wpsrs-svcs' ).split( ',' );
  397.                
  398.                if( !( url in data ) ){
  399.                    data[ url ] = [];
  400.                }
  401.                
  402.                for( j = 0; j < services.length; j++ ){
  403.                    if( data[ url ].indexOf( services[j] ) === -1 ){
  404.                        data[ url ].push( services[j] );
  405.                    }
  406.                }
  407.                
  408.            }
  409.            
  410.            var ajax_res = function( req ){
  411.                
  412.                var out = JSON.parse( req.responseText );
  413.                var ph = document.querySelectorAll( '[data-wpsrs="' + req.props.forURL + '"]' );
  414.                
  415.                for( i = 0; i < ph.length; i++ ){
  416.                    var phEle = ph[i];
  417.                    var services = phEle.getAttribute( 'data-wpsrs-svcs' ).split( ',' );
  418.                    var count = 0;
  419.                    for( j = 0; j < services.length; j++ ){
  420.                        var svc = services[j];
  421.                        if( svc in out ){
  422.                            count += parseInt( out[ svc ] ) || 0;
  423.                        }
  424.                    }
  425.                    if( count > 0 ){
  426.                        phEle.innerHTML = wpsr_helpers.format_num( count );
  427.                    }
  428.                }
  429.                
  430.            }
  431.            
  432.            for( var url in data ){
  433.                if( data.hasOwnProperty( url ) ){
  434.                    send_data = {
  435.                        'url': url,
  436.                        'services': data[ url ]
  437.                    };
  438.                    to_send = 'data=' + JSON.stringify( send_data );
  439.                    
  440.                    wpsr_helpers.ajax( ajax_url, 'POST', to_send, ajax_res, { forURL: url } );
  441.                    
  442.                }
  443.            }
  444.            
  445.        }
  446.        
  447.    }
  448.  
  449.    // Popup events
  450.    var pp_close_btns = document.querySelectorAll('.wpsr-pp-close');
  451.    if( pp_close_btns.length > 0 ){
  452.        for( i = 0; i < pp_close_btns.length; i++ ){
  453.            var pp_close_btn = pp_close_btns[i];
  454.            pp_close_btn.addEventListener( 'click', function(e){
  455.                e.preventDefault();
  456.                var id = this.getAttribute('data-id');
  457.                wpsr_helpers.close_popup(id);
  458.            });
  459.        }
  460.    }
  461.  
  462.    // Share menu
  463.    window.wpsr_share_menu = {
  464.        set_data: function(metadata){
  465.            try{
  466.                this.data = JSON.parse(metadata);
  467.            }catch(e){
  468.                this.data = {};
  469.                console.log(e);
  470.            }
  471.        },
  472.        get_param: function(param){
  473.            var defaults = {
  474.                'url': document.location.href,
  475.                'title': document.title,
  476.                'short-url': document.location.href
  477.            };
  478.            this.data = (typeof this.data === 'undefined') ? defaults : this.data;
  479.            if(param in this.data){
  480.                return this.data[param];
  481.            }else{
  482.                return (param in defaults) ? defaults[param] : '';
  483.            }
  484.        },
  485.        process_link: function(link){
  486.            var pattern = /\{(.+?)\}/g;
  487.            var current;
  488.            while(current = pattern.exec(link)){
  489.                var param_val = this.get_param(current[1]);
  490.                link = link.replaceAll('{' + current[1] + '}', encodeURI(param_val));
  491.            }
  492.            return link;
  493.        }
  494.    }
  495.  
  496.    var sm_btns = document.querySelectorAll('.sr-share-menu a');
  497.    if( sm_btns.length > 0 ){
  498.        for( i = 0; i < sm_btns.length; i++ ){
  499.            var sm_btn = sm_btns[i];
  500.            sm_btn.addEventListener( 'click', function(){
  501.                var metadata = this.getAttribute( 'data-metadata' );
  502.                wpsr_share_menu.set_data(metadata);
  503.                wpsr_helpers.open_popup('wpsr-share-menu');
  504.            });
  505.        }
  506.    }
  507.  
  508.    var sm_links = document.querySelectorAll('.wpsr-sm-link');
  509.    if( sm_links.length > 0 ){
  510.        for( i = 0; i < sm_links.length; i++ ){
  511.            var sm_link = sm_links[i];
  512.            sm_link.addEventListener( 'click', function(e){
  513.                e.preventDefault();
  514.  
  515.                var link_raw = (wpsr_helpers.is_mobile() && this.getAttribute('data-m')) ? this.getAttribute('data-m') : this.getAttribute('data-d');
  516.                var link = wpsr_share_menu.process_link(atob(link_raw));
  517.  
  518.                if(this.classList.contains('wpsr-sml-shortlink')){
  519.                    socializer_shortlink(e, false, link);
  520.                    return;
  521.                }
  522.  
  523.                if(link.indexOf('http') == 0){
  524.                    wpsr_helpers.popup_window(link, '_blank', 800, 500);
  525.                }else{
  526.                    window.location.href = link;
  527.                }
  528.            });
  529.        }
  530.    }
  531.  
  532.    var sl_copy_btn = document.getElementById('wpsr-sl-copy-btn');
  533.    if( sl_copy_btn ){
  534.        sl_copy_btn.addEventListener( 'click', function(e){
  535.            e.preventDefault();
  536.            wpsr_helpers.copy(document.getElementById('wpsr-short-link-url').value);
  537.            this.innerText = this.getAttribute('data-d');
  538.            var that = this;
  539.            setTimeout(function(){
  540.                that.innerText = that.getAttribute('data-c');
  541.            }, 3000);
  542.        });
  543.    }
  544.  
  545. });
  546.  
  547. function socializer_addbookmark( e ){
  548.    var ua = navigator.userAgent.toLowerCase();
  549.    var isMac = (ua.indexOf('mac') != -1), str = '';
  550.    e.preventDefault();
  551.    str = (isMac ? 'Command/Cmd' : 'CTRL') + ' + D';
  552.    alert('Press ' + str + ' to bookmark this page');
  553. }
  554.  
  555. function socializer_shortlink(e, link_ele, link_raw){
  556.    if( typeof e.preventDefault === 'function' ){
  557.        e.preventDefault();
  558.    }
  559.    var link = link_ele ? link_ele.getAttribute( 'href' ) : link_raw;
  560.    wpsr_helpers.open_popup('wpsr-short-link');
  561.    document.getElementById('wpsr-short-link-url').value = link;
  562. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement