v2exMarkdown

为v2ex而生的markdown渲染

Stan na 12-06-2018. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         v2exMarkdown
// @namespace    https://github.com/hundan2020/v2exMarkdown
// @version      0.3
// @description  为v2ex而生的markdown渲染
// @author       hundan,ccsiyu
// @match        https://www.v2ex.com/t/*
// @require      https://cdn.staticfile.org/showdown/1.8.6/showdown.js
// @require      https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js
// @grant        none
// ==/UserScript==

(function () {
	String.prototype.replaceAll = function (search, replacement) {
		var target = this;
		return target.replace(new RegExp(search, 'g'), replacement);
	};

	var markdownSwitch = true;
	$.when(true).then(function () {
		if (markdownSwitch) {
			processMarkdown();
		}
	});

	function processMarkdown() {
		if (window.location.href.indexOf("mwap") > -1) { // for mobile site // dummy here
		} else { // desktop site
			$("div.reply_content").each(function () {
				var postMain = $(this)[0];
				var postText = postMain.innerText || postMain.textContent;
				var postContentLines = postText.split("\n");
				var converter = new showdown.Converter({
						simplifiedAutoLink: true
					});
				var innerHTML = "";
				postContentLines.forEach(function (item, index) {
					if (item.substring(0, 4) === "<br>"){
						innerHTML += converter.makeHtml(item.substring(4));
					}else if (item.charAt(0) == '<'){
						innerHTML += item;
					}else{
						innerHTML += converter.makeHtml(item);
					}
				});
				postMain.innerHTML = innerHTML;
			});
		}
	}
})();