Binary library

By shoecream Last update Feb 21, 2010 — Installed 36,771 times.

Script Summary: Lets you download files as a binary stream, and upload them as multipart/form-data. Userscript library.



License: cc-by-sa

This script is a userscript library, and thus should only be included in other user scripts. Installing this won't really do anything.

How to use:

1. In your metadata block, add @require http://userscripts.org/scripts/source/69456.user.js
2. In your script scope, you will now have access to the BinaryRes object.

API:

function BinaryRes.get( details )
This method will retrieve a remote URL as a binary stream. It only takes one argument, the details object.

The following properties must be defined:
url: String The URL to send the request to. Must be an absolute URL.
callback: Function A function that will be called when the request has completed successfully. It is passed one argument, the Response object of GM_xmlhttpRequest fame.

The following property is optional:
override: Object Any properties defined here will override the predefined properties that are passed to the GM_xmlhttpRequest function.

function BinaryRes.post( details )
This method will perform a "file upload" POST request, like the kind seen here. It only takes one argument, the details object.

The following properties must be defined:
url: String The URL to send the request to. Must be an absolute URL.
callback: Function A function that will be called when the request has completed successfully. It is passed one argument, the Response object of GM_xmlhttpRequest fame.
data: Object The data to be posted. Each key of this object will be the name of the form element you are posting from (i.e. the name="" attribute). If the value of the property is a String, it is interpreted as regular form data. If the value of the property is an Object, it will be interpreted as a file upload. In that case, it needs the following properties:

value: String The binary data that will be posted to the URL. If you are using the complementary BinaryRes.get function, this will be the responseText property of the Response object passed to the callback after a successful GET request.
filename: String (optional) The file name of the data that you are posting.
type: String (optional) The MIME type of the data that you are posting. The BinaryRes.guessType( [byte stream] ) method will do some magic number guessing for the most basic of uses.

The post method also supports override exactly the same as the get method does.