Created
May 1, 2009 01:55
-
-
Save os0x/104812 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ==UserScript== | |
// @name twitter.AutoPager | |
// @namespace http://ss-o.net/ | |
// @version 0.2 | |
// @include http://twitter.com/* | |
// @include https://twitter.com/* | |
// ==/UserScript== | |
// Thx! id:Constellation | |
(function autopager(unsafeWindow,loaded){ | |
if (!loaded && window.opera && document.readyState == 'interactive') { | |
document.addEventListener('DOMContentLoaded', function(){autopager(unsafeWindow,true);}, false); | |
return; | |
} | |
var state = true; | |
var remainHeight = 400; | |
var loading = false; | |
var win = this.contentWindow || window; | |
var filters = []; | |
if (!win.AutoPagerize) { | |
win.AutoPagerize = { | |
addFilter:function(f) { | |
filters.push(f); | |
} | |
}; | |
} | |
var ev = document.createEvent('Event'); | |
ev.initEvent('GM_AutoPagerizeLoaded', true, true); | |
document.dispatchEvent(ev); | |
var last = get_last_line(); | |
var filter = function(){ | |
loading = false; | |
var docs = get_inserted_line(last.id); | |
filters.forEach(function(f){ | |
f(docs); | |
}); | |
last = get_last_line(); | |
}; | |
if (typeof unsafeWindow.onPageChange === 'function') { | |
var _onPageChange = unsafeWindow.onPageChange; | |
unsafeWindow.onPageChange = function(){ | |
_onPageChange(); | |
filter(); | |
}; | |
} else { | |
unsafeWindow.onPageChange = filter; | |
} | |
window.addEventListener('scroll', function(){ | |
if (loading) return; | |
var remain = document.documentElement.scrollHeight - window.innerHeight - window.pageYOffset; | |
if (state && remain < remainHeight) | |
click_more(); | |
}, false); | |
function click_more(){ | |
var event = document.createEvent('MouseEvents'); | |
event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | |
get_more().dispatchEvent(event); | |
loading = true; | |
} | |
function get_more(){ | |
return document.getElementById('more'); | |
} | |
function get_last_line(){ | |
return document.evaluate('id("timeline")/li[last()]',document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue; | |
} | |
function get_inserted_line(id){ | |
var x = 'id("timeline")/li[preceding-sibling::li[@id="'+id+'"]]'; | |
var res=[],r = document.evaluate(x,document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); | |
for (var i = 0,l = r.snapshotLength;i<l;i++) res.push(r.snapshotItem(i)); | |
return res; | |
} | |
})(this.contentWindow||this.unsafeWindow||window,0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment