Skip to content

Instantly share code, notes, and snippets.

@pounard
Last active August 29, 2015 14:12
Show Gist options
  • Save pounard/fb805285fe57836eecde to your computer and use it in GitHub Desktop.
Save pounard/fb805285fe57836eecde to your computer and use it in GitHub Desktop.
/*jslint browser: true, plusplus: true, indent: 2 */
/*global Node, console, Blob */
/**
* Et tu pleures. Et tu pleures. Et tu pleures. Mais ça marche.
*
* But how the fuck will I make this work?
* - Step 1: Open your firebug (or other) console
* - Step 2: Paste this code into it
* - Step 3: Click "Execute" (or maybe "Run")
* - Step 4: If everything went OK your browser will download the file
*/
(function (console) {
"use strict";
// Thanks http://stackoverflow.com/a/19818659
console.save = function (data, filename) {
if (!data) {
console.error('Console.save: No data');
return;
}
if (!filename) {
filename = 'console.json';
}
if (typeof data === "object") {
data = JSON.stringify(data, undefined, 4);
}
var
blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
};
}(console));
(function (document) {
"use strict";
// Thanks http://stackoverflow.com/a/281335
Array.prototype.clean = function () {
var i;
for (i = 0; i < this.length; i++) {
if ("string" !== typeof this[i] || /^\s*$/.test(this[i])) {
this.splice(i, 1);
i--;
}
}
return this;
};
// Our code
var
matches = document.querySelectorAll("#functions .function"),
current, match, key, akey, symbols = [], output, args, desc, junk, otherJunk, i;
if (matches) {
for (key in matches) {
match = matches[key];
if (match instanceof Node) {
desc = [];
junk = match.querySelectorAll(".searchable");
if (junk) {
for (akey in junk) {
desc.push(junk[akey].textContent);
}
desc = desc.clean().join("\n * ");
console.log(desc);
}
if (match.hasAttribute("cores")) { // Function
symbols.push(current = {
type: "function",
cores: match.getAttribute("cores"),
level: match.getAttribute("level"),
name: match.getAttribute("name"),
args: [],
return: null
});
junk = match.querySelector("h2.searchable").textContent;
junk = junk.split(/\(|\)/);
junk.shift();
if (junk.length) {
for (akey in junk) {
if (/\:/.test(junk[akey])) { // Gotcha return!
otherJunk = junk[akey].split(/\s+|\:/).clean();
current.return = [otherJunk[0], otherJunk[1]];
} else if ("string" === typeof junk[akey]) { // GotchARG !
otherJunk = junk[akey].split(/\s+|,/).clean();
while (1 < otherJunk.length) {
current.args.push([otherJunk.shift(), otherJunk.shift()]);
}
}
}
console.log(current);
}
// Find arguments
} else {
symbols.push(current = { // Constant
type: "const",
name: match.getAttribute("name"),
value: /\s+=\s+([\-0-9]*)/.exec(match.innerHTML)[1]
});
}
current.desc = desc;
}
}
if (symbols.length) {
output = "";
for (key in symbols) {
current = symbols[key];
if ("function" === current.type) {
args = [];
output += "\n/**\n";
if (current.desc) {
output += " * " + current.desc + "\n *\n";
}
output += " * @level " + current.level + "\n";
output += " * @cores " + current.cores + "\n";
if (current.args.length) {
output += " *\n";
for (akey in current.args) {
if ("undefined" !== typeof current.args[akey][0]) { // You never know
args.push(current.args[akey][1]);
output += " * @param " + current.args[akey][0] + " " + current.args[akey][1] + "\n";
}
}
}
if (current.return) {
output += " *\n * @return " + current.return[0] + "\n * " + current.return[1] + "\n";
}
output += " */\n";
output += "function " + current.name + "(" + args.clean().join(", ") + ") {}\n";
} else {
output += "\n/**\n";
if (current.desc) {
output += " * " + current.desc + "\n *\n";
}
output += " */\n";
output += "global " + current.name + " = " + current.value + ";\n";
}
}
console.save(output);
}
}
}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment