return window.clipboardData.setData('text', val);
}
- if (netscape && netscape.security) {
- // request access to XPCOM api
- try {
- netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
- }
- catch(e) {
- if (copyhint)
- alert('Cannot access clipboard.\nSet boolean signed.applets.codebase_principal_support in about:config');
- copyhint = false;
- return false;
- }
+ if (document.queryCommandSupported('copy')) {
+ var textArea = document.createElement('textarea');
- // use nsIClipboard interface
+ // minimise styling in case it is (briefly) rendered
+ textArea.style.position = 'fixed';
+ textArea.style.top = 0;
+ textArea.style.left = 0;
+ textArea.style.width = '2em';
+ textArea.style.height = '2em';
+ textArea.style.padding = 0;
+ textArea.style.border = 'none';
+ textArea.style.outline = 'none';
+ textArea.style.boxShadow = 'none';
+ textArea.style.background = 'transparent';
+
+ // temporarily add input field to copy text from
+ textArea.value = val;
+ document.body.appendChild(textArea);
try {
- Components.classes['@mozilla.org/widget/clipboardhelper;1']
- .getService(Components.interfaces.nsIClipboardHelper)
- .copyString(val);
- return true;
- }
- catch(e) {
- alert('Copy failed');
- return false;
+ textArea.select();
+ document.execCommand('copy');
+ } catch (e) {
+ console.log('could not copy "'+val+'" to clipboard');
}
+ document.body.removeChild(textArea);
}
}