-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
75 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,100 @@ | ||
<input hidden id="copy" /> | ||
<script> | ||
//拿来主义(真香)^_^,Clipboard 实现摘自掘金 https://juejin.im/post/5aefeb6e6fb9a07aa43c20af | ||
window.Clipboard = (function(window, document, navigator) { | ||
var textArea, | ||
//拿来主义(真香)^_^,Clipboard 实现摘自掘金 https://juejin.im/post/5aefeb6e6fb9a07aa43c20af | ||
window.Clipboard = (function (window, document, navigator) { | ||
var textArea, | ||
copy; | ||
// 判断是不是ios端 | ||
function isOS() { | ||
return navigator.userAgent.match(/ipad|iphone/i); | ||
} | ||
//创建文本元素 | ||
function createTextArea(text) { | ||
textArea = document.createElement('textArea'); | ||
textArea.value = text; | ||
textArea.style.width = 0; | ||
textArea.style.height = 0; | ||
textArea.clientHeight = 0; | ||
textArea.clientWidth = 0; | ||
document.body.appendChild(textArea); | ||
} | ||
//选择内容 | ||
function selectText() { | ||
var range, | ||
// 判断是不是ios端 | ||
function isOS() { | ||
return navigator.userAgent.match(/ipad|iphone/i); | ||
} | ||
//创建文本元素 | ||
function createTextArea(text) { | ||
textArea = document.createElement('textArea'); | ||
textArea.value = text; | ||
textArea.style.width = 0; | ||
textArea.style.height = 0; | ||
textArea.clientHeight = 0; | ||
textArea.clientWidth = 0; | ||
document.body.appendChild(textArea); | ||
} | ||
//选择内容 | ||
function selectText() { | ||
var range, | ||
selection; | ||
if (isOS()) { | ||
range = document.createRange(); | ||
range.selectNodeContents(textArea); | ||
selection = window.getSelection(); | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
textArea.setSelectionRange(0, 999999); | ||
} else { | ||
textArea.select(); | ||
if (isOS()) { | ||
range = document.createRange(); | ||
range.selectNodeContents(textArea); | ||
selection = window.getSelection(); | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
textArea.setSelectionRange(0, 999999); | ||
} else { | ||
textArea.select(); | ||
} | ||
} | ||
} | ||
//复制到剪贴板 | ||
function copyToClipboard() { | ||
try{ | ||
document.execCommand("Copy") | ||
}catch(err){ | ||
alert("复制错误!请手动复制!") | ||
//复制到剪贴板 | ||
function copyToClipboard() { | ||
try { | ||
document.execCommand("Copy") | ||
} catch (err) { | ||
alert("复制错误!请手动复制!") | ||
} | ||
document.body.removeChild(textArea); | ||
} | ||
document.body.removeChild(textArea); | ||
} | ||
copy = function(text) { | ||
createTextArea(text); | ||
selectText(); | ||
copyToClipboard(); | ||
}; | ||
copy = function (text) { | ||
createTextArea(text); | ||
selectText(); | ||
copyToClipboard(); | ||
}; | ||
return { | ||
copy: copy | ||
}; | ||
})(window, document, navigator); | ||
return { | ||
copy: copy | ||
}; | ||
})(window, document, navigator); | ||
function copyCode (e) { | ||
if (e.srcElement.tagName === 'SPAN' && e.srcElement.classList.contains('copy-code')) { | ||
let code = e.currentTarget.querySelector('code'); | ||
var text = code.innerText; | ||
if (e.srcElement.textContent === '复制成功') { | ||
console.log('复制操作频率过高'); | ||
return; | ||
function copyCode(e) { | ||
if (e.srcElement.tagName === 'SPAN' && e.srcElement.classList.contains('copy-code')) { | ||
let code = e.currentTarget.querySelector('code'); | ||
var text = code.innerText; | ||
if (e.srcElement.textContent === '复制成功') { | ||
console.log('复制操作频率过高'); | ||
return; | ||
} | ||
e.srcElement.textContent = '复制成功'; | ||
(function (elem) { | ||
setTimeout(() => { | ||
if (elem.textContent === '复制成功') { | ||
elem.textContent = '复制代码' | ||
} | ||
}, 1000); | ||
})(e.srcElement) | ||
Clipboard.copy(text); | ||
} | ||
e.srcElement.textContent = '复制成功'; | ||
(function(elem){ | ||
setTimeout(() => { | ||
if (elem.textContent === '复制成功') { | ||
elem.textContent = '复制代码' | ||
} | ||
}, 1000); | ||
})(e.srcElement) | ||
Clipboard.copy(text); | ||
} | ||
} | ||
let pres = document.querySelectorAll('pre'); | ||
pres.forEach(pre => { | ||
let code = pre.querySelector('code'); | ||
let copyElem = document.createElement('span'); | ||
copyElem.classList.add('copy-code'); | ||
copyElem.textContent = '复制代码'; | ||
pre.appendChild(copyElem); | ||
pre.onclick = copyCode | ||
}) | ||
let pres = document.querySelectorAll('pre'); | ||
pres.forEach(pre => { | ||
let code = pre.querySelector('code'); | ||
let copyElem = document.createElement('span'); | ||
copyElem.classList.add('copy-code'); | ||
copyElem.textContent = '复制代码'; | ||
pre.appendChild(copyElem); | ||
pre.onclick = copyCode | ||
}) | ||
</script> | ||
<script src="<%= site.customConfig.cdn %>/media/js/motion.js"></script> | ||
<% if (site.customConfig.bgMusic !== 'aplayer') { %> | ||
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/smooth-scroll/dist/smooth-scroll.polyfills.min.js"></script> | ||
<script> | ||
var scroll = new SmoothScroll('a[href*="#"]', { | ||
speed: 500 | ||
}); | ||
</script> | ||
<% } %> | ||
<!-- <%- include('./search') %> --> | ||
<%- include('./mouse-click') %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters