Skip to content

Instantly share code, notes, and snippets.

@atian25
Last active December 11, 2015 12:38
Show Gist options
  • Save atian25/4602119 to your computer and use it in GitHub Desktop.
Save atian25/4602119 to your computer and use it in GitHub Desktop.
加载并展开知乎收藏夹
// ==UserScript==
// @name zhihu-favorites
// @namespace https://gist.github.com/4602119
// @version 0.1
// @description 加载并展开知乎收藏夹
// @match http://www.zhihu.com/collection/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @copyright 2012+, TZ <[email protected]>
// ==/UserScript==
$(function(){
//添加按钮
$('#zh-list-meta-wrap').append('<span class="zg-bull">•</span> <a href="javascript:;" id="expandAll">列出全部</a>');
//注册点击事件
$('#expandAll').one('click', function(){
var itemCount = $('#zh-list-answer-wrap .zm-item').length;
var last = '';
waitForKeyElements("#zh-list-answer-wrap .zm-item", function(jNode){
var start = $('#zh-load-more').attr('data-next').toString();
if(last !== start){
itemCount++;
$('#expandAll').replaceWith('<span id="expandAll">努力的加载中(' + itemCount + ')...</span>');
loadMore();
last = start;
}else if(start == '-1'){
$('#expandAll').replaceWith('加载完成,共' + itemCount + '条');
expandAll();
}
console.log('loaded, next=%s', start, itemCount);
});
loadMore();
});
});
//点击按钮
function clickNode(dom){
var clickEvent = document.createEvent ("HTMLEvents");
clickEvent.initEvent ("click", true, true);
dom.dispatchEvent(clickEvent);
}
//点击加载更多
function loadMore(){
clickNode($('#zh-load-more')[0]);
}
//展开全部
function expandAll(){
$('.zm-item-expandall').each(function(item){
clickNode(this);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment