/*
* @title masuda fold
* @description 匿名日記を畳む
* @include *://anond.hatelabo.jp/
* @include *://anond.hatelabo.jp/?page=*
* @include *://anond.hatelabo.jp/c/*
* @include *://anond.hatelabo.jp/keyword/*
* @license MIT License
* @require
*/
/**
* this script is related to http://let.hatelabo.jp/yuta25/let/hJmc7avBw8tJ
*/
(function(){
'use strict';
var keyword = document.location.href.match(/jp\/keyword/) ? true : false;
var category = document.location.href.match(/jp\/c/) ? true : false;
var contentAll = [];
var getContentTitleAll = function() {
return document.querySelectorAll('.section h3:first-child');
};
var getContentAll = function(numOfProcessed) {
var contents = document.querySelectorAll('.section');
for (var i = numOfProcessed, contentsLength = contents.length; i < contentsLength; i++) {
var content = contents[i],
nodes = content.childNodes;
var div = document.createElement('div');
var offset;
if (keyword || category) {
offset = 2;
} else {
offset = 1;
}
for (var j = offset,nodesLength = nodes.length - 1; j < nodesLength; j++) {
div.appendChild(nodes[offset]);
}
var footer = nodes[offset];
content.appendChild(footer);
contentAll.push(div);
div.style.display = 'none';
content.insertBefore(div, footer);
}
};
var foldMasudaContent = function() {
var display;
var text;
var isFold;
if (this.dataset.isfold == '1') {
display = 'block';
text = 'close';
isFold = '0';
} else {
display = 'none';
text = 'OPEN';
isFold = '1';
}
contentAll[this.dataset.id].style.display = display;
this.innerHTML = text;
this.setAttribute('data-isfold', isFold);
};
var createFoldAnchor = function(id, titles) {
var span = document.createElement('span');
span.setAttribute('data-id', id);
span.setAttribute('data-isfold', 1);
span.addEventListener('click', foldMasudaContent, true);
span.style.marginLeft = '10px';
span.style.color = '#888888';
span.style.cursor = 'pointer';
titles[id].appendChild(span);
span.innerHTML = 'OPEN';
};
var masudaFold = function(numOfProcessed) {
var titles = getContentTitleAll();
getContentAll(numOfProcessed);
for (var i = numOfProcessed, titleLength = titles.length; i < titleLength; i++) {
createFoldAnchor(i, titles);
}
return titles.length;
};
var numOfProcessed = masudaFold(0);
var timer = 0;
document.addEventListener('DOMNodeInserted', function() {
if(timer) return;
timer = setTimeout(function() {
numOfProcessed = masudaFold(numOfProcessed);
timer = 0;
}, 30);
}, false);
}());