「javascript/announce」の編集履歴(バックアップ)一覧に戻る

javascript/announce - (2018/01/12 (金) 18:22:13) のソース

#js(){{{{{
<script type="text/javascript">
let fgoWikiAnnounce = {
	LastAcceptKey: 'fgowiki-announce-lastaccept',
	LastModifyKey: 'fgowiki-announce-lastmodify',
	Init: function() {
		let lm = $('.lastmod:eq(0)').text().trim().replace(/[^0-9\n]+/g, ' ').slice(0, -1);
		if (lm.length == 0) {
			this.LastModify = Number.NaN;
			return;
		}
		lm = lm.split(' ');
		let tf = new Date(Date.UTC(lm[0], parseInt(lm[1])-1, lm[2], lm[3], lm[4], lm[5]) - 32400000);
		if (isNaN(tf)) {
			this.LastModify = Number.NaN;
			return;
		}
		this.LastModify = tf.getTime();
	},
	IsAnnounce: function() {
		// まだ同意してない(初回)
		if (!localStorage[this.LastAcceptKey] || !localStorage[this.LastModifyKey]) {
			return true;
		}
		// 告知ページが更新された
		let saveModify = parseInt(localStorage[this.LastModifyKey]);
		if (!isNaN(saveModify) && !isNaN(this.LastModify) && saveModify < this.LastModify) {
			return true;
		}
		// 最後に同意してから月を跨いだ(毎月1日JSTに表示する)
		let lm = new Date(parseInt(localStorage[this.LastAcceptKey]));
		lm.setUTCHours(14);
		lm.setUTCMinutes(59);
		lm.setUTCSeconds(59);
		lm.setUTCMonth(lm.getUTCMonth()+1);
		lm.setUTCDate(0);
		if (lm.getTime() < Date.now()) {
			return true;
		}
		return false;
	},
	Accept: function() {
		if (this.LastModify) {
			localStorage[this.LastModifyKey] = this.LastModify.toString();
			localStorage[this.LastAcceptKey] = Date.now().toString();
		}
	},
	Clear: function() {
		localStorage.removeItem(this.LastModifyKey);
		localStorage.removeItem(this.LastAcceptKey);
	}
};
$(function() {
	fgoWikiAnnounce.Init();
	
	// デバッグ用
	$('#menubar').prepend($('<button>', {'type': 'button'})
	.css({'margin':'8px', 'padding':'5px 30px', 'font-size':'1.5em'})
	.text('クリア')
	.click(function() {
		fgoWikiAnnounce.Clear();
	}));
	
	//ネタバレ自粛期間テキストの置き換え
	let ngElem = $('.ref-spoil-ng:eq(0)').clone(true);
	let okElem = $('.ref-spoil-ok:eq(0)').clone(true);
	let span = $('.ref-spoil.span:eq(0)').text().trim();
	if (span.length > 0) {
		ngElem.find('.spoil-span').text(span);
	}
	$('span[class^="spoil-"]').each(function() {
		if (span.length > 0) { // 期間内
			if ($(this).attr('class').indexOf('ng') != -1) {
				$(this).html(ngElem.html());
			}
		} else { // 期間外
			if ($(this).attr('class').indexOf('ok') != -1) {
				$(this).html(okElem.html());
			}
		}
	});
	//コメントフォーム告知欄を取得しコメント欄に被せる
	let anPcom = $('.ref-announce-pcomment:eq(0) > div:last-child');
	if (anPcom.length > 0 && fgoWikiAnnounce.IsAnnounce()) {
		
		anPcom = anPcom.addClass('announce-layer').css({
			'background-color': 'rgba(255,0,0,.8)',
			'box-sizing': 'border-box',
			'color': 'white',
			'font-weight': 'bold',
			'min-height': '100%',
			'padding': '15px 0',
			'position': 'absolute',
			'text-align': 'center',
			'width': '100%',
			'z-index': '10',
		}).append($('<br>')).append($('<button>', {'type': 'button'})
		.css({'margin':'8px', 'padding':'5px 30px', 'font-size':'1.5em'})
		.text('閉じる')
		.click(function() {
			$('.announce-layer').remove();
			fgoWikiAnnounce.Accept();
		}));

		// コメントフォームに被せる
		$('.plugin_pcomment_form, .plugin_comment_num2_form, .plugin_comment_num_form, .plugin_comment_form').each(function() {
			$(this).css('position', 'relative').prepend(anPcom.clone(true));
		});
	}
});
</script>
}}}}}