Last active
August 29, 2015 14:11
-
-
Save vzvu3k6k/105bbfac74fe004c64b6 to your computer and use it in GitHub Desktop.
Quyoでスクロールすると無限にランダムな記事を表示する。
This file contains 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 Quyo: Random Auto Pager | |
// @description スクロールすると無限にランダムな記事を表示する。AutoPagerizeとは連携しない。 | |
// @version 1.2 | |
// @author vzvu3k6k | |
// @match http://quyo.hatelabo.jp/items/* | |
// @exclude http://quyo.hatelabo.jp/items/post | |
// @exclude http://quyo.hatelabo.jp/items/*/edit | |
// @namespace http://vzvu3k6k.tk/ | |
// @license CC0 | |
// ==/UserScript== | |
// 先行: | |
// quyo random autopagerize - Hatena::Let | |
// http://let.hatelabo.jp/yuta25/let/hJmezJuridFx | |
// 関連: | |
// wikipedia_random_auto_pager.user.js | |
// https://gist.github.com/negipo/c91d4f6153d2e45f56a1 | |
var isLoading = false; | |
var disable = false; | |
const remainHeight = 500; | |
// favoriteボタンなどを正しく動かすのが難しいのでフッターは隠す | |
(function(){ | |
// ロード時にjQuery($footer).slideDown()が実行されて | |
// .footer-actionのstyle属性にdisplay: blockが設定されるので、`!important`で上書きする | |
var $style = document.createElement('style'); | |
$style.textContent = '.footer-action { display: none !important }'; | |
document.head.appendChild($style); | |
})(); | |
window.addEventListener('scroll', function(event){ | |
if(disable) return; | |
if(isLoading) return; | |
// Copied from autopagerize.user.js by swdyh | |
// https://github.com/swdyh/autopagerize/blob/master/autopagerize.user.js | |
var scrollHeight = Math.max(document.documentElement.scrollHeight, | |
document.body.scrollHeight); | |
if(scrollHeight - window.innerHeight - event.pageY > remainHeight) return; | |
try{ | |
isLoading = true; | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', '/items/next'); | |
xhr.onload = function(){ | |
try{ | |
if(xhr.status !== 200){ | |
notify('記事をダウンロードできませんでした。(HTTP status: ' + xhr.status + ' - ' + xhr.statusText + ')'); | |
} | |
isLoading = false; | |
var $articleContainer = xhr.responseXML.querySelector('article.entry').parentNode; | |
document.body.appendChild($articleContainer); | |
}catch(e){ | |
disable = true; | |
notify('記事の挿入に失敗しました。', e); | |
} | |
}; | |
xhr.onerror = function(){ | |
disable = true; | |
notify('記事をダウンロードできませんでした。'); | |
}; | |
xhr.ontimeout = function(){ | |
disable = true; | |
notify('記事をダウンロードできませんでした。(timeout)'); | |
}; | |
xhr.responseType = 'document'; | |
xhr.send(); | |
}catch(e){ | |
disable = true; | |
notify('記事のダウンロードが開始できませんでした。', e); | |
} | |
}); | |
var notify = function(message, exception){ | |
var $p = document.createElement('p'); | |
$p.textContent = 'Random Auto Pager: ' + message; | |
$p.appendChild(document.createElement('br')); | |
if(exception){ | |
var $code = document.createElement('code'); | |
$code.textContent = exception; | |
$p.appendChild($code); | |
} | |
document.body.appendChild($p); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment