/*
* @title AutoPagerize keywordblog
* @description 「○○に関するブログ記事まとめ」 (http://hatenablog.com/k/keywordblog/○○) 用のAutoPagerize
* @include http://hatenablog.com/k/keywordblog/*
* @license reblog commons license ( cc by 3.0 )
*/
(function() {
// ↓↓↓なんか読み込んだタイミングで一番上に戻ってしまうので
$(document).off('scroll');
$(window).off('resize');
$('#relatedblog-form-multiplied, #relatedblog-form-narrowed').off('submit');
// ↑↑↑
var start_url = location.href;
var State = true;
var BASE_REMAIN_HEIGHT = 400;
var loading = false;
var last_element = (function() {
var a = document.querySelectorAll('#related-blog-body article.article');
return a[a.length - 1];
}());
if (!last_element) {
return error();
}
var insert_point = last_element.nextSibling;
var insert_parent = last_element.parentNode;
var next = document.querySelector('#pager-bottom a.pager-next').href;
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;
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(/<\/?(?: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);
next = get_next_page_link(htmlDoc);
if (!docs.length) {
error();
return next_page_load();
}
target_rewrite(docs);
docs.reverse();
docs = docs.map(function(doc) {
return insert_parent.insertBefore(document.importNode(doc, true), insert_point);
});
if (document.querySelector('#related-blog-body').offsetHeight < window.innerHeight) {
next_page_load();
}
return loading = false;
}
function get_next_elements(doc) {
var content = doc.querySelectorAll('#related-blog-body article.article');
return Array.apply(null, content);
}
function get_next_page_link(doc) {
return /\/k\/keywordblog\/[^"]*/.exec(doc.querySelector('#pager-bottom p.pager-inner').innerHTML);
}
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 error() {
return console.log('error');
State = false;
}
function terminate() {
return console.log('terminate');
State = false;
}
return true;
})();