Created
August 12, 2012 12:53
-
-
Save vjk2005/3331714 to your computer and use it in GitHub Desktop.
Load any number of pages via AJAX.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
DeepLoad: | |
Fetch any number of pages via AJAX for infinite scroll-like functionality. | |
First visit the site you want and put this into your browser console. | |
ToDo: | |
0. Determing page structure (where in the url should the page numbers need to be plugged in) from pages where the url doesn't have page structure yet (eg: homepage) | |
1. User can customize by passing arguments to the "get" function | |
2. resume a broken DeepLoad. Find if DOM has been modified by a previous DeepLoad run and start resuming from where the previous run left off | |
3. remove jquery dependency | |
4. convert it into a bookmarklet | |
5. make a youtube video tutorial and spread word about DeepLoad around | |
*/ | |
var s = document.createElement( 'script' ); | |
s.src = 'http://code.jquery.com/jquery-1.8.0.min.js'; | |
document.body.appendChild( s ); // load jQuery | |
var | |
targets = [] | |
, url = window.location.toString() | |
, m = url.match( /\d+/g ) | |
, start = m[ m.length - 1 ] | |
, x = 1 + +start // "40" becomes 1 + 40 = 41. string conv + 1 basically | |
, n = url.replace( start, x ) // next url | |
, g = $( 'a[href*="' + x + '"]' ); | |
for( i=0; i < g.length; i++ ) { | |
if( g[i].href.toString().indexOf( n ) != -1 ) { | |
targets.push( Math.max.apply( Math, $( g[i] ).parent().text().match( /(\d+)/g ) || $( g[i] ).parent().parent().text().match( /(\d+)/g ) ) ); | |
} | |
} | |
var target = Math.max.apply( Math, targets ); | |
function get( id ) { | |
console.log( 'fetching ' + id ); | |
$.get( url.replace( start, id ), function( data ) { | |
$( 'body' ).append( data ) | |
id++; | |
if( id <= target ) get( id ); | |
}); | |
} | |
get( x ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While ditching jQuery and going native JS is an obvious way to optimize and improve this script, other tips are also welcome.