endless summer on anond (today -> past year)
by
yuta25
2014-12-21 [2014/12/21 23:59:42]
昔の今日の増田ページをランダムでAutoPagerize風
@@ -19,7 +19,6 @@
var State = true;
var BASE_REMAIN_HEIGHT = 10000;
var loading = false;
- var visited = [];
var last_element = (function() {
var a = get_next_elements(document);
@@ -34,6 +33,20 @@
var insert_point = last_element.nextSibling;
var insert_parent = last_element.parentNode;
var next_day;
+ var next_year;
+
+ var today = new Date();
+ var month = today.toISOString().slice(5,7);
+ var day = today.toISOString().slice(8, 10);
+ var thisYear = today.getYear();
+ var bottomYear = (today.getMonth() < 9 || (today.getMonth() === 10 && today.getDay() < 24)) ? 107 : 106;
+ var pages = new Array(thisYear - bottomYear + 1);
+ for (var i = 0; i < pages.length; i++) {
+ pages[i] = {
+ day: '/' + (1900 + bottomYear + i) + month + day,
+ page: null
+ };
+ }
next_day_load();
@@ -46,52 +59,61 @@
}, false);
function next_day_load() {
- var today = new Date();
- var thisYear = today.getYear();
- // var bottomYear = (new Date("September 24, 2006")).getYear();
- var bottomYear = (today.getMonth() < 9 || (today.getMonth() === 10 && today.getDay() < 24)) ? 107 : 106;
- var nextYear = 1900 + bottomYear + Math.floor(Math.random() * (thisYear - bottomYear + 1));
- next_day = '/' + nextYear + today.toISOString().slice(5,7) + today.toISOString().slice(8, 10);
+ loading = true;
+ if (!pages.length) return;
+ next_year = Math.floor(Math.random() * pages.length);
+ next_day = pages[next_year].day;
+ if (pages[next_year].page) {
+ next_page_load();
+ } else {
+ var x = new XMLHttpRequest();
+ x.onload = function(e) {
+ if (x.status <= 200 && x.status < 300) {
+ next_page_load(e);
+ } else {
+ error();
+ next_day_load();
+ }
+ };
+ x.open('GET', next_day, true);
+ x.responseType = 'document';
+ x.send(null);
+ }
+ }
+
+ function next_page_load(e) {
+ if (e) {
+ var doc = e.target.response;
+ var pager_r = doc.querySelectorAll('.pager-r');
+ var max = pager_r[pager_r.length - 1].innerText.match(/^[0-9]*/)[0];
+ if (!pages[next_year].page) {
+ pages[next_year].page = new Array(max);
+ for (var i = 0; i < max; i++) {
+ pages[next_year].page[i] = i + 1;
+ }
+ }
+ }
+ var next_page = Math.floor(Math.random() * pages[next_year].page.length);
+ var next = next_day + '?page=' + pages[next_year].page[next_page];
+ pages[next_year].page.splice(next_page, 1);
+ if (!pages[next_year].page.length) {
+ pages.splice(next_year, 1);
+ }
+ console.log('next: ' + next);
var x = new XMLHttpRequest();
- x.onload = function(e) {
+ x.onload = function() {
if (x.status <= 200 && x.status < 300) {
- next_page_load(e);
+ history.pushState("", "", next);
+ load(x);
} else {
error();
- next_day_load();
+ next_page_load();
}
};
- loading = true;
- x.open('GET', next_day, true);
- x.responseType = 'document';
+ x.open('GET', next, true);
x.send(null);
}
- function next_page_load(e) {
- var doc = e.target.response;
- var pager_r = doc.querySelectorAll('.pager-r');
- var max = +pager_r[pager_r.length - 1].innerText.match(/^[0-9]*/)[0];
- var next = next_day + '?page=' + Math.floor(Math.random() * max + 1);
- if (visited.indexOf(next) < 0) {
- console.log('next: ' + next);
- var x = new XMLHttpRequest();
- x.onload = function() {
- if (x.status <= 200 && x.status < 300) {
- history.pushState("", "", next);
- load(x);
- } else {
- error();
- next_page_load();
- }
- };
- x.open('GET', next, true);
- x.send(null);
- visited.push(next);
- } else {
- next_day_load();
- }
- }
-
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;
@@ -117,6 +139,9 @@
docs = docs.map(function(doc) {
return insert_parent.appendChild(document.importNode(doc, true));
});
+ if (!pages.length) {
+ alert('全部読んだよ');
+ }
return loading = false;
}
/*
* @title endless summer on anond (today -> past year)
* @description 昔の今日の増田ページをランダムでAutoPagerize風
* @include http://anond.hatelabo.jp/
* @license reblog commons license ( cc by 3.0 )
*/
/**
* This script is forked from taizooo's "endless summer on dsbd"
* http://let.hatelabo.jp/taizooo/let/gYC-xcz67_6bYQ
*/
// bookmark
// autopager part from "twitter.AutoPager" : http://userscripts.org/scripts/review/47998
(function() {
var start_url = location.href;
var State = true;
var BASE_REMAIN_HEIGHT = 10000;
var loading = false;
var last_element = (function() {
var a = get_next_elements(document);
return a[a.length - 1];
}());
var max_page_number = get_max_page_number(document);
if (!last_element || !max_page_number) {
return error();
}
var insert_point = last_element.nextSibling;
var insert_parent = last_element.parentNode;
var next_day;
var next_year;
var today = new Date();
var month = today.toISOString().slice(5,7);
var day = today.toISOString().slice(8, 10);
var thisYear = today.getYear();
var bottomYear = (today.getMonth() < 9 || (today.getMonth() === 10 && today.getDay() < 24)) ? 107 : 106;
var pages = new Array(thisYear - bottomYear + 1);
for (var i = 0; i < pages.length; i++) {
pages[i] = {
day: '/' + (1900 + bottomYear + i) + month + day,
page: null
};
}
next_day_load();
window.addEventListener('scroll', function() {
if (loading) return;
var remain = document.documentElement.scrollHeight - window.innerHeight - window.pageYOffset;
if (State && remain < BASE_REMAIN_HEIGHT) {
next_day_load();
}
}, false);
function next_day_load() {
loading = true;
if (!pages.length) return;
next_year = Math.floor(Math.random() * pages.length);
next_day = pages[next_year].day;
if (pages[next_year].page) {
next_page_load();
} else {
var x = new XMLHttpRequest();
x.onload = function(e) {
if (x.status <= 200 && x.status < 300) {
next_page_load(e);
} else {
error();
next_day_load();
}
};
x.open('GET', next_day, true);
x.responseType = 'document';
x.send(null);
}
}
function next_page_load(e) {
if (e) {
var doc = e.target.response;
var pager_r = doc.querySelectorAll('.pager-r');
var max = pager_r[pager_r.length - 1].innerText.match(/^[0-9]*/)[0];
if (!pages[next_year].page) {
pages[next_year].page = new Array(max);
for (var i = 0; i < max; i++) {
pages[next_year].page[i] = i + 1;
}
}
}
var next_page = Math.floor(Math.random() * pages[next_year].page.length);
var next = next_day + '?page=' + pages[next_year].page[next_page];
pages[next_year].page.splice(next_page, 1);
if (!pages[next_year].page.length) {
pages.splice(next_year, 1);
}
console.log('next: ' + next);
var x = new XMLHttpRequest();
x.onload = function() {
if (x.status <= 200 && x.status < 300) {
history.pushState("", "", next);
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);
docs = docs.map(function(doc) {
return insert_parent.appendChild(document.importNode(doc, true));
});
if (!pages.length) {
alert('全部読んだよ');
}
return loading = false;
}
function get_next_elements(doc) {
return Array.apply(null, doc.querySelectorAll('.section'));
}
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;
}
function get_latest_id(doc) {
return doc.evaluate('number(substring-after(//*[@class="section"]/h3/a/@href, "/"))', doc, null, XPathResult.NUMBER_TYPE, null).numberValue;
}
function get_next(url) {
var random_id = Math.floor(Math.random() * max_page_number + 1);
if (isNaN(random_id)) {
return error();
}
var x = '/' + '?page=' + random_id;
return x;
}
function get_max_page_number(doc) {
return doc.evaluate('number(substring-before(//*[@class="pager-r" and not(following-sibling::*)]/text(), "ページ中"))', doc, null, XPathResult.NUMBER_TYPE, null).numberValue;
}
return true;
})();
- Permalink
- このページへの個別リンクです。
- RAW
- 書かれたコードへの直接のリンクです。
- Packed
- 文字列が圧縮された書かれたコードへのリンクです。
- Userscript
- Greasemonkey 等で利用する場合の .user.js へのリンクです。
- Loader
- @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
- Metadata
- コード中にコメントで @xxx と書かれたメタデータの JSON です。