79 lines
5.0 KiB
JavaScript
79 lines
5.0 KiB
JavaScript
|
//html5 给typecho添加 复制代码 功能
|
|||
|
// by 兔子昂
|
|||
|
var codeblocks = document.getElementsByTagName("pre")
|
|||
|
//循环每个pre代码块,并添加 复制代码
|
|||
|
|
|||
|
for (var i = 0; i < codeblocks.length; i++) {
|
|||
|
//显示 复制代码 按钮
|
|||
|
currentCode = codeblocks[i]
|
|||
|
currentCode.style = "position: relative;"
|
|||
|
var copy = document.createElement("div")
|
|||
|
copy.style = "position: absolute;right: 4px;\
|
|||
|
top: 4px;padding: 2px 8px;\
|
|||
|
margin: 8px;border-radius: 4px;cursor: pointer;"
|
|||
|
copy.innerHTML = '<svg t="1716432820088" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4617" width="20" height="20"><path d="M672 832 224 832c-52.928 0-96-43.072-96-96L128 160c0-52.928 43.072-96 96-96l448 0c52.928 0 96 43.072 96 96l0 576C768 788.928 724.928 832 672 832zM224 128C206.368 128 192 142.368 192 160l0 576c0 17.664 14.368 32 32 32l448 0c17.664 0 32-14.336 32-32L704 160c0-17.632-14.336-32-32-32L224 128z" fill="#5E6570" p-id="4618"></path><path d="M800 960 320 960c-17.664 0-32-14.304-32-32s14.336-32 32-32l480 0c17.664 0 32-14.336 32-32L832 256c0-17.664 14.304-32 32-32s32 14.336 32 32l0 608C896 916.928 852.928 960 800 960z" fill="#5E6570" p-id="4619"></path><path d="M544 320 288 320c-17.664 0-32-14.336-32-32s14.336-32 32-32l256 0c17.696 0 32 14.336 32 32S561.696 320 544 320z" fill="#5E6570" p-id="4620"></path><path d="M608 480 288.032 480c-17.664 0-32-14.336-32-32s14.336-32 32-32L608 416c17.696 0 32 14.336 32 32S625.696 480 608 480z" fill="#5E6570" p-id="4621"></path><path d="M608 640 288 640c-17.664 0-32-14.304-32-32s14.336-32 32-32l320 0c17.696 0 32 14.304 32 32S625.696 640 608 640z" fill="#5E6570" p-id="4622"></path></svg>'
|
|||
|
currentCode.appendChild(copy)
|
|||
|
//让所有 "复制"按钮 全部隐藏
|
|||
|
copy.style.visibility = "visible"
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
for (var i = 0; i < codeblocks.length; i++) {
|
|||
|
|
|||
|
|
|||
|
!function (i) {
|
|||
|
|
|||
|
//执行 复制代码 功能
|
|||
|
function copyArticle(event) {
|
|||
|
const range = document.createRange();
|
|||
|
|
|||
|
//范围是 code,不包括刚才创建的div
|
|||
|
range.selectNode(codeblocks[i].childNodes[0]);
|
|||
|
|
|||
|
const selection = window.getSelection();
|
|||
|
if (selection.rangeCount > 0) selection.removeAllRanges();
|
|||
|
selection.addRange(range);
|
|||
|
document.execCommand('copy');
|
|||
|
|
|||
|
codeblocks[i].childNodes[1].innerHTML = "复制成功"
|
|||
|
setTimeout(function () {
|
|||
|
codeblocks[i].childNodes[1].innerHTML = '<svg t="1716432820088" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4617" width="20" height="20"><path d="M672 832 224 832c-52.928 0-96-43.072-96-96L128 160c0-52.928 43.072-96 96-96l448 0c52.928 0 96 43.072 96 96l0 576C768 788.928 724.928 832 672 832zM224 128C206.368 128 192 142.368 192 160l0 576c0 17.664 14.368 32 32 32l448 0c17.664 0 32-14.336 32-32L704 160c0-17.632-14.336-32-32-32L224 128z" fill="#5E6570" p-id="4618"></path><path d="M800 960 320 960c-17.664 0-32-14.304-32-32s14.336-32 32-32l480 0c17.664 0 32-14.336 32-32L832 256c0-17.664 14.304-32 32-32s32 14.336 32 32l0 608C896 916.928 852.928 960 800 960z" fill="#5E6570" p-id="4619"></path><path d="M544 320 288 320c-17.664 0-32-14.336-32-32s14.336-32 32-32l256 0c17.696 0 32 14.336 32 32S561.696 320 544 320z" fill="#5E6570" p-id="4620"></path><path d="M608 480 288.032 480c-17.664 0-32-14.336-32-32s14.336-32 32-32L608 416c17.696 0 32 14.336 32 32S625.696 480 608 480z" fill="#5E6570" p-id="4621"></path><path d="M608 640 288 640c-17.664 0-32-14.304-32-32s14.336-32 32-32l320 0c17.696 0 32 14.304 32 32S625.696 640 608 640z" fill="#5E6570" p-id="4622"></path></svg>'
|
|||
|
}, 1000);
|
|||
|
//清除选择区
|
|||
|
if (selection.rangeCount > 0) selection.removeAllRanges(); 0
|
|||
|
}
|
|||
|
codeblocks[i].childNodes[1].addEventListener('click', copyArticle, false);
|
|||
|
|
|||
|
}(i);
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
function a(a, b, c) {
|
|||
|
if (document.selection) a.focus(), sel = document.selection.createRange(), c ? sel.text = b + sel.text + c : sel.text = b, a.focus();
|
|||
|
else if (a.selectionStart || "0" == a.selectionStart) {
|
|||
|
var l = a.selectionStart,
|
|||
|
m = a.selectionEnd,
|
|||
|
n = m;
|
|||
|
c ? a.value = a.value.substring(0, l) + b + a.value.substring(l, m) + c + a.value.substring(m, a.value.length) : a.value = a.value.substring(0, l) + b + a.value.substring(m, a.value.length);
|
|||
|
c ? n += b.length + c.length : n += b.length - m + l;
|
|||
|
l == m && c && (n -= c.length);
|
|||
|
a.focus();
|
|||
|
a.selectionStart = n;
|
|||
|
a.selectionEnd = n
|
|||
|
} else a.value += b + c, a.focus()
|
|||
|
}
|
|||
|
var b = (new Date).toLocaleTimeString(),
|
|||
|
c = document.getElementById("textarea") || 0;
|
|||
|
window.SIMPALED = {};
|
|||
|
window.SIMPALED.Editor = {
|
|||
|
daka: function() {
|
|||
|
a(c, "滴!学生卡!打卡时间:" + b, ",请上车的乘客系好安全带~")
|
|||
|
},
|
|||
|
zan: function() {
|
|||
|
a(c, " 文章写得不错,给你点个赞,继续加油哈!")
|
|||
|
},
|
|||
|
cai: function() {
|
|||
|
a(c, "骚年,我怀疑你写了一篇假的文章!")
|
|||
|
}
|
|||
|
};
|