【问题标题】:GM.xmlHttpRequest: Received no URL ErrorGM.xmlHttpRequest:没有收到 URL 错误
【发布时间】:2018-04-28 17:06:52
【问题描述】:

这个脚本运行在a Amazon deals page:

// ==UserScript==
// @name     Unnamed Script 138015
// @version  1
//@include     http://www.amazon.in/*
// @grant       GM_setValue   
// @grant       GM_getValue  
// @grant       GM.xmlHttpRequest
// @grant       GM_xmlhttpRequest
// @grant       GM.addStyle
// @grant       GM.getResourceText
// @grant       GM.getValue
// @grant       GM.setValue
// @grant       GM.info
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// ==/UserScript==
var x=document.getElementById("dealTitle"); //fetches the URL
alert(x);
GM.xmlHttpRequest ( {
    method: "GET",
    url: x,
    onload: function (response) {
        var parser  = new DOMParser ();
        var doc         = parser.parseFromString (response.responseText, "text/html");
        var price  = document.getElementsByClassName("a-size-medium a-color-price")[0];
        $("body").prepend ('<h1>' + price + '</h1>');
    },
    onerror: function (e) {
        console.error ('**** error ', e);
    },
    onabort: function (e) {
        console.error ('**** abort ', e);
    },
    ontimeout: function (e) {
        console.error ('**** timeout ', e);
    }
} );

它在控制台日志中显示以下错误。

Error: GM.xmlHttpRequest: Received no URL.
Stack trace:
GM_xmlHttpRequest@user-script:null/Unnamed%20Script%20138015:572:21
userScript@user-script:null/Unnamed%20Script%20138015:504:1
scopeWrapper@user-script:null/Unnamed%20Script%20138015:632:9
@user-script:null/Unnamed%20Script%20138015:487:17

有什么方法可以使用从页面获取的链接在HttpRequest中发送?

【问题讨论】:

    标签: xmlhttprequest gm-xmlhttprequest greasemonkey-4


    【解决方案1】:
    1. getElementById 不返回 URL;它返回一个节点。
    2. dealTitle 元素是通过 AJAX 添加的;当您的脚本首次运行时它不存在。因此,您需要在脚本中使用 AJAX 感知技术。

    这样的事情应该可以工作:

    // ==UserScript==
    // @name        Unnamed Script 138015
    // @version     2
    // @match       *://www.amazon.in/*
    // @grant       GM.xmlHttpRequest
    // @grant       GM_xmlhttpRequest
    // @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
    // ==/UserScript==
    
    waitForKeyElements ("#dealTitle", fetchDealPage);
    
    function fetchDealPage (jNode) {
        var dURL = jNode.attr ("href");
        console.log ("dURL: ", dURL);
    
        GM.xmlHttpRequest ( {
            method: "GET",
            url: dURL,
        //etc...
    }
    

    虽然您可能必须安装 Tampermonkey,并将行更改为 GM_xmlhttpRequest ( {
    不确定 GM4 是否支持以这种方式使用 GM.xmlHttpRequest

    【讨论】:

    • 你好布洛克,谢谢你的帮助,它成功了。我还有一个问题,因为您可能已经注意到每笔交易都有 ID dealTitle,所以在我完成第一个交易后,您能告诉我一种获取下一笔交易 URL 的方法吗?再次感谢。 :)
    • 这有点复杂,因为您需要调整选择器并链接或排队 XML 调用。这两个主题都在此处的其他问题中讨论。但是,如果您不知道如何让它工作,请打开一个新问题。
    • 哦,试试看或发布一个新问题。
    猜你喜欢
    • 2013-06-20
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多