1"use strict"; 2 3[].forEach.call(document.querySelectorAll('a'), function(el) { 4 el.addEventListener('click', wpil_link_clicked); 5 el.addEventListener('auxclick', wpil_link_clicked); 6}); 7 8function wpil_link_clicked(e){ 9// e.preventDefault(); // todo remember to remove this 10 var link = this; 11 var linkAnchor = ''; 12 var hasImage = false; 13 var imageTitle = ''; 14 var imageTags = ['img', 'svg']; 15 16 // if the click wasn't a primary or middle click, or there's no link, exit 17 if(!(e.which == 1 || e.button == 0 ) && !(e.which == 2 || e.button == 4 ) || link.length < 1){ 18 return; 19 } 20 21 if('1' === wpilFrontend.disableClicks){ 22 return; 23 } 24 25 if(this.href === undefined || link.href === '#'){ 26 return; 27 } 28 29 function findLinkText(link){ 30 if(link.children.length > 0){ 31 for(var i in link.children){ 32 var childThis = link.children[i]; 33 if(childThis.children !== undefined && childThis.children.length > 0 && linkAnchor === ''){ 34 findLinkText(childThis); 35 } 36 if(childThis.nodeType === 1 && -1 !== imageTags.indexOf(childThis.nodeName.toLowerCase()) && imageTitle === ''){ 37 hasImage = true; 38 var title = (undefined !== childThis.title) ? childThis.title : ''; 39 40 if(undefined !== title){ 41 imageTitle = title.trim(); 42 } 43 } 44 linkAnchor = linkAnchor.trim(); 45 imageTitle = (imageTitle !== undefined) ? imageTitle.trim(): ''; 46 }; 47 } 48 if(undefined !== link.outerText){ 49 linkAnchor = link.outerText; 50 } 51 } 52 53 findLinkText(link); 54 55 if(linkAnchor === '' && hasImage){ 56 if(imageTitle !== ''){ 57 linkAnchor = wpilFrontend.clicksI18n.imageText + imageTitle; 58 }else{ 59 linkAnchor = wpilFrontend.clicksI18n.imageNoText; 60 } 61 }else if(linkAnchor === '' && !hasImage){ 62 linkAnchor = wpilFrontend.clicksI18n.noText; 63 } 64 65 // ignore non-content links 66 if(wpilFrontend.trackAllElementClicks === '0' && hasParentElements(link, 'header, footer, nav, [id~=header], [id~=menu], [id~=footer], [id~=widget], [id~=comment], [class~=header], [class~=menu], [class~=footer], [class~=widget], [class~=comment], #wpadminbar')){ 67 return; 68 } 69 70 var location = getLinkLocation(link); 71 72 makeAjaxCall({ 73 action: 'wpil_link_clicked', 74 post_id: wpilFrontend.postId, 75 post_type: wpilFrontend.postType, 76 link_url: link.href, 77 link_anchor: linkAnchor, 78 link_location: location 79 }); 80} 81window.addEventListener("load", openLinksInNewTab); 82 83/** 84 * Sets non nav links on a page to open in a new tab based on the settings. 85 **/ 86function openLinksInNewTab(){ 87 // exit if non of the links are supposed to open in a new tab via JS 88 if(wpilFrontend.openLinksWithJS == 0 || (wpilFrontend.openExternalInNewTab == 0 && wpilFrontend.openInternalInNewTab == 0) ){ 89 return; 90 } 91 92 [].forEach.call(document.querySelectorAll('a'), function(element) { 93 // if the link is not a nav link 94 if(!hasParentElements(element, 'header, footer, nav, [id~=header], [id~=menu], [id~=footer], [id~=widget], [id~=comment], [class~=header], [class~=menu], [class~=footer], [class~=widget], [class~=comment], #wpadminbar')){ 95 // if there is a url in the link, there isn't a target and the link isn't a jump link 96 if(element.href && !element.target && element.href.indexOf(window.location.href) === -1){ 97 var url = new URL(element.href); 98 var internal = (window.location.hostname === url.hostname) ? true: false; 99 // if the settings allow it 100 if( (internal && parseInt(wpilFrontend.openInternalInNewTab)) || (!internal && parseInt(wpilFrontend.openExternalInNewTab)) ){ 101 // set the link to open in a new page 102 element.setAttribute('target', '_blank'); 103 } 104 } 105 } 106 });
107} 108 109// replacement for "$(element).parents('x,y,z')" in if checks 110function hasParentElements(element, parentList = ''){ 111 var tag = (!element) ? false: element.tagName.toLowerCase(); 112 if(!element || tag === 'body' || tag === 'main' || tag === 'article'){ 113 return false; 114 } 115 116 if(typeof parentList === 'string'){ 117 parentList = parentList.split(','); 118 } 119 120 var found = false; 121 for(var i in parentList){ 122 var j = parentList[i]; 123 124 if(-1 !== j.indexOf('id~=')){ 125 var id = j.replace(/\[id~=|\]/g, '').trim(); 126 127 if(undefined !== element.id && '' !== element.id && -1 !== element.id.indexOf(id)){ // if the id is set and has a match 128 found = true; 129 break; 130 } 131 }else if(-1 !== j.indexOf('class~=')){ 132 var id = j.replace(/\[class~=|\]/g, '').trim(); 133 if(undefined !== element.classList && '' !== element.className && -1 !== element.className.indexOf(id)){ 134 found = true; 135 break; 136 } 137 }else if(-1 !== j.indexOf('#')){ 138 var id = j.replace(/#/g, '').trim(); 139 if(undefined !== element.id && '' !== element.id && id === element.id){ 140 found = true; 141 break; 142 } 143 }else if(!j.match(/[^a-zA-Z]/)){ // tag match 144 var id = j.trim(); 145 if(element.tagName.toLowerCase() === id){ 146 found = true; 147 break; 148 } 149 } 150 } 151 152 if(found){ 153 return true; 154 }else if(element.parentNode !== ''){ 155 return hasParentElements(element.parentNode, parentList); 156 } 157 158 return false; 159}; 160 161function makeAjaxCall(callData = {}){ 162 if(window.jQuery){ 163 callWithJquery(callData); 164 }else{ 165 callWithVanilla(callData); 166 } 167} 168 169function callWithJquery(callData = {}){ 170 jQuery.ajax({ 171 type: 'POST', 172 url: wpilFrontend.ajaxUrl, 173 data: callData, 174 success: function(response){ 175 176 } 177 }); 178} 179 180function callWithVanilla(callData = {}){ 181 function respond(dat){ 182 if(dat.currentTarget !== undefined){ 183 console.log(dat.currentTarget.response); 184 } 185 } 186 187 async function ajaxPost (data, callback) { 188 var url = wpilFrontend.ajaxUrl, 189 xhr = new XMLHttpRequest(); 190 var params = []; 191 for(var i in data){ 192 params.push(encodeURIComponent(i) + '=' + encodeURIComponent(data[i])); 193 } 194 params = params.join('&'); 195 xhr.open("POST", url); 196 xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 197// xhr.onload = callback.bind(xhr); 198 xhr.send(params); 199 } 200 201 ajaxPost(callData, respond); 202} 203 204function getLinkLocation(element){ 205 var location = 'Body Content'; 206 var sections = {'Search': ['search', '[id~=search]', '[class~=search]'], 207 'Header': ['header', '[id~=header]', '[class~=header]'], 208 'Comment Section': ['[id~=comment]', '[class~=comment]'], 209 'Footer': ['footer', '[id~=footer]', '[class~=footer]'], 210 'Menu': ['[id~=menu]', '[class~=menu]'], 211 'Navigation': ['nav'], 212 'Sidebar': ['sidebar', '[id~=sidebar]', '[class~=sidebar]', '[id~=widget]', '[class~=widget]'], 213 'Body Content': ['main', 'article', '[class~=main]'], 214 }; 215 216 if(!element || element.tagName.toLowerCase() === 'body'){ 217 return location; 218 } 219 220 var found = false; 221 elementLoop: 222 for(var section in sections){ 223 var sectionData = sections[section]; 224 for(var i in sectionData){ 225 var j = sectionData[i]; 226 227 if(-1 !== j.indexOf('id~=')){ 228 var id = j.replace(/\[id~=|\]/g, '').trim(); 229 230 if(undefined !== element.id && '' !== element.id && -1 !== element.id.indexOf(id)){ // if the id is set and has a match 231 found = true; 232 location = section; 233 break elementLoop; 234 } 235 }else if(-1 !== j.indexOf('class~=')){ 236 var id = j.replace(/\[class~=|\]/g, '').trim(); 237 if(undefined !== element.classList && '' !== element.className && -1 !== element.className.indexOf(id)){ 238 found = true; 239 location = section; 240 break elementLoop; 241 } 242 }else if(-1 !== j.indexOf('#')){ 243 var id = j.replace(/#/g, '').trim();
244 if(undefined !== element.id && '' !== element.id && id === element.id){ 245 found = true; 246 location = section; 247 break elementLoop; 248 } 249 }else if(!j.match(/[^a-zA-Z]/)){ // tag match 250 var id = j.trim(); 251 if(element.tagName.toLowerCase() === id){ 252 found = true; 253 location = section; 254 break elementLoop; 255 } 256 } 257 } 258 } 259 260 if(found){ 261 return location; 262 }else if(element.parentNode !== ''){ 263 return getLinkLocation(element.parentNode); 264 } 265 266 return false; 267}
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.