Wrapper for Clipboard API and ZeroClipboard as alternative.
Uses execCommand if supported otherwise fallback to ZeroClipboard library
- IE10+
- Chrome 43+,
- Opera 29+
See ZeroClipboard
Copy target's value on click:
document.addEventListener('DOMContentLoaded', function () {
Clipboard.copy('.copy', function (e) {
return e.target.value;
});
})Copy static text:
Clipboard.copy('.copy', 'Hello, world!');Handle events:
Clipboard.on('copy', console.log.bind(console));
Clipboard.on('error', console.error.bind(console));Define custom driver:
new Clipboard.Driver('prompt', {
checkSupport: function () {
return true;
},
events: [ ],
copy: function (elem, callback) {
elem.forEach(function (item) {
var handler = function (e) {
window.prompt("Copy to clipboard: Ctrl+C, Enter", this.callbackToString(callback, e.target));
}.bind(this);
item.addEventListener('mousedown', handler, false);
this.events.push({
elem: item,
handler: handler
})
}, this);
},
destroy: function () {
this.events.forEach(function (item) {
item.elem.removeEventListener('mousedown', item.handler, false);
});
this.events.length = 0;
}
});Configure
Clipboard.config({
baseDriver: 'native',
ZeroClipboard: window.ZeroClipboard
});===
When fires a mousedown event on a target, will copy text from a callback to the copy buffer.
-
{String|Array|HTMLElement|HTMLCollection}target` Selector or list of DOMElements to be listened -
{String} callbackStatic text for the copy buffer -
{Function} callbackRetrieves text for the copy buffer -
{ClipboardCustomEvent} callback.arguments[0]Custom clipboard event
Triggers copy event, if text was copied. Otherwise triggers error event.
===
Unbind all events, removes cached data and fake element
Triggers destroy event
===
Clipboard driver interface.
Driver.using Current driver name
Driver.current() Current driver
Driver.use({String} name) Use given driver
Driver.has({String} name) Is driver declared
Driver.get({String} name) Get driver by name
Driver.register({ClipboardDriver} driver) Declare new driver
Driver.remove({String} name) Remove driver
new Clipboard.Driver(name, driver) Creates and register new driver.
Use Clipboard.Driver to declare custom clipboard driver.
{String} nameDriver name{Object} driverDriver prototype{Function} driver.checkSupportValidates driver compatability{Function} driver.copyCopy function{Function} driver.destroyDestroys driver{Object} driver.configSet properties to global config
Example:
new Clipboard.Driver('custom', {
checkSupport: function () {
//...
},
copy: function (elem, callback) {
//...
},
destroy: function () {
//...
}
});Subscribe on clipboard events:
Clipboard.on('copy', function(e) {
console.log(e);
});or subscribe on event once:
Clipboard.one('copy', function(e) {
console.log(e);
});Arguments:
{String} nameEvent name{Function} callbackCallback function{Object} [context]Callback context
Unsubscribe from clipboard events:
Clipboard.off('copy', myCallback);Triggered new event:
Clipboard.trigger('customEvent', {
target: document.body
});Interface:
{DOMElement} e.targetHandled target element{String} e.clipboardTypeHandled driver's name{String} e.textCopied text (only for ``copy` event){String} e.nameError name (only forerrorevent){String} e.messageError message (only forerrorevent){Date} e.timeStampTimestamp