fix(js): load copy button script in post only

This commit is contained in:
curben 2019-12-20 02:44:20 +00:00
parent e59bfdbca6
commit feeaca491f
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
3 changed files with 33 additions and 32 deletions

View File

@ -1,5 +1,8 @@
<% if (is_post()) { %> <% if (is_post()) { %>
<%- js({ src: '/libs/clipboard.js/2.0.4/clipboard.min.js', 'data-sri-fallback': '/js/clipboard.min.js', integrity: 'sha384-8CYhPwYlLELodlcQV713V9ZikA3DlCVaXFDpjHfP8Z36gpddf/Vrt47XmKDsCttu', crossorigin: 'anonymous', defer: true}) %> <%- js([
{ src: '/libs/clipboard.js/2.0.4/clipboard.min.js', 'data-sri-fallback': '/js/clipboard.min.js', integrity: 'sha384-8CYhPwYlLELodlcQV713V9ZikA3DlCVaXFDpjHfP8Z36gpddf/Vrt47XmKDsCttu', crossorigin: 'anonymous', defer: true },
'/js/copy-button.js'
]) %>
<% } %> <% } %>
<%/* javascript of Chameleon theme */%> <%/* javascript of Chameleon theme */%>

View File

@ -6,34 +6,3 @@ document.getElementById('searchClick').addEventListener('click', () => {
document.getElementById('searchClickMob').addEventListener('click', () => { document.getElementById('searchClickMob').addEventListener('click', () => {
searchFormMob.submit() searchFormMob.submit()
}, false) }, false)
/*
* Copy button
* Following functions only execute after
* the 'document' is ready or
* <script src> is executed.
*/
document.addEventListener('DOMContentLoaded', () => {
// Copy button
const copyCode = new ClipboardJS('.copy-button', {
target: (trigger) => {
return trigger.previousElementSibling
}
})
copyCode.on('success', (event) => {
event.clearSelection()
event.trigger.textContent = 'Copied'
window.setTimeout(() => {
event.trigger.textContent = 'Copy'
}, 2000)
})
copyCode.on('error', (event) => {
event.trigger.textContent = 'Press "Ctrl + C" to copy'
window.setTimeout(() => {
event.trigger.textContent = 'Copy'
}, 2000)
})
}, false)

View File

@ -0,0 +1,29 @@
/*
* Copy button
* Following functions only execute after
* the 'document' is ready or
* <script src> is executed.
*/
document.addEventListener('DOMContentLoaded', () => {
// Copy button
const copyCode = new ClipboardJS('.copy-button', {
target: (trigger) => {
return trigger.previousElementSibling
}
})
copyCode.on('success', (event) => {
event.clearSelection()
event.trigger.textContent = 'Copied'
window.setTimeout(() => {
event.trigger.textContent = 'Copy'
}, 2000)
})
copyCode.on('error', (event) => {
event.trigger.textContent = 'Press "Ctrl + C" to copy'
window.setTimeout(() => {
event.trigger.textContent = 'Copy'
}, 2000)
})
}, false)