1const wpp_params = document.currentScript.dataset; 2const WordPressPopularPosts = (function() { 3 4 "use strict"; 5 6 const noop = function(){}; 7 8 const get = function( url, params, callback, additional_headers ){ 9 callback = ( 'function' === typeof callback ) ? callback : noop; 10 ajax( "GET", url, params, callback, additional_headers ); 11 }; 12 13 const post = function( url, params, callback, additional_headers ){ 14 callback = ( 'function' === typeof callback ) ? callback : noop; 15 ajax( "POST", url, params, callback, additional_headers ); 16 }; 17 18 const ajax = function( method, url, params, callback, additional_headers ){ 19 /* Create XMLHttpRequest object and set variables */ 20 let xhr = new XMLHttpRequest(), 21 target = url, 22 args = params, 23 valid_methods = ["GET", "POST"], 24 headers = { 25 'X-Requested-With': 'XMLHttpRequest' 26 }; 27 28 method = -1 != valid_methods.indexOf( method ) ? method : "GET"; 29 30 /* Set request headers */ 31 if ( 'POST' == method ) { 32 headers['Content-Type'] = 'application/x-www-form-urlencoded'; 33 } 34 35 if ( 'object' == typeof additional_headers && Object.keys(additional_headers).length ) { 36 headers = Object.assign({}, headers, additional_headers); 37 } 38 39 /* Set request method and target URL */ 40 xhr.open( method, target + ( 'GET' == method ? '?' + args : '' ), true ); 41 42 for (const key in headers) { 43 if ( headers.hasOwnProperty(key) ) { 44 xhr.setRequestHeader( key, headers[key] ); 45 } 46 } 47 48 /* Hook into onreadystatechange */ 49 xhr.onreadystatechange = function() { 50 if ( 4 === xhr.readyState && 200 <= xhr.status && 300 > xhr.status ) { 51 if ( 'function' === typeof callback ) { 52 callback.call( undefined, xhr.response ); 53 } 54 } 55 }; 56 57 /* Send request */ 58 xhr.send( ( 'POST' == method ? args : null ) ); 59 }; 60 61 const theme = function(wpp_list) { 62 let base_styles = document.createElement('style'), 63 dummy_list = document.createElement('ul'); 64 65 dummy_list.innerHTML = '<li><a href="#"></a></li>'; 66 wpp_list.parentNode.appendChild(dummy_list); 67 68 let dummy_list_item_styles = getComputedStyle(dummy_list.querySelector('li')), 69 dummy_link_item_styles = getComputedStyle(dummy_list.querySelector('li a')); 70 71 base_styles.innerHTML = '.wpp-list li {font-size: '+ dummy_list_item_styles.fontSize +'}'; 72 base_styles.innerHTML += '.wpp-list li a {color: '+ dummy_link_item_styles.color +'}'; 73 74 wpp_list.parentNode.removeChild(dummy_list); 75 76 let wpp_list_sr = wpp_list.attachShadow({mode: "open"}); 77 78 wpp_list_sr.append(base_styles); 79 80 while(wpp_list.firstElementChild) { 81 wpp_list_sr.append(wpp_list.firstElementChild); 82 } 83 }; 84 85 return { 86 get: get, 87 post: post, 88 ajax: ajax, 89 theme: theme 90 }; 91 92})(); 93 94(function(){ 95 if ( ! Object.keys(wpp_params).length ) { 96 console.error('WPP params not found, if you are using a JS minifier tool please add wpp.min.js to its exclusion list'); 97 return; 98 } 99 100 const post_id = Number(wpp_params.postId); 101 let do_request = true; 102 103 if ( post_id ) { 104 if ( '1' == wpp_params.sampling ) { 105 let num = Math.floor(Math.random() * wpp_params.samplingRate) + 1; 106 do_request = ( 1 === num ); 107 } 108 109 if ( 'boolean' === typeof window.wpp_do_request ) { 110 do_request = window.wpp_do_request; 111 } 112 113 if ( do_request ) { 114 WordPressPopularPosts.post( 115 wpp_params.apiUrl + '/v2/views/' + post_id, 116 "_wpnonce=" + wpp_params.token + "&sampling=" + wpp_params.sampling + "&sampling_rate=" + wpp_params.samplingRate, 117 function( response ) { 118 wpp_params.debug&&window.console&&window.console.log&&window.console.log(JSON.parse(response)); 119 } 120 ); 121 } 122 } 123})(); 124 125document.addEventListener('DOMContentLoaded', function() { 126 if ( ! Object.keys(wpp_params).length ) { 127 return; 128 } 129 130 const widget_placeholders = document.querySelectorAll('.wpp-widget-block-placeholder, .wpp-shortcode-placeholder'); 131 let w = 0; 132 133 while ( w < widget_placeholders.length ) { 134 fetchWidget(widget_placeholders[w]); 135 w++; 136 } 137 138 const sr = document.querySelectorAll('.popular-posts-sr'); 139 140 if ( sr.length ) { 141 for( let s = 0; s < sr.length; s++ ) { 142 WordPressPopularPosts.theme(sr[s]); 143 } 144 } 145 146 function fetchWidget(widget_placeholder) { 147 let headers = { 148 'Content-Type': 'application/json', 149 'X-WP-Nonce': wpp_params.token 150 }, 151 params = '', 152 method = 'POST', 153 url = wpp_params.apiUrl + '/v2/widget?is_single=' + wpp_params.postId + ( wpp_params.lang ? '&lang=' + wpp_params.lang : '' ); 154 155 let json_tag = widget_placeholder.parentNode.querySelector('script[type="application/json"]'); 156 157 if ( json_tag ) { 158 let args = JSON.parse(json_tag.textContent.replace(/[\n\r]/g,'')); 159 params = JSON.stringify(args); 160 } 161 162 WordPressPopularPosts.ajax( 163 method, 164 url, 165 params, 166 function(response) { 167 renderWidget(response, widget_placeholder); 168 }, 169 headers 170 ); 171 } 172 173 function renderWidget(response, widget_placeholder) { 174 widget_placeholder.insertAdjacentHTML('afterend', JSON.parse(response).widget); 175 176 let parent = widget_placeholder.parentNode, 177 sr = parent.querySelector('.popular-posts-sr'), 178 json_tag = parent.querySelector('script[type="application/json"]'); 179 180 if ( json_tag ) 181 parent.removeChild(json_tag); 182 183 parent.removeChild(widget_placeholder); 184 parent.classList.add('wpp-ajax'); 185 186 if ( sr ) { 187 WordPressPopularPosts.theme(sr); 188 } 189 190 let event = new Event("wpp-onload", {"bubbles": true, "cancelable": false}); 191 parent.dispatchEvent(event); 192 } 193});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.