endless summer on dsbd
by
taizooo
2014-09-01 [2014/09/01 20:21:47]
promotion version (Exponential Endless Summer)
@@ -93,7 +93,7 @@
}
function get_next_elements(doc) {
- var r = doc.evaluate('id("posts")/li[starts-with(@class,"post_")]', doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
+ var r = doc.evaluate('id("posts")/li[div[starts-with(@id, "post_")]]', doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0, l = r.snapshotLength, res = new Array(l); i < l; i++) res[i] = r.snapshotItem(i);
return res;
}
/*
* @title endless summer on dsbd
* @description promotion version (Exponential Endless Summer)
* @include http://www.tumblr.com/dashboard*
* @include http://www.tumblr.com/show*
* @include http://www.tumblr.com/blog*
* @license reblog commons license ( cc by 3.0 )
*/
// bookmark
// autopager part from "twitter.AutoPager" : http://userscripts.org/scripts/review/47998
// random choice part from ReblogMachine : http://reblog.machine.mamemomonga.com/
// -> ReblogMachine.endless_summer#choice : http://reblog.machine.mamemomonga.com/js/ReblogMachine.js
// history.pushState/history.replaceState : http://d.hatena.ne.jp/javascripter/20100404/1270411268
// show page regexp from "Tumblr Dashboard Filter by Post Type" : http://userscripts.org/scripts/review/40794
// Exponential Endless Summer by saitamanodoruji : http://tumblr.g.hatena.ne.jp/saitamanodoruji/20120604/1338776265
(function () {
var num = 2;
var start_url = location.href;
var oldest_id = 264102; // http://ku.tumblr.com/post/264102
var latest_id = get_latest_id(document);
latest_id = (latest_id > 20000000000) ? latest_id : 20000000000;
console.log('latest_id: ' + latest_id);
var State = true;
var HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';
var BASE_REMAIN_HEIGHT = 10000;
var loading = false;
var last_element = get_next_elements(document).pop();
if (!last_element) {
return error();
}
insert_point = last_element.nextSibling;
insert_parent = last_element.parentNode;
next_page_load();
window.addEventListener('scroll', function () {
if (loading) return;
var remain = document.documentElement.scrollHeight - window.innerHeight - window.pageYOffset;
if (State && remain < BASE_REMAIN_HEIGHT) {
next_page_load();
}
}, false);
function next_page_load() {
loading = true;
var next = get_next(start_url);
history.pushState("", "", next);
console.log('next: ' + next);
var x = new XMLHttpRequest();
x.onload = function () {
if (x.status <= 200 && x.status < 300) {
load(x);
} else {
error();
next_page_load();
}
};
x.open('GET', next, true);
x.send(null);
}
function load(x) {
var html = x.responseText.replace(/<script(?:[ \t\r\n][^>]*)?>[\S\s]*?<\/script[ \t\r\n]*>|<\/?(?:i?frame|html|script|object)(?:[ \t\r\n][^<>]*)?>/gi, ' ');
var htmlDoc;
if (document.implementation.createHTMLDocument) {
htmlDoc = document.implementation.createHTMLDocument('hogehoge')
} else {
htmlDoc = document.cloneNode(false);
if (htmlDoc) {
htmlDoc.appendChild(htmlDoc.importNode(document.documentElement, false));
} else {
htmlDoc = document.implementation.createDocument(null, 'html', null);
}
}
var range = document.createRange();
range.selectNodeContents(document.documentElement);
htmlDoc.documentElement.appendChild(range.createContextualFragment(html));
var docs = get_next_elements(htmlDoc);
if (!docs.length) {
error();
return next_page_load();
}
target_rewrite(docs);
img_cache(docs);
docs = docs.map(function (doc) {
return insert_parent.insertBefore(document.importNode(doc, true), insert_point);
});
loading = false;
}
function get_next_elements(doc) {
var r = doc.evaluate('id("posts")/li[div[starts-with(@id, "post_")]]', doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0, l = r.snapshotLength, res = new Array(l); i < l; i++) res[i] = r.snapshotItem(i);
return res;
}
function target_rewrite(docs) {
docs.forEach(function (doc) {
var as = doc.getElementsByTagName('a');
for (var i = 0, l = as.length; i < l; i++) {
var a = as[i],
_a = a.getAttribute('href');
if (_a && !/^javascript:/.test(_a) && !/^#/.test(_a)) a.setAttribute('target', '_blank');
}
});
}
function img_cache(docs) {
docs.forEach(function (doc) {
var is = doc.getElementsByTagName('img');
for (var i = 0, l = is.length; i < l; i++) {
img = new Image();
img.src = is[i].src;
}
});
}
function error() {
return console.log('error');
State = false;
}
function terminate() {
return console.log('terminate');
State = false;
}
function get_latest_id(doc) {
return doc.evaluate('number(substring-after(id("posts")/li[starts-with(@id,"post_")]/@id,"post_"))', doc, null, XPathResult.NUMBER_TYPE, null).numberValue;
}
function get_next(url) {
var ramdom_id = draw(oldest_id, latest_id);
var x = /(?:\/dashboard|(?:\/blog\/[-\w]+)?\/show\/\w+|\/blog\/[-\w]+)/.exec(url)[0] + '/' + num + '?offset=' + ramdom_id;
num +=1;
return x;
}
/* Exponential Endless Summer by saitamanodoruji */
/* http://tumblr.g.hatena.ne.jp/saitamanodoruji/20120604/1338776265 */
/* http://saitamanodoruji.tumblr.com/exponentialEndlessSummer */
function draw(oldestID, newestID) {
var oldestUTime = getUnixTimeFromPostID(oldestID);
var newestUTime = getUnixTimeFromPostID(newestID);
var drawnUTime = oldestUTime + Math.floor((newestUTime - oldestUTime) * Math.random());
return getPostIDFromUnixTime(drawnUTime);
}
function getPostIDFromUnixTime(utime) {
if (utime < 1149465600000) {
return;
} else if (utime < 1183377600000) {
return Math.ceil(3.12 * Math.pow(10, -244) * Math.exp(4.865 * Math.pow(10, -10) * utime));
} else if (utime >= 1183377600000) {
return Math.ceil(7.768 * Math.pow(10, -22) * Math.exp(5.423 * Math.pow(10, -11) * utime));
}
}
function getUnixTimeFromPostID(postID) {
if (postID < 1) {
return;
} else if (postID < 4600108) {
return Math.floor(Math.log(postID / (3.12 * Math.pow(10, -244))) / (4.865 * Math.pow(10, -10)));
} else if (postID >= 4600108) {
return Math.floor(Math.log(postID / (7.768 * Math.pow(10, -22))) / (5.423 * Math.pow(10, -11)));
}
}
})()
- Permalink
- このページへの個別リンクです。
- RAW
- 書かれたコードへの直接のリンクです。
- Packed
- 文字列が圧縮された書かれたコードへのリンクです。
- Userscript
- Greasemonkey 等で利用する場合の .user.js へのリンクです。
- Loader
- @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
- Metadata
- コード中にコメントで @xxx と書かれたメタデータの JSON です。