Last active
May 2, 2018 06:57
-
-
Save jsgao0/ef39e2a925a1097967838dc0bff06431 to your computer and use it in GitHub Desktop.
Intercept the data of google poi while user clicks on the map.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = { | |
getQueryParameter: function (name, url) { | |
if (!url) { | |
url = window.location.href; | |
} | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return null; | |
if (!results[2]) return ''; | |
return decodeURIComponent(results[2].replace(/\+/g, " ")); | |
} | |
}; | |
var observer = new MutationObserver(function(mutations) { | |
var i = 0, jsonpScript, jsonpCallback, jsonpCallbackNP, insertedNodes = []; | |
mutations.forEach(function(mutation) { | |
for (i = 0; i < mutation.addedNodes.length; i++) { | |
insertedNodes.push(mutation.addedNodes[i]); | |
} | |
}); | |
jsonpScript = [].filter.call(insertedNodes, function (script) { | |
if (script.src) { | |
return script.src.indexOf('https://maps.googleapis.com/maps/api/js/jsonp/ApplicationService.GetEntityDetails') > -1; | |
} else { | |
return false; | |
} | |
}).pop(); | |
if (jsonpScript) { | |
jsonpCallbackNP = url.getQueryParameter('callback', jsonpScript.src).split('.'); | |
jsonpCallback = window[jsonpCallbackNP[0]][jsonpCallbackNP[1]]; | |
window[jsonpCallbackNP[0]][jsonpCallbackNP[1]] = function (d) { | |
console.log(d); | |
jsonpCallback(d); | |
}; | |
} | |
}); | |
observer.observe(query.q('head'), { childList: true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment