Created
November 22, 2019 21:00
-
-
Save Sarapulov/e86910e17a878c2dba94c69a072d07cb to your computer and use it in GitHub Desktop.
Zendesk Help Center JS to inject Copy URL button into article page
This file contains hidden or 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
// Copy URL sharing button. CSS dependency: .share-url | |
(function() { | |
'use strict'; | |
if (window.location.href.indexOf('/articles/') > -1) { // only run on article page | |
if ( !$('.btn-copy').length ) { // add button if doesn't exist | |
$('.article-share .share').prepend('<li><a class="btn-copy share-url">Copy URL</a></li>'); | |
$('.btn-copy').on('click', copyStringToClipboard); | |
} | |
function copyStringToClipboard(event) { // copy article URL | |
event.preventDefault(); | |
var el = document.createElement('textarea'); | |
el.value = window.location.href; | |
el.setAttribute('readonly', ''); | |
el.style = { position: 'absolute', left: '-9999px' }; | |
document.body.appendChild(el); | |
el.select(); | |
try { | |
var successful = document.execCommand('copy'); | |
console.log('Article URL was ' + (successful ? 'copied' : 'not copied')); | |
var $btn = $('.btn-copy'); | |
if (successful) { | |
$btn.text('Copied!'); | |
setTimeout(function() { | |
$btn.text('Copy URL'); | |
}, 4000); | |
} | |
} catch(err) { | |
console.log('Oops, unable to copy Article URL', err); | |
} | |
document.body.removeChild(el); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment