PageSourceSearch

https://www.cancom.de/wp-content/cache/min/2/wp-content/plugins/gr…/js/location-helpers.js?ver=1790869033

js cancom.de collected 2026-10-02 07:19:28 UTC 12,819 bytes, 35 lines download raw bytes

1(function(){'use strict';function calculateDistance(lat1,lon1,lat2,lon2){const R=6371;const dLat=(lat2-lat1)*Math.PI/180;const dLon=(lon2-lon1)*Math.PI/180;const a=Math.sin(dLat/2)*Math.sin(dLat/2)+Math.cos(lat1*Math.PI/180)*Math.cos(lat2*Math.PI/180)*Math.sin(dLon/2)*Math.sin(dLon/2);const c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));return R*c}
2function isZipCode(query){if(!query||typeof query!=='string')return!1;const zipPattern=/^\d{2,5}([\s-]?\d{0,3})?$/;return zipPattern.test(query.trim())}
3function extractPostId(html){if(!html)return null;const tempDiv=document.createElement('div');tempDiv.innerHTML=html;const postElement=tempDiv.querySelector('[data-post]');if(postElement){const postId=parseInt(postElement.getAttribute('data-post'),10);return isNaN(postId)?null:postId}
4return null}
5function formatDistance(km,unit){if(unit==='miles'){const miles=km*0.621371;if(miles<1){const yards=miles*1760;return Math.round(yards)+' yd'}
6return miles.toFixed(1)+' miles'}
7if(km<1){const meters=km*1000;return Math.round(meters)+' m'}
8return km.toFixed(1)+' km'}
9function addDistanceToHtml(html,distance,unit){if(!html)return html;const tempDiv=document.createElement('div');tempDiv.innerHTML=html;const titleElement=tempDiv.querySelector('h1, h2, h3, h4, h5, h6, .wp-block-post-title, .entry-title');if(titleElement){const distanceElement=document.createElement('span');distanceElement.className='greyd-location-distance';distanceElement.textContent=formatDistance(distance,unit);distanceElement.setAttribute('aria-label','Distance: '+formatDistance(distance,unit));titleElement.insertAdjacentElement('afterend',distanceElement)}
10return tempDiv.innerHTML}
11function getCountryCode(countryName){if(!countryName||typeof countryName!=='string')return null;const normalized=countryName.trim();if(!normalized)return null;const countryMap={'germany':'de','deutschland':'de','allemagne':'de','alemania':'de','austria':'at','österreich':'at','autriche':'at','switzerland':'ch','schweiz':'ch','suisse':'ch','svizzera':'ch','swiss':'ch','france':'fr','frankreich':'fr','francia':'fr','italy':'it','italien':'it','italia':'it','spain':'es','spanien':'es','espagne':'es','españa':'es','netherlands':'nl','niederlande':'nl','pays-bas':'nl','holland':'nl','belgium':'be','belgien':'be','belgique':'be','belgië':'be','poland':'pl','polen':'pl','pologne':'pl','polonia':'pl','czech republic':'cz','tschechien':'cz','république tchèque':'cz','česká republika':'cz','portugal':'pt','greece':'gr','griechenland':'gr','grèce':'gr','denmark':'dk','dänemark':'dk','danemark':'dk','sweden':'se','schweden':'se','suède':'se','finland':'fi','finnland':'fi','finlande':'fi','norway':'no','norwegen':'no','norvège':'no','ireland':'ie','irland':'ie','irlande':'ie','united kingdom':'gb','uk':'gb','great britain':'gb','britain':'gb','england':'gb','grossbritannien':'gb','royaume-uni':'gb','russia':'ru','russland':'ru','russie':'ru','ukraine':'ua','ukraina':'ua','united states':'us','usa':'us','us':'us','united states of america':'us','vereinigte staaten':'us','vereinigte staaten von amerika':'us','vereinigte':'us','états-unis':'us','états-unis d\'amérique':'us','canada':'ca','kanada':'ca','mexico':'mx','mexiko':'mx','méxico':'mx','china':'cn','chine':'cn','japan':'jp','japon':'jp','india':'in','indien':'in','inde':'in','south korea':'kr','südkorea':'kr','corée du sud':'kr','korea':'kr','australia':'au','australien':'au','australie':'au','new zealand':'nz','neuseeland':'nz','nouvelle-zélande':'nz','brazil':'br','brasilien':'br','brésil':'br','brasil':'br','argentina':'ar','argentinien':'ar','argentine':'ar','chile':'cl','turkey':'tr','türkei':'tr','turquie':'tr','israel':'il','israël':'il','saudi arabia':'sa','saudi-arabien':'sa','arabie saoudite':'sa','united arab emirates':'ae','uae':'ae','vereinigte arabische emirate':'ae','émirats arabes unis':'ae','south africa':'za','südafrika':'za','afrique du sud':'za','egypt':'eg','ägypten':'eg','égypte':'eg','nigeria':'ng','nigéria':'ng'};const lower=normalized.toLowerCase();if(countryMap[lower])return countryMap[lower];for(const key in countryMap){if(lower.includes(key)||key.includes(lower))return countryMap[key]}
12for(const key in countryMap){const keyWords=key.split(/\s+/);if(keyWords.length>1&&keyWords.length<=3){const keyStart=keyWords.slice(0,2).join(' ');if(lower.startsWith(keyStart)||lower.includes(keyStart))return countryMap[key]}}
13return null}
14function inferCountryFromLocations(locations){if(!locations||typeof locations!=='object')return null;const countryCounts={};let totalPosts=0,postsWithCountry=0;
14Object.values(locations).forEach(function(loc){if(loc&&typeof loc==='object'){const country=loc.country&&typeof loc.country==='string'?loc.country.trim():null;if(country){countryCounts[country]=(countryCounts[country]||0)+1;postsWithCountry++}
15totalPosts++}});if(totalPosts===0||postsWithCountry===0)return null;const sorted=Object.keys(countryCounts).sort(function(a,b){return countryCounts[b]-countryCounts[a]});if(sorted.length>0){const mostCommon=sorted[0];const pct=(countryCounts[mostCommon]/postsWithCountry)*100;if(pct>=30)return mostCommon}
16return null}
17function getCountriesFromLocations(locations){if(!locations||typeof locations!=='object')return[];const countryCounts={};Object.values(locations).forEach(function(loc){if(loc&&loc.country&&typeof loc.country==='string'){const country=loc.country.trim();if(country)countryCounts[country]=(countryCounts[country]||0)+1}});return Object.keys(countryCounts).sort(function(a,b){return countryCounts[b]-countryCounts[a]})}
18function extractCountryFromResult(result){var country='',countryCode='';if(result.address&&result.address.country)country=result.address.country.trim();if(result.address&&result.address.country_code)countryCode=result.address.country_code.toLowerCase();if(!country&&result.display_name){var parts=result.display_name.split(',').map(function(p){return p.trim()});if(parts.length>0){var lastPart=parts[parts.length-1];var code=getCountryCode(lastPart);if(code){countryCode=code.toLowerCase();country=lastPart}else{country=lastPart;countryCode=getCountryCode(lastPart)||''}}}
19return{country:country,countryCode:countryCode}}
20var codeToNameMap={us:'United States',ua:'Ukraine',de:'Germany',at:'Austria',ch:'Switzerland',fr:'France',it:'Italy',es:'Spain',nl:'Netherlands',be:'Belgium',pl:'Poland',cz:'Czech Republic',pt:'Portugal',gr:'Greece',dk:'Denmark',se:'Sweden',fi:'Finland',no:'Norway',ie:'Ireland',gb:'United Kingdom',ru:'Russia',ca:'Canada',mx:'Mexico',cn:'China',jp:'Japan',in:'India',kr:'South Korea',au:'Australia',nz:'New Zealand',br:'Brazil',ar:'Argentina',cl:'Chile',tr:'Turkey',il:'Israel',sa:'Saudi Arabia',ae:'United Arab Emirates',za:'South Africa',eg:'Egypt',ng:'Nigeria'};function rankGeocodingResults(results,preferredCountries,locations){if(!results||results.length===0)return[];var actualLocations=locations;if(locations&&typeof locations==='object'&&locations.locations){actualLocations=locations.locations}
21var allPreferredCountries=[];if(preferredCountries&&preferredCountries.length>0){preferredCountries.forEach(function(c){if(c&&allPreferredCountries.indexOf(c)===-1)allPreferredCountries.push(c)})}
22var serverPreferred=locations&&typeof locations==='object'&&Array.isArray(locations.preferred_countries)?locations.preferred_countries:null;if(serverPreferred&&serverPreferred.length>0){serverPreferred.forEach(function(c){if(c&&allPreferredCountries.indexOf(c)===-1)allPreferredCountries.push(c)})}else if(actualLocations){getCountriesFromLocations(actualLocations).forEach(function(c){if(c&&allPreferredCountries.indexOf(c)===-1)allPreferredCountries.push(c)})}
23var scoredResults=results.map(function(result){var score=0;var countryInfo=extractCountryFromResult(result);var resultCountry=countryInfo.country;var resultCountryCode=countryInfo.countryCode;var normalizedResultCountry=resultCountry;if(resultCountryCode&&codeToNameMap[resultCountryCode.toLowerCase()]){normalizedResultCountry=codeToNameMap[resultCountryCode.toLowerCase()]}else if(resultCountryCode&&allPreferredCountries.length>0){for(var i=0;i<allPreferredCountries.length;i++){var preferredCode=getCountryCode(allPreferredCountries[i]);if(preferredCode&&preferredCode.toLowerCase()===resultCountryCode.toLowerCase()){normalizedResultCountry=allPreferredCountries[i];break}}}
24if(allPreferredCountries.length>0){var resultLower=resultCountry?resultCountry.toLowerCase():'';var normalized=normalizedResultCountry?normalizedResultCountry.toLowerCase():'';for(var j=0;j<allPreferredCountries.length;j++){var preferred=allPreferredCountries[j].toLowerCase();if(resultLower&&(preferred===resultLower||preferred===normalized||resultL
24ower.indexOf(preferred)!==-1||preferred.indexOf(resultLower)!==-1)){score+=1000-j;break}}
25if(score===0&&resultCountryCode){for(var k=0;k<allPreferredCountries.length;k++){var pc=getCountryCode(allPreferredCountries[k]);if(pc&&pc.toLowerCase()===resultCountryCode.toLowerCase()){score+=800-k;break}}}}
26if(result.type==='postcode'||(result.class==='place'&&result.type==='postcode'))score+=100;if(result.importance)score+=result.importance*10;if(result.address){if(result.address.city||result.address.town||result.address.village)score+=10;if(result.address.state||result.address.region)score+=5}
27return{result:result,score:score}});scoredResults.sort(function(a,b){return b.score-a.score});return scoredResults.map(function(item){return item.result})}
28function geocodeAddress(address,isZipCode,preferredCountries,locations){if(!address||typeof address!=='string')return Promise.resolve(null);var q=address.trim();if(!q)return Promise.resolve(null);var geocodeBase=(typeof greyd!=='undefined'&&greyd.rest_api&&greyd.rest_api.geocode_url)?greyd.rest_api.geocode_url:(typeof greyd!=='undefined'&&greyd.rest_api&&greyd.rest_api.root&&greyd.rest_api.routes&&greyd.rest_api.routes.geocode)?greyd.rest_api.root+greyd.rest_api.routes.geocode:'';if(!geocodeBase){return Promise.reject(new Error('Geocoding route not available'))}
29var params={address:q,postalcode:(isZipCode===!0?1:0),limit:10};var allPreferredCountries=[];if(preferredCountries&&preferredCountries.length>0){preferredCountries.forEach(function(c){if(c&&allPreferredCountries.indexOf(c)===-1)allPreferredCountries.push(c)})}
30var serverPreferred=locations&&typeof locations==='object'&&Array.isArray(locations.preferred_countries)?locations.preferred_countries:null;if((!serverPreferred||serverPreferred.length===0)&&typeof greyd!=='undefined'&&greyd.data&&Array.isArray(greyd.data.preferred_countries)&&greyd.data.preferred_countries.length>0){serverPreferred=greyd.data.preferred_countries}
31if(serverPreferred&&serverPreferred.length>0){serverPreferred.forEach(function(c){if(c&&allPreferredCountries.indexOf(c)===-1)allPreferredCountries.push(c)})}else if(locations&&typeof locations==='object'&&locations.locations){getCountriesFromLocations(locations.locations).forEach(function(c){if(c&&allPreferredCountries.indexOf(c)===-1)allPreferredCountries.push(c)})}
32if(preferredCountries&&preferredCountries.length>0){var countryCodes=preferredCountries.map(function(country){return getCountryCode(country)}).filter(function(code){return code!==null&&code!==''});if(countryCodes.length>0){params.countrycodes=countryCodes.join(',')}}
33var sep=geocodeBase.indexOf('?')!==-1?'&':'?';var queryString=Object.keys(params).map(function(key){return encodeURIComponent(key)+'='+encodeURIComponent(params[key])}).join('&');var url=geocodeBase+sep+queryString;return fetch(url,{method:'GET',headers:{'Accept':'application/json'}}).then(function(res){if(!res.ok)throw new Error('Geocode request failed: '+res.status);return res.json()}).then(function(data){if(!data||!Array.isArray(data)||data.length===0)return null;var bestResult;if(allPreferredCountries.length>0){var ranked=rankGeocodingResults(data,allPreferredCountries,locations);bestResult=ranked.length>0?ranked[0]:data[0]}else{bestResult=data[0]}
34var lat=parseFloat(bestResult.lat,10);var lng=parseFloat(bestResult.lon,10);if(isNaN(lat)||isNaN(lng)||lat<-90||lat>90||lng<-180||lng>180)return null;var countryInfo=extractCountryFromResult(bestResult);return{lat:lat,lng:lng,country:countryInfo.country||undefined,countryCode:countryInfo.countryCode||undefined}}).catch(function(err){if(typeof console!=='undefined'&&console.warn)console.warn('Geocode error:',err);return Promise.reject(err)})}
35window.greydLocationHelpers={calculateDistance:calculateDistance,isZipCode:isZipCode,extractPostId:extractPostId,formatDistance:formatDistance,addDistanceToHtml:addDistanceToHtml,getCountryCode:getCountryCode,inferCountryFromLocations:inferCountryFromLocations,getCountriesFromLocations:getCountriesFromLocations,extractCountryFromResult:extractCountryFromResult,rankGeocodingResults:rankGeocodingResults,geocodeAddress:geocodeAddress}})()

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.