🍄ContribShare Button

A share button that copies the raw, not percent-encoded URL and invokes the share sheet, if available.

(function () {
	var shareButton = document.createElement('button');
	shareButton.innerHTML = 'Share';
	shareButton.style = "\
	background: none;\
	margin: 0;\
	padding: 0;\
	border: none;\
	font: inherit;\
	color: inherit;\
	cursor: pointer;\
	opacity: 0.6;\
	";

	var shareEnabled = true;

	// if you want to disable share sheet on desktops,
	// uncomment the following line:
	// shareEnabled = /android|webos|iphone|ipad|ipod/i.test(window.navigator.userAgent);

	// or to disable it completely:
	// shareEnabled = false;

	var onSuccess = function () {
		shareButton.innerHTML = 'Copied!';
	};

	shareButton.addEventListener('click', function () {
		var url = decodeURI(window.location.href);

		if (window.navigator.share && shareEnabled) {
			window.navigator.share({ text: url });
		} else if (window.navigator.clipboard) {
			window.navigator.clipboard.writeText(url).then(onSuccess);
		} else {
			var textArea = document.createElement("textarea");
			textArea.value = url;
			textArea.style = "\
			background: none;\
			color: transparent;\
			position: fixed;\
			top: 0;\
			left: 0;\
			padding: 0;\
			width: 4px;\
			height: 4px;\
			";

			document.body.appendChild(textArea);
			textArea.focus();
			textArea.select();

			try {
				if (document.execCommand('copy')) onSuccess();
			} catch (err) {
				console.error(err);
			}
			document.body.removeChild(textArea);
		}
	});


	var naviTitle = document.querySelector('.navi-title');
	naviTitle.parentNode.insertBefore(shareButton, naviTitle.nextSibling);
})();

Place it, for example, at path/to/wiki/static/share.js, then add the following lines to config.ini:

[CustomScripts]
ViewScripts = /static/share.js