I know that question was posted long ago, but I needed to check how google does that, so maybe someone will find that useful.
Actually google uses also system clipboard but it's a bit tricky. In case when you use keyboard shortcut you can catch copy/paste/cut event on e.g. window:
window.addEventListener('copy', function (ev) {
console.log('copy event');
// you can set clipboard data here, e.g.
ev.clipboardData.setData('text/plain', 'some text pushed to clipboard');
// you need to prevent default behaviour here, otherwise browser will overwrite your content with currently selected
ev.preventDefault();
});
live example for keyboard shortcut: http://jsfiddle.net/tyk9U/
Unfortunately this is only solution for keyboard shortcut and there is a problem with context menu, because you can't access clipboard data without native (trusted) copy/cut/paste event. But google does interesting trick. There is API document.execCommand()
that allows you to run commands for contenteditable element and there is the command 'copy' which you can trigger it via document.execCommand('copy')
. But when you try this in console in Chrome it will return false
. I spent a bit of time investigating that and it turned out that they have Chrome extension installed, called "Google Drive" (go to chrome://apps/ and you can see it there) that enables clipboard access for domains drive.google.com and docs.google.com. Open some document or spreadsheet and type in console document.execCommand('copy')
- it will return true
. When you uninstall the extension you won't be able to use clipboard operations from context menu.
You can create such application for yourself with very simple manifest file (details here https://developer.chrome.com/apps/first_app):
{
"manifest_version": 2,
"name": "App name",
"description": "App description",
"version": "1.0",
"app": {
"urls": [
"http://your.app.url.here/"
],
"launch": {
"web_url": "http://your.app.url.here/"
}
},
"icons": {
"128": "x-128.png"
},
"permissions": [
"clipboardRead",
"clipboardWrite"
]
}
"permissions" field here enables clipboard operations.
Now when you have this enabled you can do document.execCommand('copy')
and it will work (will return true
). But this is not everything - document.execCommand('copy')
in chrome triggers copy event and you can catch it with the same code that is used for catching keyboard clipboard shortcuts. This is now Google does it.
Of course this description is valid only for Chrome.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…