Frage

If I go to this Google Maps page, some of the HTML is missing in View Source, but shows up in Firebug.

Likewise, when that same URL is passed to my function, the following HTML does not show up in the responseText, but it does show in Firebug when I open the page.

<a id="mapmaker-link" class="kd-button mini left" style="" href="https://www.google.com/mapmaker?ll=41.06877,-112.047203&spn=0.038696,0.132093&t=h&z=14&vpsrc=0&q=1093+W+3090+S,Syracuse,+UT&utm_medium=website&utm_campaign=relatedproducts_maps&utm_source=mapseditbutton_normal">

Here is the function I'm using:

function updateMap(url) {
     GM_xmlhttpRequest(
    {
        method: 'GET',
        url: url,
        onload: function(resp) {
            var ll = resp.responseText.split("mapmaker?")[1];
            ll     = ll.split("&")[0];
            document.getElementById('googlemap').href = url+"&"+ll;
        }
    });
}


I have placed a sample responseText value at pastebin.com/Tt8nrzG8.

War es hilfreich?

Lösung

The response is "missing" HTML because the called page loads that HTML (and almost all of the page's content) via AJAX.
GM_xmlhttpRequest (and all other current AJAX methods) only gets the static source of a given page. Such XHR requests cannot process a requested page's javascript, like a browser does when you browse to the page.

In fact, if you save that sample responseText, that you linked, as an HTML file; you'll see it looks like this:

The OP's sample response


See "How to get an AJAX get-request to wait for the page to be rendered before returning a response?", for the same type of problem. But note that the answer recommends that you use an API, if one is available.

So, use the Google Maps API to get the lat/long you want for your URL.

Or, the easiest approach is still to have the script also run on Google maps pages and do a one-time zoom on links with your special URL parameter -- like I recommended on your previous question. This has the added advantage that no calls to Google are made/needed until you actually decide to click your Google Maps link.

If you do opt for the iframe approach (again, NOT recommended for ANY Google site), beware that you will need to adjust the URL to tell Google to allow iframing and the lat/long information will be in a different part of the page.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top