Last active
March 7, 2022 08:04
-
-
Save theopendle/08b405b91f4c1c62e401475456ff73c2 to your computer and use it in GitHub Desktop.
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
(function(window, document, Granite, $) { | |
console.log('copyAssetLink.js loaded'); | |
$(window).adaptTo("foundation-registry").register("foundation.collection.action.action", { | |
name: "cq.wcm.copyAssetLink", | |
handler: function(name, el, config, collection, selections) { | |
if (selections.length != 1) { | |
return; | |
} | |
let ids = selections.map(function(v) { | |
return $(v).data("foundationCollectionItemId"); | |
}); | |
let link = window.location.origin + ids[0]; | |
copy(link); | |
} | |
}); | |
function copy(content) { | |
let dummy = document.createElement('textarea'); | |
dummy.style.position = 'absolute'; | |
dummy.style.left = '-9999px'; | |
dummy.value = content; | |
document.body.appendChild(dummy); | |
dummy.select(); | |
try { | |
document.execCommand('copy'); | |
} catch { | |
let useragent = navigator.userAgent ? navigator.userAgent : 'unknown browser'; | |
log.warn('Copy to clipboard not supported by ' + useragent) | |
} finally { | |
document.body.removeChild(dummy); | |
} | |
} | |
})(window, document, Granite, Granite.$); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment