JianShu Formatter 简书Markdown格式转换

简书Markdown格式转换

目前為 2019-03-31 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         JianShu Formatter 简书Markdown格式转换
// @namespace    http://tampermonkey.net/
// @namespace    https://www.jianshu.com/u/15893823363f
// @require https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js
// @version      2.2
// @description  简书Markdown格式转换
// @author       Zszen
// @match        https://www.jianshu.com/writer
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    //var targetEditor = $("#arthur-editor");

initMe()

function initMe(){
setTimeout(function(){
    var btUpdate = $("a.fa.fa-columns");
    if(!btUpdate){
       initMe();
       return;
    }
    btUpdate.on('click',initMe);
	//
	console.log( $("ul.clearfix"));
    var addedBt = $('<li class="_2zLpt"><a class="fa format-code" data-action="">格</a></li>');
    $("ul.clearfix").append(addedBt);
    addedBt.on('click', formatInput)
    addedBt = $('<li class="_2zLpt"><a class="fa insert-title" data-action="">插</a></li>');
    $("ul.clearfix").append(addedBt);
    addedBt.on('click', addTitle)
},800)
}

function formatInput(){
    var txt = $("textarea");
    //console.log(txt.val());
    var str = txt.val();
    str = str.replace(/···/g,'```')
    str = str.replace(/·/g,'`')
    str = str.replace(/〈|《/g,'<')
    str = str.replace(/〉|》/g,'>')
    //str = str.replace(//g,'<')
    //str = str.replace(//g,'>')
    str = str.replace(/「|『/g,'{')
    str = str.replace(/』|」/g,'}')
    //console.log(str);
    txt.val(str)
}

function addTitle(){
    var txt = $("textarea");
    //var startPos = txt.selectionStart;
    //console.log(startPos)
	insertAtCursor(txt,"\n___\n####")
	setSelectionRange(txt,txt.value.length,txt.value.length);
}

function insertAtCursor(myField, myValue) {
    if (document.selection) {
        //IE support
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        // sel.select();
    } else if (myField.selectionStart || myField.selectionStart == '0') {
        //MOZILLA/NETSCAPE support
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        var beforeValue = myField.value.substring(0, startPos);
        var afterValue = myField.value.substring(endPos, myField.value.length);

        myField.value = beforeValue + myValue + afterValue;

        myField.selectionStart = startPos + myValue.length;
        myField.selectionEnd = startPos + myValue.length;
        // myField.focus();
    } else {
        //myField.value += myValue;
        myField.val(myField.val()+myValue)
        // myField.focus();
    }
}

function setSelectionRange(input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	}else if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

})();