SHARE
TWEET

GreasemonkeyJavaScript Copy to Clipboard button

a guest Feb 28th, 2012 20 Never
  1. <SCRIPT LANGUAGE="JavaScript">
  2.     <!-- Begin
  3.     function copyit(theField) {
  4.     var selectedText = document.selection;
  5.     if (selectedText.type == 'Text') {
  6.     var newRange = selectedText.createRange();
  7.     theField.focus();
  8.     theField.value = newRange.text;
  9.     } else {
  10.     alert('Alert: Select The text in the textarea then click on this button');
  11.     }
  12.     }
  13.     // End -->
  14.     </script>
  15.     <input onclick="copyit(this.form.text_select)" type="button" value="Click Here to Copy the Highlighted Text" name="copy_button">
  16.        
  17. var tc = textToCopy.replace(/nn/g, 'n');
  18. if (window.clipboardData) // IE
  19. {
  20.   window.clipboardData.setData("Text", tc);
  21. }
  22. else
  23. {
  24.   unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  25.   const clipboardHelper = Components.classes
  26.       ["@mozilla.org/widget/clipboardhelper;1"].
  27.       getService(Components.interfaces.nsIClipboardHelper);
  28.   clipboardHelper.copyString(tc);
  29. }
RAW Paste Data
Top