Skip to content

Instantly share code, notes, and snippets.

@tim-smart
Created March 11, 2010 11:19
Show Gist options
  • Select an option

  • Save tim-smart/329046 to your computer and use it in GitHub Desktop.

Select an option

Save tim-smart/329046 to your computer and use it in GitHub Desktop.
var Wrapper, postToPastebin, wrapper;
// This is a concept for making a script wrapper with a userscript and pastebin.ca
Wrapper = function Wrapper(scriptId) {
this.id = scriptId;
return this;
};
// Will contain / contains the meta data
Wrapper.prototype.meta = null;
// Contains content waiting to be injected
Wrapper.prototype.queue = [];
// This functions grabs a copy of the meta.js
Wrapper.prototype.fetchMeta = function fetchMeta(callback) {
GM_xmlhttpRequest({
method: 'GET',
url: "http://userscripts.org/scripts/source/" + this.id + ".meta.js",
onload: (function(__this) {
return (function onload() {
return (function(xhr) {
this.meta = xhr.responseText.trim();
return callback(this.meta);
}).apply(__this, arguments);
});
})(this)
});
};
// Takes a comment, and adds it to the inject queue
Wrapper.prototype.queueComment = function queueComment(comment) {
this.queue.push("// " + comment);
return this;
};
// This function takes a key / value pair, converts it to a metadata string,
// then adds it to the inject queue
Wrapper.prototype.queueMetadata = function queueMetadata(key, value) {
this.queue.push("// @" + key + " " + value);
return this;
};
// This function takes the pending changes, and applies it to the metadata
Wrapper.prototype.injectQueue = function injectQueue() {
var end, temp;
if ('string' !== typeof this.meta) {
return false;
}
temp = this.meta.split("\n");
end = temp.pop();
Array.prototype.push.apply(temp, this.queue);
this.queue = [];
temp.push(end);
this.meta = temp.join("\n");
return this;
};
// This function takes the meta, and puts an closure around it
Wrapper.prototype.wrapWithFunction = function wrapWithFunction() {
return "(function() {\n" + this.meta + "\n})();";
};
//### Helper functions
// This functions takes some code and a callback, sends it to pastebin.ca, then calls
// the callback when finished with the paste ID as the first and only argument
postToPastebin = function postToPastebin(code, callback) {
return GM_xmlhttpRequest({
url: 'http://pastebin.ca/quiet-paste.php?api=TQnk/tF+KeBcNWKL47BNh+d5Qywio6ko',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: "content=" + encodeURIComponent(code) + "&expiry=" +
encodeURIComponent('5 minutes') + "&type=27",
onload: function onload(xhr) {
var result;
result = xhr.responseText.split(':');
return callback('SUCCESS' === result[0] ? result[1] : null);
}
});
};
//### Usage
wrapper = new Wrapper(12345);
wrapper.fetchMeta(function() {
wrapper.queueComment('').
queueComment('This wrapper can be used for installing updaters with scripts').
queueMetadata('attribution', 'Marti').
queueMetadata('attribution', 'Jesse Andrews').
injectQueue();
postToPastebin(wrapper.wrapWithFunction(), function(pasteId) {
GM_openInTab("http://pastebin.ca/raw/" + pasteId + "#.meta.js");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment