【问题标题】:GM_xmlhttpRequest's responsetext is missing some HTMLGM_xmlhttpRequest 的响应文本缺少一些 HTML
【发布时间】:2013-04-09 22:39:51
【问题描述】:

如果我转到this Google Maps page查看源代码中缺少一些 HTML,但会显示在 Firebug 中。

同样,当将相同的 URL 传递给我的函数时,以下 HTML 不会显示在 responseText 中,但当我打开页面时它会显示在 Firebug 中。

<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">

这是我正在使用的功能:

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;
        }
    });
}


我在pastebin.com/Tt8nrzG8 放置了一个样本responseText 值。

【问题讨论】:

标签: javascript xmlhttprequest greasemonkey gm-xmlhttprequest


【解决方案1】:

响应是“缺少”HTML,因为被调用页面通过 AJAX 加载该 HTML(以及几乎所有页面内容)。
GM_xmlhttpRequest(以及所有其他当前 AJAX 方法)仅获取给定的页面。此类 XHR 请求无法处理所请求页面的 javascript,就像您浏览页面时浏览器所做的那样。

其实如果你把that sample responseText, that you linked,保存为HTML文件;你会看到它看起来像这样:


请参阅"How to get an AJAX get-request to wait for the page to be rendered before returning a response?",了解相同类型的问题。但请注意,答案建议您使用API(如果可用)。

因此,请使用 the Google Maps API 获取您想要的 URL 纬度/经度。

或者,最简单的方法仍然是让脚本也在 Google 地图页面上运行,并使用您的特殊 URL 参数对链接进行一次性缩放——就像我在 your previous question 上推荐的那样.这样做还有一个额外的好处,即在您真正决定点击您的 Google 地图链接之前,无需拨打/需要拨打 Google。

如果您确实选择 iframe 方法(同样,不推荐用于任何 Google 网站),请注意您需要 adjust the URL to tell Google to allow iframing 并且纬度/经度信息将位于不同的部分页面。

【讨论】:

猜你喜欢
  • 2016-08-29
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2010-11-22
相关资源
最近更新 更多