Geekhub

geekhub增强插件

Verze ze dne 20. 10. 2020. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Geekhub
// @namespace    https://geekhub.com
// @version      1.0
// @description  geekhub增强插件
// @author       Leetao
// @match        https://geekhub.com/*
// @require      https://cdn.jsdelivr.net/npm/marked/marked.min.js
// @grant GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    var initMarkdown = function() {
        var divNode = document.createElement('div');
        divNode.setAttribute('id','markdown-preview-comment');
        var comment = document.getElementById("comment-box");
        if(comment != null) {
            comment.parentElement.appendChild(divNode);
        };
        var el = document.getElementById('comment-box');
        if(el != null){
            el.addEventListener('input',function () {
                var value = document.getElementById('comment-box').value;
                console.log(value);
                if(value != null) {
                    document.getElementById('markdown-preview-comment').innerHTML = marked(value);
                }
            });
        }

        var postDivNode = document.createElement('div');
        postDivNode.setAttribute('id','markdown-preview-post');
        var post = document.getElementById("post_content");
        if(post != null) {
            post.parentElement.appendChild(postDivNode);
        };
        var e2 = document.getElementById('post_content');
        if(e2 != null){
            e2.addEventListener('input',function () {
                var value = document.getElementById('post_content').value;
                if(value != null) {
                    document.getElementById('markdown-preview-post').innerHTML = marked(value);
                }
            });
        }
    }


    var upload = function(fileList) {
        for(var j = 0; j < fileList.length; j++) {
            var formData = new FormData();
            formData.append('smfile', fileList[j]);
            var response = GM_xmlhttpRequest({
                method: "post",
                url: 'https://sm.ms/api/v2/upload',
                data: formData,
                onload: function(r) {
                    var json = JSON.parse(r.responseText);
                    if (json.success == true){
                        var url = json.data.url;
                        var name = json.data.storename;
                        if(document.getElementById('comment-box') != null) {
                            document.getElementById('comment-box').value += "![alt "+ name +"](" + url + ")";
                        }
                        if(document.getElementById("post_content") != null) {
                            document.getElementById('post_content').value += "![alt "+ name +"](" + url + ")";
                        }
                    } else {
                        alert("上传失败");
                    }
                }
            });
        }

    }



    document.addEventListener('paste', function (event) {
        var items = event.clipboardData && event.clipboardData.items;
        var fileList = [];
        var urlList = [];
        if (items && items.length) {
            // 检索剪切板items
            for (var i = 0; i < items.length; i++) {
                if (items[i].type.indexOf('image') !== -1) {
                    fileList.push(items[i].getAsFile());
                }
            }
        }
        // 此时fileList就是剪切板中的图片文件列表
        if(fileList.length !== 0) { // 不为0,则上传到 sm.ms
            upload(fileList);
        }
    });

    initMarkdown();

})();