X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/5a85c6bfca6be44dadd58eedd0d9f0d1d5619e5f..c544fb231402a1111fbc42dd7bae87aed6b0f4dc:/clipboard.js diff --git a/clipboard.js b/clipboard.js index cbf43d6..8ef21d1 100644 --- a/clipboard.js +++ b/clipboard.js @@ -5,29 +5,31 @@ function clipboardcopy(val) { 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); } }