// ==UserScript== // @name reddddit // @version 0.2 // @namespace http://bender.fesb.hr/~robert/ // @description Auto append things for reddit page // @include http://www.reddit.com/* // ==/UserScript== var PAGE_URL = "http://gist.github.com/591568", RAW_URL = "http://gist.github.com/raw/591568/" SCRIPT_URL = RAW_URL + "reddddit.user.js", UPDATE_DATE_URL = RAW_URL + "update_date", THING_PARENT = '.sitetable', THING = THING_PARENT + ' .thing', NEXTPREV = '.nextprev', NEXT = NEXTPREV + ' a[rel~="next"]'; //GM_setValue('exclude_promoted', true); if ( GM_getValue('exclude_promoted') ) { THING += ':not(.promoted)'; } // async auto-update setTimeout(function(){ var d = new Date(), today = d.getUTCFullYear()+'-'+d.getUTCMonth()+'-'+d.getUTCDate(), check_day = GM_getValue('last_version_check_day', ''); function onload( req ) { GM_setValue('last_version_check_day', today); var text = req.responseText; if ( !/^[\d\s\-]+$/.test(text) ) { return; } var update_day = text.replace(/\s*/g, ''); if ( check_day < update_day && window.confirm('New version of Reddddit script is available! Do you wont to install it now?') ) { window.location.href = SCRIPT_URL; } } if ( !check_day ) { // Don't bother on first day. GM_setValue('last_version_check_day', today); } else if ( check_day < today ) { GM_xmlhttpRequest({ method: 'GET', url: UPDATE_DATE_URL, onload: onload }); } }, 100); function all( a, b ) { return all._slice.call( b ? a.querySelectorAll( b ) : document.querySelectorAll( a ), 0 ); } all._slice = [].slice; function first( a, b ) { return b ? a.querySelector( b ) : document.querySelector( a ); } function last( a, b ) { a = all( a, b ); return a.length ? a[ a.length - 1 ] : null; } function move( a ) { var x = null; if ( a ) { if ( a.nodeType ) { a.parentNode && a.parentNode.removeChild( a ); x = a; } else if ( a[0] ) { x = ( a[0].ownerDocument || a[0] ).createDocumentFragment(); for ( var i = 0; a[i]; ++i ) { x.appendChild( a[i] ); } } } move.x = x; return move; } move.to = function( parent, next ) { return this.x && parent.insertBefore( this.x, next || null ); } move.inPlaceOf = function( node ) { return this.x ? node && node.parentNode.replaceChild( this.x, node ) : node && node.parentNode.removeChild( node ) && null; } if ( window === window.top && first(NEXTPREV) ) (function( history ){ var enabled = GM_getValue('enabled', true), working, delta = 600, start_time, next_url; var rbar = first('#header-bottom-right'); if ( rbar ) (function(){ function toggle( event ) { enabled = !enabled; GM_setValue( 'enabled', enabled ); this.textContent = ( enabled ? 'Disable' : 'Enable' ) + ' Auto Append'; event && event.preventDefault(); } var span = document.createElement('span'); span.innerHTML = ' (*)'; var a = all( span, 'a' ); a[0].addEventListener('click', toggle, false); enabled = !enabled; toggle.call( a[0] ); a[1].href = PAGE_URL; var sep = first( rbar, '.separator' ); sep && rbar.appendChild( sep.cloneNode(true) ); rbar.appendChild( span ); })(); function onreadystatechange( req ) { if ( req.readyState == 4 ) { working = false; if ( req.status == 200 ) { appendThingsFromHtml( req.responseText ); delta = Math.round( ( +new Date() - start_time ) * 0.4 ) + 250; delta = Math.min( Math.max( 400, delta ), 4000 ); history && history.replaceState( {}, "", next_url ); } else { last( NEXT + ' i' ).textContent = '(Loading Failed: ' + req.status + ' - ' + req.statusText + ')'; } } } function appendThingsFromHtml( html ) { var div = document.createElement('div'); div.innerHTML = html; move( all(div, 'script') ); move( all(div, THING) ).to( last(THING_PARENT) ); var new_next = first( div, NEXT ); var old_next = first( NEXT ); if ( !new_next ) { var last_id = last( THING ).className.match(/\bid-(\S+)/i)[1]; new_next = old_next.cloneNode(true); new_next.href = old_next.href.replace( /(\bafter=)[^\&]*/i, last_id ); } move( new_next ).inPlaceOf( old_next ); } document.addEventListener('scroll', function() { if ( working || !enabled ) { return; } if ( document.documentElement.scrollTop + window.innerHeight > document.body.offsetHeight - delta ) { var next = first( NEXT ); if ( !next ) return; working = true; start_time = +new Date(); next.innerHTML += ' (Loading...)'; next_url = next.href.toString() GM_xmlhttpRequest({ method: 'GET', url: next_url, onreadystatechange: onreadystatechange }); } }, false); })( window.history );