Hide backticks on Hackpad

Hide the ugly backticks around code-formatted text on Hackpad.

目前為 2015-12-08 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Hide backticks on Hackpad
// @namespace    http://zesty.ca/
// @version      1.1
// @description  Hide the ugly backticks around code-formatted text on Hackpad.
// @author       Ka-Ping Yee
// @match        https://*.hackpad.com/*
// @grant        none
// ==/UserScript==
/* jshint -W097 */
'use strict';

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}

if (typeof ace !== 'undefined') {
    // Make backticks very small and almost invisible.  We don't set them
    // to display: none because that screws up the editor; you still have to
    // be able to insert and delete backticks to control formatting.
    addGlobalStyle('span { font-weight: normal; opacity: 0.9; }');
    addGlobalStyle('span.code { font-size: 95%; color: #800; margin: 0 !important; padding: 1px 3px !important; box-shadow: none; background: #f4f4f4; }');
    addGlobalStyle('span.code.hidebackticks { user-select: none; font-size: 6px; font-family: arial; opacity: 0.2; margin: 0 -1px !important; padding: 1px 0 !important; }');

    var originalAceEditor = ace.editor;
    ace.editor = function(a, b) {
        var result = new originalAceEditor(a, b);
        var originalGetSpansForLine = result.getSpansForLine;

        result.getSpansForLine = function(dummy, appender) {
            originalGetSpansForLine.call(result, dummy, function(text, cls) {
                if (cls.match(/ code$/)) {
                    // We can't delete the backticks altogether or the next
                    // pass of the formatter won't be able to tell that the
                    // text should still be monospaced.  So, we make separate
                    // spans for the backticks and use CSS to hide them.
                    text = text.replace(/^`(.*)`$/, '$1');
                    appender('`', cls + ' hidebackticks');
                    appender(text, cls);
                    appender('`', cls + ' hidebackticks');
                } else appender(text, cls);
            });
        };
        return result;
    };
}

// Whenever the user completes a text selection, this adjusts the ends of the
// selection to avoid picking up leading or trailing backticks.
document.body.addEventListener('mouseup', function() {
    var sel = window.getSelection();
    if (sel.rangeCount === 0) return;
    var range = sel.getRangeAt(0);
    var start = range.startContainer, soff = range.startOffset;
    var end = range.endContainer, eoff = range.endOffset;
    if (!start || !end || start === end) return;
    while (true) {
        if (start === end) break;
        if (start.textContent.substring(soff).trim() === '' ||
            start.parentNode.className.match(/ hidebackticks$/)) {
            start = start.parentNode.nextSibling.childNodes[0];
            soff = 0;
            range.setStart(start, soff);
            continue;
        }
        if (end.textContent.substring(0, eoff).trim() === '' ||
            end.parentNode.className.match(/ hidebackticks$/)) {
            end = end.parentNode.previousSibling.childNodes[0];
            eoff = end.textContent.length;
            range.setEnd(end, eoff);
            continue;
        }
        break;
    }
    sel.empty();
    sel.addRange(range);
});