geekhub增强插件
当前为
// ==UserScript==
// @name Geekhub
// @namespace https://geekhub.com
// @version 0.1
// @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');
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').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) {
console.log(r);
var json = JSON.parse(r.responseText);
if (json.success == true){
var url = json.data.url;
var name = json.data.storename;
document.getElementById('comment-box').value += "";
} 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();
})();