原创文|Space9
Baklib是一款优雅的在线写作平台,界面清爽,满足个人用户的撰写、托管和分享。内置的多款展示模板满足不同用户的使用场景。适合给企业做帮助文档、知识库、产品手册、FAQ文档等,适合给个人做独立的博客和日记本。用户还可以对站点开放状态进行管理(可设置成公开/私密和密码访问),只给你的好友查看。
不过在本人的使用习惯上还有一些不足,在Markdown编辑模式下,插入图片不是很方便,所以写下baklib Markdown助手为图片上传提供便捷,同时也希望这个脚本也能帮助到其他有共同需求的人
使用方法
- 浏览器安装Tampermonkey扩展程序。
- 进入Tampermonkey管理面板,点击加号,复制下面源码到编辑器里面,保存即可。
- 在Baklib写文章界面右下角可以看到选择文件按钮,点击按钮,上传图片之后直接Ctrl + C粘贴到文章中即可。
源码
// ==UserScript==
// @name baklib Markdown助手
// @namespace https://www.baklib.com/
// @version 1.0
// @description baklib在使用Markdown写作中的图片上传工具,上传图片之后直接Ctrl + C粘贴到文章中即可。
// @author 独居者
// @match https://*.baklib.com/-/admin/articles/*
// @grant GM.setClipboard
// @grant unsafeWindow
// ==/UserScript==
(function () {
'use strict';
unsafeWindow.btnUploadFile = function (e) {
var imgFile = e.target.files[0];
if (!/\/(?:jpeg|jpg|png|gif)/i.test(imgFile.type)) {
console.log("图片格式不支持");
alert("图片格式不支持");
return;
}
//获取token
var data = new FormData();
data.append("file_name", imgFile.name);
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
//发送图片
var data_s = new FormData();
var jsoncontent = JSON.parse(this.responseText);
data_s.append("key", jsoncontent.uploadFileName);
data_s.append("token", jsoncontent.token);
data_s.append("file", imgFile);
var xhr_s = new XMLHttpRequest();
xhr_s.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
var resdata = JSON.parse(this.responseText);
//拼接Markdown图片地址
var markdownUrl = "![" + imgFile.name + "](" + resdata.url + ")";
//设置到剪贴板
GM.setClipboard(markdownUrl);
}
});
xhr_s.open("POST", jsoncontent.uploadHost);
xhr_s.send(data_s);
}
});
var curHref = window.document.location.href;
var pathName = window.document.location.pathname;
var pos = curHref.indexOf(pathName);
var hostPath = curHref.substring(0, pos);
xhr.open("POST", hostPath + "/uploads/token");
xhr.send(data);
}
function addimgbtn() {
var div = document.createElement("div");
div.innerHTML = '<div id="imgbtn" style="position:fixed;right:2em;bottom:2em;"><input class="md-image-tool__file_input" type="file" οnchange="btnUploadFile(event)" style="width:200px;" accept="image/*"></div>';
document.body.appendChild(div);
}
addimgbtn();
})();
源码打包
baklib Markdown助手.zip
https://474b.com/file/20114131-432857376
本文仅为个人学习使用,不得用于任何商业用途,否则后果自负!如侵犯到您的权益,请及时通知我,我会及时处理。