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

javascript/announce - (2021/03/04 (木) 13:47:39) のソース

#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.setMinutes(59);
		lm.setSeconds(59);
		lm.setMonth(lm.getMonth()+1);
		lm.setHours(23);
		lm.setDate(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);
	},
	CreateWrapContent: function(force) {
		var force = (force !== undefined) ? force : false;
		//コメントフォーム告知欄を取得しコメント欄に被せる
		let self = this;
		let anPcom = $('.ref-announce-pcomment:eq(0)');
		if (anPcom.length > 0 && $('.announce-layer').length == 0 && (self.IsAnnounce() || force)) {
			
			anPcom = anPcom.clone(true).attr('class', 'announce-layer');
                     $('.warning-wrapper .uk-overflow-container', anPcom).removeClass('uk-overflow-container');
			let btn = $('.announce-close-button', anPcom);
			btn.replaceWith($('<button>', {'type': 'button'})
			.addClass('announce-close')
			.text(btn.text())
			.click(function() {
				$('.announce-layer').remove();
				self.Accept();
			}));
			$('table', anPcom).attr('align', 'center');
		$.each(anPcom.find('.lazy'), function(i,b) {
		  if (b.dataset.original) {
		    b.src = this.dataset.original;
		    b.removeAttribute("data-original");
		    b.style.opacity = 1;
		  }
		  else if (b.dataset.srcset) {
		    b.srcset = b.dataset.srcset;
		    b.removeAttribute("data-srcset");
		  }
		});

			// コメントフォームに被せる
			$('.plugin_pcomment_form, .plugin_comment_num2_form, .plugin_comment_num_form, .plugin_comment_form, .commentics').each(function() {
				let tabs = $(this).parents('.tabs');
				let tgt = (tabs.length > 0) ? tabs : $(this);
				if (tgt.find('.announce-layer').length == 0) {
					tgt.css('position','relative').prepend(anPcom.clone(true));
					let sticky = $('.warning-wrapper', tgt);
					$('.announce-layer', tgt).css({'height': '100%', 'bottom': '0', 'min-height': $('.warning-wrapper', tgt).outerHeight(true)+'px'});
					// コメント欄が長すぎる場合下段にも追加表示
					if ((sticky.outerHeight(true) * 2 + window.innerHeight) < sticky.parent().height()) {
						sticky.clone(true).addClass('sticky').css({'bottom': 0}).appendTo(sticky.parent());
					}
				}
			});
		}
	}
};
document.addEventListener('DOMContentLoaded', function() {
	fgoWikiAnnounce.Init();
	// テキストノード追加拡張
	$.fn.appendText = function(text) {
		return this.each(function() {
			var textNode = document.createTextNode(text);
			$(this).append(textNode);
		});
	};
	
	// デバッグ用
//	$('#menubar').prepend($('<button>', {'type': 'button'}).addClass('announce-close')
//	.text('DEBUG:告知再表示')
//	.click(function() {
//		fgoWikiAnnounce.Clear();
//	}));
	
	//ネタバレ自粛期間テキストの置き換え
	let Elem;
	let span = $('.ref-spoil-span:eq(0)').text().trim();
	let end_time;
	let et = $('<span>').addClass('announce-endtime');
	if (span.length > 0) {
		end_time = new Date(Date.now() + (parseInt(span)*1000));
		if (!isNaN(end_time)) {
			$('<span>').text('自粛終了予定日  ').appendTo(et);
			$('<span>').text(end_time.getFullYear()).appendTo(et);
			et.appendText('年');
		 	$('<span>').text(('00' + (end_time.getMonth()+1)).slice(-2)).appendTo(et);
		 	et.appendText('月');
		 	$('<span>').text(('00' + end_time.getDate()).slice(-2)).appendTo(et);
		 	et.appendText('日 ');
			Elem = $('.ref-spoil-ng:eq(0)').clone(true).removeClass('ref-spoil-ng');
			Elem.find('.spoil-span').empty().css('background-color', 'rgb(183,31,17)').append(et);
		}
	} else {
		Elem = $('.ref-spoil-ok:eq(0)').clone(true).removeClass('ref-spoil-ok');
		et.append(Elem.find('.spoil-span').text());
		Elem.find('.spoil-span').empty().append(et);
	}
	$('div[class^="spoil-"]').each(function() {
		if (!end_time || isNaN(end_time) || end_time.getTime() < Date.now()) { // 期間外
			if ($(this).attr('class').indexOf('ok') != -1) {
				$(this).empty().append(Elem.clone(true));
			}
		} else { // 期間内
			if ($(this).attr('class').indexOf('ng') != -1) {
				$(this).empty().append(Elem.clone(true));
			}
		}
	});
	fgoWikiAnnounce.CreateWrapContent();
	let btnElem = $('<button>', {'type': 'button'}).css({'margin': '5px 0'}).text('利用ルールを表示する').click(function() {
		fgoWikiAnnounce.CreateWrapContent(true);
		
	});
	$('.plugin_pcomment_form, .plugin_comment_num2_form, .plugin_comment_num_form, .plugin_comment_form').each(function() {
		$('input[type="submit"]', this).after(btnElem.clone(true)).after('<br>');
	});
      $('.commentics').each(function() {
            $(this).after(btnElem.clone(true)).after('<br>');
      });

	// サーヴァント個別ページフッター個別告知
	// 個別ページだけにある適当なidからcollectionNoを辿る
	let svt_no = $('#classname').parents('#wikibody').find('h2:eq(0)').text().trim().slice(-3);
	let svt_name = $('#classname').parents('tbody').find('tr:eq(1) > td:eq(1)').text().trim();
	$('.announce-svtfooter').find('.svtname').text(svt_name); // &spanclass(svtname)を個別ページのサーヴァント名に置換
	$('div[class^="ra-svt"]').hide();
	$('.ra-svt' + svt_no).show();
});
</script>
}}}}}