Javascript GM.xmlHttpRequest:无法理解URL
我想尝试stackoverflow,但出现了一个错误:Javascript GM.xmlHttpRequest:无法理解URL,javascript,greasemonkey-4,Javascript,Greasemonkey 4,我想尝试stackoverflow,但出现了一个错误: Script error: Error: "GM.xmlHttpRequest : Could not understand the URL : https://acqua.kmi.open.ac.uk/predict TypeError: URL is not a constructor 我认为它是“acqua.kmi.open.ac.uk”,有很多子域名。我怎样才能修好它 用户脚本代码: // ==UserScript== // @
Script error:
Error: "GM.xmlHttpRequest : Could not understand the URL :
https://acqua.kmi.open.ac.uk/predict
TypeError: URL is not a constructor
我认为它是“acqua.kmi.open.ac.uk”,有很多子域名。我怎样才能修好它
用户脚本代码:
// ==UserScript==
// @name ACQUA: StackExchange best answer predictor
// @namespace https://acqua.kmi.open.ac.uk/
// @description G. Gkotsis, K. Stepanyan, C. Pedrinaci, J. Domingue, and M. Liakata. It's all in the Content: State of the art Best Answer Prediction based on Discretisation of Shallow Linguistic Features. In Proceedings of the 2014 ACM Conference on Web Science, WebSci '14, pages 202-210, New York, NY, USA, 2014. ACM.
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @include http*://*.stackexchange.com/questions/*
// @include http*://stackoverflow.com/questions/*
// @include http*://askubuntu.com/questions/*
// @include http*://mathoverflow.net/questions/*
// @include http*://serverfault.com/questions/*
// @include http*://superuser.com/questions/*
// @updateURL https://acqua.kmi.open.ac.uk/page/js/bestAnswer.user.js
// @version 1.4
// @run-at document-end
// @grant GM.xmlHttpRequest
// ==/UserScript==
// GM_getResourceURL
URL = "https://acqua.kmi.open.ac.uk/predict"; //?URL=" + encodeURIComponent(document.URL);
GM.xmlHttpRequest({
method: "GET",
url: URL,
onload: function(xhr) {
id = "#answer-"+xhr.responseText;
var num = parseInt(xhr.responseText) || 0;
if (num==0){
$("#header").append("<div style='display:-moz-inline-stack;display:inline-block;background:url(https://acqua.kmi.open.ac.uk/page/img/acqua-icon-white.png) 5px 8px no-repeat #9a3334;margin:-15px 0;font-size:medium;padding:12px 10px 6px 34px;color:#fff;zoom:1;*display:inline;'>Acqua: You need to <a href='https://acqua.kmi.open.ac.uk/authenticate' style='color:#fff;border-bottom:1px solid #fff;' target=='_new' title='Authenticate'>authenticate</a></div>");
}
else
$("#header").append("<div style='display:-moz-inline-stack;display:inline-block;background:url(https://acqua.kmi.open.ac.uk/page/img/acqua-icon-white.png) 5px 8px no-repeat #003366;margin:-15px 0;font-size:medium;padding:12px 10px 6px 34px;color:#fff;zoom:1;*display:inline;'>Acqua is loaded</div>(<a href='#"+num+"'>jump to the answer</a>)");
$(id).prepend( "<div style='display:block;text-align:right;padding:20px 15px 0 0'><a href='https://acqua.kmi.open.ac.uk' title='ACQUA' target='_blank'><img src='https://acqua.kmi.open.ac.uk/page/img/acqua-icon.png' alt='ACQUA icon'></a></div>");
$(id).css({"border-color": "#1b75bb",
"border-width":"5px",
"border-style":"solid"});
$(id).append("<span style='background:url(https://acqua.kmi.open.ac.uk/page/img/acqua-icon-white.png) #1b75bb no-repeat 15px 0px;display:block;color:#fff;padding:5px 15px 5px 45px;float: right;' id='acquafeedback'>ACQUA: Did you find this useful? <a id='acquayes' style='background:#003366;margin-left:6px;padding:2px 2px;color:#fff'>Yes</a> <a id='acquano' style='background:#003366;margin-left:6px;padding:2px 2px;color:#fff'>No</a></span>");
$('#acquayes').hover(function(){
$(this).css('background','#9a3334');
}, function(){$(this).css('background','#003366');}
);
$('#acquano').hover(function(){
$(this).css('background','#9a3334');
}, function(){$(this).css('background','#003366');}
);
$("#acquayes").click (function () {
$.ajax({
url: "https://acqua.kmi.open.ac.uk/feedback?answer=yes&id="+xhr.responseText,
jsonp: "callback",
dataType: "jsonp",
data: {
q: "",
format: "json"
},
success: function( response ) {
;
}, error:function(response){$('#acquafeedback').html("Thank you!");}
});
});
$("#acquano").click (function () {
$.ajax({
url: "https://acqua.kmi.open.ac.uk/feedback?answer=no&id="+xhr.responseText,
jsonp: "callback",
dataType: "jsonp",
data: {
q: "",
format: "json"
},
success: function( response ) {
;
}, error:function(response){$('#acquafeedback').html("Thank you!");}
});
});
}
});
/==UserScript==
//@name ACQUA:StackExchange最佳答案预测器
//@名称空间https://acqua.kmi.open.ac.uk/
//@description G.Gkotsis、K.Stepanyan、C.Pedrinaci、J.Domingue和M.Liakata。全部内容包括:基于浅层语言特征离散化的最新最佳答案预测。2014年ACM网络科学会议记录,WebSci'14,第202-210页,美国纽约州纽约市,2014年。ACM。
//@需要http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
//@include http*://*。stackexchange.com/questions/*
//@include http://stackoverflow.com/questions/*
//@include http://askubuntu.com/questions/*
//@include http://mathoverflow.net/questions/*
//@include http://serverfault.com/questions/*
//@include http://superuser.com/questions/*
//@updateURLhttps://acqua.kmi.open.ac.uk/page/js/bestAnswer.user.js
//@version 1.4
//@在文档末尾运行
//@grant GM.xmlHttpRequest
//==/UserScript==
//GM_getResourceURL
URL=”https://acqua.kmi.open.ac.uk/predict"; //?URL=“+encodeURIComponent(document.URL);
GM.xmlHttpRequest({
方法:“获取”,
url:url,
onload:函数(xhr){
id=“#答案-”+xhr.responseText;
var num=parseInt(xhr.responseText)| 0;
如果(num==0){
$(“#头”).append(“Acqua:youneedto”);
}
其他的
$(“#头”).append(“Acqua已加载()”);
$(id)。前缀(“”);
$(id).css({“边框颜色”:“#1b75bb”,
“边框宽度”:“5px”,
“边框样式”:“实心”});
$(id).append(“ACQUA:你觉得这个有用吗?是不是”);
$('#acquayes')。悬停(函数(){
$(this.css('background','#9a3334');
},function(){$(this).css('background','#003366');}
);
$('#acquano')。悬停(函数(){
$(this.css('background','#9a3334');
},function(){$(this).css('background','#003366');}
);
$(“#acquayes”)。单击(函数(){
$.ajax({
url:“https://acqua.kmi.open.ac.uk/feedback?answer=yes&id=“+xhr.responseText,
jsonp:“回调”,
数据类型:“jsonp”,
数据:{
q:“,
格式:“json”
},
成功:功能(响应){
;
},错误:函数(响应){$('acquafeedback').html(“谢谢!”)}
});
});
$(“#acquano”)。单击(函数(){
$.ajax({
url:“https://acqua.kmi.open.ac.uk/feedback?answer=no&id=“+xhr.responseText,
jsonp:“回调”,
数据类型:“jsonp”,
数据:{
q:“,
格式:“json”
},
成功:功能(响应){
;
},错误:函数(响应){$('acquafeedback').html(“谢谢!”)}
});
});
}
});
我认为它是“acqua.kmi.open.ac.uk”,有很多子域
根本不是问题
URL
是现有JS接口的名称
将变量命名为不同的名称。URL
是一个
不要试图覆盖它
一个快速而肮脏的黑客将只是使用一个不同的名称
一个更好的解决方案是首先使用IIFE来避免创建新的全局
(function () {
var URL = "https://acqua.kmi.open.ac.uk/predict"; //?URL=" +
encodeURIComponent(document.URL);
GM.xmlHttpRequest({
method: "GET",
// etc ...
)();
这不是我的代码,我看不出来。我不明白他们如何在第一次运行它时不出现这个问题。我删除了URL变量。新的作用域没有用,userscript已经有了作用域。在这种情况下,添加“var”就够了。