Skip to content

Instantly share code, notes, and snippets.

@8th713
Last active August 29, 2015 14:06
Show Gist options
  • Save 8th713/e43de18a9254da536f36 to your computer and use it in GitHub Desktop.
Save 8th713/e43de18a9254da536f36 to your computer and use it in GitHub Desktop.
[deprecation] http://8th713.github.io/extend-pixiv/ 配布先を変更しました。
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// ==UserScript==
// @name Sort the work list for pixiv
// @version 0.1.11
// @description Makes possible to toggle the asc/desc of work list.
// @author 8th713
// @namespace http://8th713.tumblr.com/
// @match http://www.pixiv.net/member_illust.php?id*
// @noframes
// @copyright 2014, 8th713
// @license MIT
// ==/UserScript==
var Stream = require('./stream');
var ID_RE = /illust_id=(\d+)/i;
var CURRENT_CLASS = 'current';
var PAGERIZE_EVENTS = [
'AutoPagerize_DOMNodeInserted',
'AutoPatchWork.DOMNodeInserted',
'AutoPagerAfterInsert'
];
var $$ = document.querySelectorAll.bind(document);
var mediator = {
last: null,
register: function(fn) {
if (this.last) { this.last(); }
this.last = fn;
}
};
function sortBy(arr, fn) {
arr = [].slice.call(arr);
arr.sort(fn);
return arr;
}
function extractId(el) {
var id = ID_RE.exec(el.firstElementChild.href);
el.dataset.id = id && id[1];
return id && +id[1];
}
function asc(a, b) {
a = (a.dataset.id) ? +a.dataset.id : extractId(a);
b = (b.dataset.id) ? +b.dataset.id : extractId(b);
return a - b;
}
function desc(a, b) {
a = (a.dataset.id) ? -a.dataset.id : -extractId(a);
b = (b.dataset.id) ? -b.dataset.id : -extractId(b);
return a - b;
}
function add(el, index) {
this[~~(index / 20)].appendChild(el);
}
var pagerizeStream = new Stream();
PAGERIZE_EVENTS.forEach(function(event) {
document.body.addEventListener(event, function(event) {
pagerizeStream.publish(event);
}, false);
});
function createBtn(text, isDesc) {
var stream = new Stream();
var btn = document.createElement('a');
btn.textContent = text;
btn.href = '';
container.appendChild(btn);
function reset() {
btn.classList.remove(CURRENT_CLASS);
}
btn.addEventListener('click', function(event) {
event.preventDefault();
stream.publish(event);
});
return stream
.filter(function() {
return !btn.classList.contains(CURRENT_CLASS);
})
.map(function() {
return isDesc;
})
.subscribe(function() {
mediator.register(reset);
btn.classList.add(CURRENT_CLASS);
});
}
var container = document.createElement('li');
var ascBtnClickStream = createBtn('新しい順', true);
var descBtnClickStream = createBtn('古い順', false);
document.querySelector('.menu-items').appendChild(container);
ascBtnClickStream
.merge(descBtnClickStream)
.combine(pagerizeStream, function(isDesc) {
return isDesc;
})
.scan(undefined, function(last, newer) {
return (last === false && newer === false) ? false
: (last === newer) ? undefined
: newer;
})
.subscribe(function(isDesc) {
if (isDesc === undefined) { return; }
requestAnimationFrame(function() {
var roots = $$('.display_works>ul');
var items = $$('.display_works>ul>li');
sortBy(items, isDesc ? desc : asc).forEach(add, roots);
});
});
// initialize
pagerizeStream.publish();
ascBtnClickStream.publish();
},{"./stream":2}],2:[function(require,module,exports){
/**
* Quote from kamo.js v0.0.4
* (c) 2014 Ryo Nakamura (https://github.com/r7kamura/kamo.js)
* License: MIT
*/
module.exports = Stream;
function Stream() {
this.subscriptions = [];
}
Stream.prototype.publish = function(msg) {
var i = 0;
var len = this.subscriptions.length;
for (; i < len; i++) {
this.subscriptions[i](msg);
}
return this;
};
Stream.prototype.subscribe = function(subscription) {
this.subscriptions.push(subscription);
return this;
};
Stream.prototype.filter = function (filter) {
var filteredStream = new Stream();
this.subscribe(function(msg) {
if (filter(msg)) {
filteredStream.publish(msg);
}
});
return filteredStream;
};
Stream.prototype.map = function(map) {
var mapStream = new Stream();
this.subscribe(function(msg) {
mapStream.publish(map(msg));
});
return mapStream;
};
Stream.prototype.merge = function(stream) {
var mergedStream = new Stream();
var publisher = mergedStream.publish.bind(mergedStream);
this.subscribe(publisher);
stream.subscribe(publisher);
return mergedStream;
};
Stream.prototype.combine = function(anotherStream, combiner) {
var combinedStream = new Stream();
var latestMsgOfThis;
var latestMsgOfAnother;
var hasAnyMsgOfThis = false;
var hasAnyMsgOfAnother = false;
this.subscribe(function(msg) {
latestMsgOfThis = msg;
hasAnyMsgOfThis = true;
if (hasAnyMsgOfAnother) {
combinedStream.publish(combiner(latestMsgOfThis, latestMsgOfAnother));
}
});
anotherStream.subscribe(function(msg) {
latestMsgOfAnother = msg;
hasAnyMsgOfAnother = true;
if (hasAnyMsgOfThis) {
combinedStream.publish(combiner(latestMsgOfThis, latestMsgOfAnother));
}
});
return combinedStream;
};
Stream.prototype.scan = function(initialValue, accumulator) {
var accumulatorStream = new Stream();
var prevMsg = initialValue;
this.subscribe(function(msg) {
prevMsg = accumulator(prevMsg, msg);
accumulatorStream.publish(prevMsg);
});
return accumulatorStream;
};
},{}]},{},[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment