Skip to content

Instantly share code, notes, and snippets.

@iwinux
Last active March 19, 2016 13:39
Show Gist options
  • Save iwinux/d4d8f0fefc635f660d1f to your computer and use it in GitHub Desktop.
Save iwinux/d4d8f0fefc635f660d1f to your computer and use it in GitHub Desktop.
选择困难症患者专用的 Chrome 脚本,在豆瓣阅读征文大赛作品列表里随机挑一本作品
// ==UserScript==
// @name pick-works
// @namespace http://tampermonkey.net/
// @version 0.1
// @author E.T
// @match https://read.douban.com/competition/2015/works*
// @grant none
// ==/UserScript==
;(function () {
'use strict';
function randInt(upper) {
return Math.floor(Math.random() * upper) // [0, upper)
}
function pick(upper, prevIdx) {
var idx = randInt(upper)
while (idx === prevIdx) {
idx = randInt(upper)
}
return idx
}
Do(function () {
var btnParent = $('.works-filter .inner')
, items = $('.works-list .item')
, idxUpper = items.length
, prevIdx
, prevItem
btnParent.append('<a href="#" class="btn btn-pick active">给大爷挑一个</a>')
btnParent.click('.btn-pick', function (evt) {
evt.preventDefault()
var idx = pick(idxUpper, prevIdx)
, item = $(items[idx])
if (prevItem !== undefined) {
prevItem.hide()
item.show()
} else {
for (var i = 0; i < idxUpper; ++i) {
if (i === idx) { continue }
$(items[i]).hide()
}
}
prevIdx = idx
prevItem = item
})
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment