此脚本在亚马逊交易页面上运行:
// ==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 中发送?