Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use GM_xmlhttpRequest to POST data on Chrome?

I'm writing a user script to take an image from a page, and upload it to a server. The script works fine in FF (Greasemonkey and Scriptish), but when I use Chrome (using Tampermonkey or Ninjakit), it does not send the data, it sends the string * [object Object] * instead.

Here is my script:

// ==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: "http://myserver.org/mysscript.py",
    headers: {
    "Content-Type": "multipart/form-data"
    },
    data:_data,
    onload: function(response) {
             console.log ("gut response");
         $("#input").get()[0].value=response.responseText;
    }
    });
}


Both Tampermonkey and Ninjakit do send the request. In Tampermonkey, I get a response, in Ninjakit I don't (onload is never called).

But they do not send the actual image encoded with base64 - when I read the data - the server gets [object Object] as the POST body (Instead of data body, I can't get devtools network panel to show requests made by GM_xmlhttpRequest, so I checked it on the server side).

like image 744
Ohad Cohen Avatar asked Oct 16 '13 01:10

Ohad Cohen


People also ask

What is the use of XMLHttpRequest?

All modern browsers have a built-in XMLHttpRequest object to request data from a server. All major browsers have a built-in XML parser to access and manipulate XML. The XMLHttpRequest object can be used to request data from a web server.

How to send post data in JavaScript with XMLHttpRequest?

To send post data in JavaScript with XMLHTTPRequest, first, we have to create an XMLHTTPRequest object: After that initialize it with the open () method with the request URL. We also pass the method “post” and set the asynchronous to true. Below is the code: Now set the content type urlencoded using the setRequestHeader method:

Can I use XMLHttpRequest in Manifest V3?

In Manifest V3, XMLHttpRequest is not supported in background pages (provided by Service Workers). Please consider using its modern replacement, fetch (). Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy.

How do I use cross site XMLHttpRequest?

Cross-site XMLHttpRequest. Modern browsers support cross-site requests by implementing the Cross-Origin Resource Sharing (CORS) standard. As long as the server is configured to allow requests from your web application's origin, XMLHttpRequest will work. Otherwise, an INVALID_ACCESS_ERR exception is thrown.


1 Answers

It might be that FormData and multipart/form-data are not well supported on those platforms. Need to look into it more (later).

Meanwhile, try the more typical approach; use application/x-www-form-urlencoded or JSON.

EG:

GM_xmlhttpRequest ( {
    method:     "POST",
    url:        "http://myserver.org/mysscript.py",
    data:       "image64=" + encodeURIComponent (img64),
    headers:    {
        "Content-Type": "application/x-www-form-urlencoded"
    },
    onload:     function (response) {
        console.log ("gut response");
        $("#input").get()[0].value=response.responseText;
    }
} );
like image 77
Brock Adams Avatar answered Oct 27 '22 00:10

Brock Adams