Markdown toolbar for reddit.com

Creates a Markdown toolbar whenever you make/edit text posts or comments in reddit.com

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Markdown toolbar for reddit.com
// @namespace   darkred
// @version     1.4.1
// @description Creates a Markdown toolbar whenever you make/edit text posts or comments in reddit.com
// @author      wOxxOm, darkred
// @license     MIT
// @include     https://www.reddit.com/*submit*
// @include     https://old.reddit.com/*submit*
// @include     https://www.reddit.com/*comments*
// @include     https://old.reddit.com/*comments*
// @grant       GM_addStyle
// @icon        https://raw.githubusercontent.com/dcurtis/markdown-mark/master/png/66x40-solid.png
//
//
// This is a modified version of the script "Markdown toolbar for GreasyFork and UserStyles.org" ()https://greasyfork.org/en/scripts/6779-markdown-toolbar-for-greasyfork-and-userstyles-org)
// Thanks a lot to wOxxOm for making that script.
//
// @supportURL  https://github.com/darkred/Userscripts/issues
// ==/UserScript==


var x;

// IF IT'S A SUBMIT PAGE
if (window.location.href.indexOf('submit') > - 1) {
	// THEN ADD TOOLBAR TO THE 'NEW POST' TEXTBOX
	x = document.querySelectorAll('div.md > textarea:nth-child(1)')[0].parentNode;
	addFeatures(x);
}
else {
	var textareas = document.querySelectorAll('textarea');

	// ADD TOOLBAR: TO EDITING YOUR POST, TO 'NEW COMMENT' FORM AND TO EDITING YOUR EXISTING COMMENT(S)
	for (var i = 0; i < textareas.length; i++) {
		x = document.querySelectorAll('textarea') [i].parentNode;
		addFeatures(x);
	}
}





function addFeatures(n) {

	n.parentNode.textAreaNode = x.firstChild;

	GM_addStyle('\
			.Button {\
					display: inline-block;\
					cursor: pointer;\
					margin: 0px;\
					font-size: 12px;\
					line-height: 1;\
					font-weight: bold;\
					padding: 4px 6px;\
					background: -moz-linear-gradient(center bottom , #CCC 0%, #FAFAFA 100%) repeat scroll 0% 0% #F8F8F8;\
					border: 1px solid #999;\
					border-radius: 2px;\
					white-space: nowrap;\
					text-shadow: 0px 1px 0px #FFF;\
					box-shadow: 0px 1px 0px #FFF inset, 0px -1px 2px #BBB inset;\
					color: #333;}');


	// add buttons
	btnMake(n, '<b>B</b>', 'Bold', '**');
	btnMake(n, '<i>I</i>', 'Italic', '*');
	// btnMake(n, '<u>U</u>', 'Underline', '<u>','</u>');
	// btnMake(n, '<s>S</s>', 'Strikethrough', '<s>','</s>');
	btnMake(n, '<s>S</s>', 'Strikethrough', '~~');
	btnMake(n, '^', 'Superscript', '^','', true);
	btnMake(n, '\\n', 'Line break', '&nbsp;\n', '', true);
	btnMake(n, '---', 'Horizontal line', '\n\n---\n\n', '', true);
	btnMake(n, 'URL', 'Add URL to selected text',
		function(e) {
			try {edWrapInTag('[', ']('+prompt('URL'+':')+')', edInit(e.target));}
			catch(e) {}
		});
	// btnMake(n, 'Image', 'Convert selected https://url to inline image', '!['+'image'+'](', ')');
	btnMake(n, 'Table', 'Insert table template', '\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n', '', true);
	btnMake(n, 'Code', 'Apply CODE markdown to selected text',
		function(e){
			var ed = edInit(e.target);
			if (ed.sel.indexOf('\n') < 0)
				edWrapInTag('`', '`', ed);
			else
				edWrapInTag(((ed.sel1==0) || (ed.text.charAt(ed.sel1-1) == '\n') ? '' : '\n') + '```' + (ed.sel.charAt(0) == '\n' ? '' : '\n'),
					(ed.sel.substr(-1) == '\n' ? '' : '\n') + '```' + (ed.text.substr(ed.sel2,1) == '\n' ? '' : '\n'),
					ed);
		});
}

function btnMake(afterNode, label, title, tag1, tag2, noWrap) {
	var a = document.createElement('a');
	a.className = 'Button';
	a.innerHTML = label;
	a.title = title;
	a.style.setProperty('float','right');

	a.addEventListener('click',
		typeof(tag1) === 'function'
			? tag1
			: noWrap ? function(e){edInsertText(tag1, edInit(e.target));}
				: function(e){edWrapInTag(tag1, tag2, edInit(e.target));});

	var nparent = afterNode.parentNode;
	a.textAreaNode = nparent.textAreaNode;
	nparent.insertBefore(a, nparent.firstElementChild);
}



function edInit(btn) {

	var ed = {node: btn.parentNode.textAreaNode } ;

	ed.sel1 = ed.node.selectionStart;
	ed.sel2 = ed.node.selectionEnd,
	ed.text = ed.node.value;
	ed.sel = ed.text.substring(ed.sel1, ed.sel2);
	return ed;
}




function edWrapInTag(tag1, tag2, ed) {
	ed.node.value = ed.text.substr(0, ed.sel1) + tag1 + ed.sel + (tag2?tag2:tag1) + ed.text.substr(ed.sel2);
	ed.node.setSelectionRange(ed.sel1 + tag1.length, ed.sel1 + tag1.length + ed.sel.length);
	ed.node.focus();
}

function edInsertText(text, ed) {
	ed.node.value = ed.text.substr(0, ed.sel2) + text + ed.text.substr(ed.sel2);
	ed.node.setSelectionRange(ed.sel2 + text.length, ed.sel2 + text.length);
	ed.node.focus();
}