New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: GM_xmlhttpRequest setting responseType support #1834

Closed
gameclamp opened this Issue Nov 28, 2013 · 10 comments

Comments

Projects
None yet
6 participants
@gameclamp
Copy link

gameclamp commented Nov 28, 2013

I want to use GM_xmlhttpRequest to get an image and store it in indexeddb.The responseText should be blob.
https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/

@Spittie

This comment has been minimized.

Copy link

Spittie commented Feb 11, 2014

Hi, just wanting to say that I would love to have this feature :)

It's still planned for version 1.16? As this hasn't got any comment in 3 months.

@steelywing

This comment has been minimized.

Copy link

steelywing commented Mar 7, 2014

👍 I need this function too

@Spittie

This comment has been minimized.

Copy link

Spittie commented Mar 8, 2014

@steelywing That's how I ended up doing it (coffeescript, but should be easy to get):

GM_xmlhttpRequest {
  method: "GET",
  url: url,
  overrideMimeType: "text/plain; charset=x-user-defined",
  onload: (xhr) ->
    r = xhr.responseText
    data = new Uint8Array(r.length)
    i = 0
    while i < r.length
      data[i] = r.charCodeAt(i)
      i++

    return blob = new Blob([data], {type: "image/png"})
}

It's not nearly as nice as setting responseType to "blob" (and it's probably way slower as well), but it does work, at least for what I needed it (it will return a blob with the retrieved data).
(I've hardcoded the mime type, but it's not hard to get it from the response headers)

@steelywing

This comment has been minimized.

Copy link

steelywing commented Mar 8, 2014

@Spittie Thanks a lot ! it working.
I find the reference in MDN, I use charset=us-ascii before, it has some strange convertion making the file corrupt.

@gameclamp

This comment has been minimized.

Copy link

gameclamp commented Oct 12, 2014

I was using ver2.2 and firefox32,and set : responseType: 'blob' , but what I get in the onload() function is not blob object,just some text like

����(�Exif��II*�

my code:

GM_xmlhttpRequest({
    method:"GET",
    url:imgurl,
    responseType: 'blob',
    onload:function(res){
        console.log(res.response);
    }
})
@arantius

This comment has been minimized.

Copy link
Collaborator

arantius commented Oct 14, 2014

See #2032 .

@pendave

This comment has been minimized.

Copy link

pendave commented Oct 30, 2016

Can I set responseType : "arraybuffer" now?

@janekptacijarabaci

This comment has been minimized.

Copy link
Contributor

janekptacijarabaci commented Oct 30, 2016

Can I set responseType : "arraybuffer" now?

Yes. See also http://www.greasespot.net/2015/05/greasemonkey-32-release.html

@pendave

This comment has been minimized.

Copy link

pendave commented Oct 31, 2016

I try to rip out the email info from online PDF and firstly the whole text should be get.

So I find http://hublog.hubmed.org/archives/001948.html
and its nice example:
http://git.macropus.org/2011/11/pdftotext/example

I use Greasemonkey version 4.1.10 to do a cross domain version with GM_xmlhttpRequest.
I see responseType : "arraybuffer" should be working but I can't get the script doing right from XMLHttpRequest to GM_xmlhttpRequest.

Here's the script from that site, how can I make it working correctly?
var pdfsrc = window.location.href;
//using a sample PDF for test now
pdfsrc ='http://git.macropus.org/2011/11/pdftotext/example/journal.pone.0026738.pdf';
html = document.body.innerHTML +'<div style="position: absolute; left: 10px; top: 200px; color: white; z-index: 100;">Attention<br><iframe id="input" src="'+ pdfsrc +'"></iframe><br><iframe id="processor" src="http://hubgit.github.com/2011/11/pdftotext/"></iframe><br><div id="output"></div></div>';
document.body.innerHTML = html;
//document.getElementById("plugin").remove();
var input = document.getElementById("input");
var processor = document.getElementById("processor");
var output = document.getElementById("output");
// listen for messages from the processor
window.addEventListener("message", function(event){
if (event.source != processor.contentWindow) return;
switch (event.data){
// "ready" = the processor is ready, so fetch the PDF file
case "ready":
var xhr = new XMLHttpRequest();
//url: input.getAttribute("src"),
url: pdfsrc,
xhr.responseType = "arraybuffer";
xhr.onload = function(event) {
processor.contentWindow.postMessage(this.response, "*");
};
xhr.send();
break;
// anything else is the text of the PDF
default:
output.textContent = event.data.replace(/\s+/g, " ");
break;
}
}, true);

Especially here I update with

case "ready":
GM_xmlhttpRequest({
method: "GET",
url: input.getAttribute("src"),
onload: function(event) {
processor.contentWindow.postMessage(this.response, "*");
}
});
break;

qq 20161031104510

@janekptacijarabaci

This comment has been minimized.

Copy link
Contributor

janekptacijarabaci commented Oct 31, 2016

// ==UserScript==
// @name        Greasemonkey - issue - 1834
// @include     http://git.macropus.org/2011/11/pdftotext/example/
// @grant       GM_xmlhttpRequest
// ==/UserScript==

// 1 - GM_xmlhttpRequest(), 2 - XMLHttpRequest()
var what = 1;

var pdfsrc = window.location.href;
// using a sample PDF for test now
if (what == 1) {
  // pdfsrc = "http://www.cbu.edu.zm/downloads/pdf-sample.pdf";
  pdfsrc = "http://git.macropus.org/2011/11/pdftotext/example/journal.pone.0026738.pdf";
} else if (what == 2) {
  pdfsrc = "http://git.macropus.org/2011/11/pdftotext/example/journal.pone.0026738.pdf";
}
html = document.body.innerHTML + `
  <div style="position: absolute; left: 10px; top: 200px; color: white; z-index: 100;">
  Attention<br><iframe id="input" src="`+ pdfsrc +`"></iframe><br>
  <iframe id="processor" src="http://hubgit.github.com/2011/11/pdftotext/"></iframe><br>
  <div id="output"></div></div>
`;
document.body.innerHTML = html;
// document.getElementById("plugin").remove();
var input = document.getElementById("input");
var processor = document.getElementById("processor");
var output = document.getElementById("output");
// listen for messages from the processor
window.addEventListener("message", function (event) {
  if (event.source != processor.contentWindow) return;
  switch (event.data) {
    // "ready" = the processor is ready, so fetch the PDF file
    case "ready":
      if (what == 1) {
        GM_xmlhttpRequest({
          method: "GET",
          responseType: "arraybuffer",
          // url: input.getAttribute("src"),
          url: pdfsrc,
          onload: function (event) {
            var clonedArrayBuffer = new Uint8Array(event.response);
            var clonedArrayBufferStr = "";
            for (var i = 0, count = clonedArrayBuffer.byteLength; i < count; i++) {
              clonedArrayBufferStr += clonedArrayBuffer[i]
                  + (i == (count - 1) ? "" : " ");
            }
            console.log("GM_xmlhttpRequest():" + "\n" + clonedArrayBufferStr);
            processor.contentWindow.postMessage(event.response, "*");
          }
        });
      } else if (what == 2) {
        var xhr = new XMLHttpRequest();
        // var url = input.getAttribute("src");
        var url = pdfsrc;
        xhr.responseType = "arraybuffer";
        xhr.onload = function (event) {
          var clonedArrayBuffer = new Uint8Array(this.response);
          var clonedArrayBufferStr = "";
          for (var i = 0, count = clonedArrayBuffer.byteLength; i < count; i++) {
            clonedArrayBufferStr += clonedArrayBuffer[i]
                + (i == (count - 1) ? "" : " ");
          }
          console.log("XMLHttRequest():" + "\n" + clonedArrayBufferStr);
          processor.contentWindow.postMessage(this.response, "*");
        };
        xhr.open("GET", url);
        xhr.send();
      } else {
        console.log("NOTHING!");
      }
      break; 
    // anything else is the text of the PDF
    default:
      output.textContent = event.data.replace(/\s+/g, " ");
      break;
  }
}, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment