미디어위키:Gadget-siteNotice.js

/*
* siteNotice
* The contents of Mediawiki:Sitenotice are always exposed to search engines.
* This gadget fixes the problem.
* @author ykhwong
* Reference: https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/DismissableSiteNotice/+/master/modules/ext.dismissableSiteNotice.js
*/
/*jshint esversion: 6 */
const noticeGrpPage = '틀:소도구/noticeGrp';
const isBot = /bot|googlebot|crawler|spider|robot|crawling/i.test(navigator.userAgent);
const isMobile = /\.m\.wikipedia\.org/.test(window.location.host);
const storageData = {
	html: 'parsedSiteNoticeHtml',
	dismissClicked: 'dismissClicked',
	noticeId: 'storedSiteNoticeId'
};

if (isBot) {
	$("#siteNotice, .noprint, .mw-jump-link").html("");
}

$(function () {
	var sitenoticeId = '';
	var dismissStr = '';
	var sitenoticeStyle = '';

	function html2text(html) {
		const tag = document.createElement('div');
		tag.innerHTML = html;
		return tag.innerText;
	}

	function getDivHtml(html, selector) {
		const tag = document.createElement('div');
		tag.innerHTML = html;
		return $(tag).find(selector).html();
	}

	function getDivText(html, selector) {
		const tag = document.createElement('div');
		tag.innerHTML = html;
		return $(tag).find(selector).text().trim();
	}

	function setMobileStyle() {
		const $notice = $("#siteNoticeLocal");
		$notice.css({
			'position': 'relative',
			'padding-left': $notice.css('padding-left') === '0px' ? '12px' : $notice.css('padding-left'),
			'padding-top': $notice.css('padding-top') === '0px' ? '12px' : $notice.css('padding-top'),
			'padding-right': $notice.css('padding-right') === '0px' ? '12px' : $notice.css('padding-right'),
			'padding-bottom': $notice.css('padding-bottom') === '0px' ? '15px' : $notice.css('padding-bottom')
		});
		$(".mw-dismissable-notice-close2").css({
			'position': 'relative',
			'top': '0px',
			'right': '0px',
			'padding-left': '5px',
			'text-align': 'right',
			'float': 'right'
		});
	}

	function setDesktopStyle() {
		$("#siteNoticeLocal").css({
			'padding-top': '5px',
			'padding-bottom': '5px',
			'margin-bottom': '15px'
		});
		$(".mw-dismissable-notice-close2").css({
			'position': 'relative',
			'top': '0px',
			'right': '7px',
			'padding-left': '5px',
			'text-align': 'right',
			'float': 'right'
		});
	}

	function procDismiss() {
		$("#siteNoticeLocal").prepend(
			'<div class="mw-dismissable-notice-close2">' +
			'<a tabindex="0" role="button">' +
			'<img style="display: block; opacity: 0.55; background-color: white; width: 17px; margin-left: 2px; margin-bottom: 2px;" ' +
			'src="data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2220%22 height=%2220%22 viewBox=%220 0 20 20%22%3E%3Ctitle%3Eclose%3C/title%3E%3Cpath d=%22M4.34 2.93l12.73 12.73-1.41 1.41L2.93 4.35z%22/%3E%3Cpath d=%22M17.07 4.34L4.34 17.07l-1.41-1.41L15.66 1.93z%22/%3E%3C/svg%3E" ' +
			'title="' + dismissStr + '">' +
			'</a></div>'
		);

		if (isMobile) {
			setMobileStyle();
		} else {
			setDesktopStyle();
		}

		$("#siteNoticeLocal ul").css({
			"list-style": "none",
			"text-align": "left",
			"display": "table",
			"margin": "0 auto",
			"padding-left": "10px",
			"max-width": "calc(100% - 20px)"
		});

		$('.mw-dismissable-notice-close2 a').on('click keypress', function (e) {
			if (e.type === 'click' || (e.type === 'keypress' && e.which === 13)) {
				$("#siteNoticeLocal").hide();
				mw.storage.set(storageData.dismissClicked, "true");
				mw.storage.set(storageData.noticeId, sitenoticeId);
				waitForPortletAndAdd();
			}
		});
	}

	function appendNoticeFromHtml(html, isForAnon) {
		const content = isForAnon ? getDivHtml(html, "#gadgetAnonnotice") : getDivHtml(html, "#gadgetSiteNotice");
		const style = getDivText(html, "#sitenoticeStyle");

		if (!content || !/\S/.test(html2text(content))) {
			return;
		}

		const newHtml = '<div id="siteNoticeLocal" style="' + style + '">' +
			'<div class="mw-parser-output">' + content + '</div></div>';

		if ($("#siteNotice").html().trim() !== newHtml.trim()) {
			$("#siteNotice").html(newHtml);
			procDismiss();
		}
	}

	function procApi(forceShow = false) {
		if (isBot) return;

		const api = new mw.Api();
		api.parse(new mw.Title(noticeGrpPage)).then(function (html) {
			const isLoggedIn = mw.config.get('wgUserName') !== null;

			sitenoticeId = getDivText(html, "#sitenoticeId");
			dismissStr = getDivText(html, "#dismissLabel");
			sitenoticeStyle = getDivText(html, "#sitenoticeStyle");

			const storedId = mw.storage.get(storageData.noticeId);
			const oldHtml = mw.storage.get(storageData.html) || "";

			if (html.trim() !== oldHtml.trim()) {
				mw.storage.set(storageData.html, html);
			}

			const gadgetSiteNotice = getDivHtml(html, "#gadgetSiteNotice");
			const gadgetAnonnotice = getDivHtml(html, "#gadgetAnonnotice");

			const siteText = html2text(gadgetSiteNotice).trim();
			const anonText = html2text(gadgetAnonnotice).trim();

			if (isLoggedIn) {
				if (/\S/.test(siteText) && (storedId !== sitenoticeId || forceShow)) {
					mw.storage.set(storageData.dismissClicked, "false");
					appendNoticeFromHtml(html, false);
					$('#t-restoreNotice').remove();
				}
			} else {
				if (!/\S/.test(anonText)) return;
				if (/^\s*-\s*$/.test(anonText)) {
					if (/\S/.test(siteText) && (storedId !== sitenoticeId || forceShow)) {
						mw.storage.set(storageData.dismissClicked, "false");
						appendNoticeFromHtml(html, false);
						$('#t-restoreNotice').remove();
					}
				} else {
					mw.storage.set(storageData.dismissClicked, "false");
					appendNoticeFromHtml(html, true);
					$('#t-restoreNotice').remove();
				}
			}
		});
	}

	function waitForPortletAndAdd(retries = 10) {
		if ($('#p-tb').length === 0) {
			if (retries > 0) {
				setTimeout(() => waitForPortletAndAdd(retries - 1), 100);
			}
			return;
		}
		if ($('#t-restoreNotice').length > 0) return;

		mw.util.addPortletLink(
			'p-tb',
			'#',
			'사이트 공지 다시 보기',
			't-restoreNotice',
			'숨긴 사이트 공지를 다시 표시합니다.',
			null,
			'#t-specialpages'
		);

		$('#t-restoreNotice a').on('click', function (e) {
			e.preventDefault();
			mw.storage.set(storageData.dismissClicked, "false");
			procApi(true);
		});
	}

	if (mw.storage.get(storageData.dismissClicked) === "true") {
		waitForPortletAndAdd();
	}

	const cachedHtml = mw.storage.get(storageData.html);

	if (mw.storage.get(storageData.dismissClicked) === "false" && cachedHtml) {
		const isLoggedIn = mw.config.get('wgUserName') !== null;
		const cachedSiteText = html2text(getDivHtml(cachedHtml, "#gadgetSiteNotice")).trim();
		const cachedAnonText = html2text(getDivHtml(cachedHtml, "#gadgetAnonnotice")).trim();

		if (isLoggedIn) {
			appendNoticeFromHtml(cachedHtml, false);
		} else {
			if (!/\S/.test(cachedAnonText)) {
				return;
			}

			if (/^\s*-\s*$/.test(cachedAnonText)) {
				appendNoticeFromHtml(cachedHtml, false);
			} else {
				appendNoticeFromHtml(cachedHtml, true);
			}
		}
	}

	procApi();
});
Prefix: a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9

Portal di Ensiklopedia Dunia

Kembali kehalaman sebelumnya