Skip to content

Instantly share code, notes, and snippets.

@jwoglom
Forked from 140bytes/LICENSE.txt
Last active January 3, 2016 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwoglom/8420339 to your computer and use it in GitHub Desktop.
Save jwoglom/8420339 to your computer and use it in GitHub Desktop.
g=function(c,x,i,a){i=i||0,a=a||[],e=function(h){a.push(h);g(c,x,++i,a)},i==c.length?x(a):((d=(f=c[i]).src)?$.get(d,e):e(f.innerHTML))}

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

// Given c=An array of script objects (from querySelectorAll/getElementsByTagName) and x=callback function
g = function (c, x, i, a) {
i = i || 0, // set the counter i
a = a || [], // define the array a
e = function (h) { // set up a function to recur to the next value
a.push(h); // by pushing the result to the array
g(c, x, ++i, a) // and incrementing i
};
i == c.length ? x(a) : // If it's the last element in the array, run the callback
((d = (f = c[i]).src) ? // Otherwise, if there's a src attribute
$.get(d, e) : // get it's source with a jQuery (or alternate) GET function and recur
e(f.innerHTML)) // if not, recur with it's innerHTML
}
g=function(c,x,i,a){i=i||0,a=a||[],e=function(h){a.push(h);g(c,x,++i,a)},i==c.length?x(a):((d=(f=c[i]).src)?$.get(d,e):e(f.innerHTML))}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 James Woglom <wogloms.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "getInlineResources",
"description": "This code snippet loops through a given list of elements and compiles their inline sources and remote sources into an array.",
"keywords": [
"ajax",
"array",
"inline",
"resources"
]
}
// some javascript code
var z = 5;
var y = function() { alert('Hi!'); }
<!DOCTYPE html>
<title>Foo</title>
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="somescriptfile.js"></script>
<script src="index.js"></script>
<script>/* some inline code */ var x = 5; </script>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
getInlineResources = g;
getInlineResources(document.querySelectorAll('script'), function(scrs) {
console.log('There are '+scrs.length+' JS blocks on this page.');
console.log(scrs);
});
</script>
@atk
Copy link

atk commented Jan 16, 2014

Using $.get feels a bit like cheating, otherwise it's a nice idea. A recursive approach might save you some bytes (you can maybe use them to use a JSONp request service to get the file):

g=function(c,x,a){a=a||[];return c.length?(a.push($.get(c[0].src)||c[0].innerHTML),g([].slice.call(c,1),x,a)):x(a)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment