我在以下用户脚本代码中得到一个ReferenceError:
// ==UserScript==
// @name ...
// @namespace ...
// @description ...
// @include ...
// @grant GM_xmlhttpRequest
// ==/UserScript==
console.log(GM_info);
try
{
console.log(GM_xmlhttpRequest({ method: "GET", url: "http://google.ca/", synchronous: true }).readyState);
}
catch (e)
{
console.log(e);
}
...
Run Code Online (Sandbox Code Playgroud)
它首先GM_info成功记录,然后记录ReferenceError.(我正在使用Firefox/Firebug.)
ReferenceError:未定义GM_xmlhttpRequest
为什么我会收到此错误?
我正在编写一个用户脚本,以从页面获取图像并将其上传到服务器。该脚本在FF(Greasemonkey和Scriptish)中运行良好,但是当我使用Chrome(使用Tampermonkey或Ninjakit)时,它不发送数据,而是发送字符串* [object Object] *。
这是我的脚本:
// ==UserScript==
// @id myid
// @name myname
// @version 1.0
// @namespace ohadcn
// @author Ohad Cohen
// @description mydescription
// @include https://*
// @grant GM_xmlhttpRequest
// @require https://code.jquery.com/jquery-2.0.3.min.js
// @run-at document-end
// ==/UserScript==
function getBase64Image(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
img=$("img[alt=myImage]").get(0);
img.onload=function(){
var img64=getBase64Image(img)
var _data=new FormData();
_data.append("image64",img64);
GM_xmlhttpRequest({
method: "POST",
url: …Run Code Online (Sandbox Code Playgroud) greasemonkey google-chrome gm-xmlhttprequest tampermonkey ninjakit
如下所示:
我有一个异步的脚本.我想传递一个值INTO这个函数,这样当调用onload函数时我可以用它来显示在网页中.
我遇到的挑战是,每次将其传递给函数时,此值都会发生变化.
所以,例如,如果我传入'abc','def','xyz'.
我最终会
xyz
xyz
xyz
Run Code Online (Sandbox Code Playgroud)
代替
abc
def
xyz
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是,如何将值传递给此函数,以便函数的每次调用都知道完成后要显示的内容?
我试图接到一个GM_xmlhttpRequest同步行为的电话,但我不能像我期望的那样让它工作:
function myFunction (arg) {
var a;
GM_xmlhttpRequest ( {
method: "GET",
url: "http://example.com/sample/url",
synchronous: true,
onload: function (details) {
a = details.responseText;
}
} );
return a;
}
b = myFunction ();
alert (b);
Run Code Online (Sandbox Code Playgroud)
我从来没有得到任何回报b; 这是未定义的.我在这里缺少一些步骤吗?
我使用的是Greasemonkey的v0.9.13和Firefox的v9.0.1.
我正在运行GM_xmlhttpRequest(使用Greasemonkey脚本),并将其存储responseText到新创建的HTML元素中:
var responseHTML = document.createElement('HTML');
...
onload: function() { responseHTML.innerHTML = response.responseText; }
Run Code Online (Sandbox Code Playgroud)
然后我试图在中找到一个元素responseHTML:
console.log(responseHTML.getElementsByTagName('div'));
console.log(responseHTML.getElementById('result_0'));
Run Code Online (Sandbox Code Playgroud)
第一个工作正常,但第二个则不行。有任何想法吗?
javascript greasemonkey xmlhttprequest getelementbyid gm-xmlhttprequest
我的脚本不起作用.AJAX调用没有发生.为什么?
// ==UserScript==
// @name prova
// @namespace http://blogpagliaccio.wordpress.com/
// @description prova
// @include http://*
// @version 1
// @grant GM_xmlhttpRequest
// @require http://userscripts.org/scripts/source/85398.user.js
// ==/UserScript==
// [........... other code]
console.log('start ajax call...');
GM_xmlhttpRequest({
method: "POST",
url: "www.prova.it",
data: {parametro:parametro},
onload: function(response) {
console.log(response.responseText);
},
onerror: function(reponse) {
alert('error');
console.log(reponse);
}
});
Run Code Online (Sandbox Code Playgroud)
我在一个@grant指令中列出了API函数,但是我没有看到AJAX调用和响应.