title: How do you configure Firefox to allow Javascript to intercept a value pasted from the clipboard?
body: <p>I need to filter out certain characters when a user pastes from the clipboard. (I don't actually want to modify what's in the clipboard.) I already have this working in IE. <em>Sigh</em>...</p>
  
  <p><strong>tl;dr:</strong> <code>capability.policy</code> items in <strong>user.js</strong> make it into <strong>prefs.js</strong> but aren't taking effect.</p>
  
  <p>FYI, I can reasonably have all users install <strong>user.js</strong>. But none of us have admin access to our machines, so that's about all I can do regarding configuration.</p>
  
  <p>Thanks for any help!</p>
  
  <p><strong>Note:</strong></p>
  
  <ol>
  <li>For the purposes of this question, I do not want to "get around" accessing the clipboard -- <br>E.g., I do not want to operate on the value of the DOM element <em>after</em> a paste occurs 
  
  <li>I do not want a solution that requires a <a href="https://en.wikipedia.org/wiki/Comparison_of_JavaScript_frameworks" rel="nofollow noreferrer">framework/library</a> (like jQuery, Dojo, Prototype, YUI, ...)
  </ol>
  
  <p><strong>Steps followed</strong></p>
  
  <ol>
  <li>Determined location of my <strong>profile</strong> folder by going to URL <strong>about:support</strong> =&gt; Profile Directory =&gt; <kbd>Open Containing Folder</kbd>
  <br>
  <br>
  
  <li><em>Tried</em> to <a href="https://developer.mozilla.org/en/Midas/Security_preferences" rel="nofollow noreferrer">enable clipboard in Firefox</a> by creating <strong>user.js</strong> with the following lines:
  <pre>
  user_pref("just.mike", "test to see if user.js works.");
  user_pref("capability.policy.policynames", "allowclipboard");
  user_pref("capability.policy.allowclipboard.sites", "https://my-site.com");
  user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
  user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess");
  </pre>
  <em>Note: https://my-site.com isn't the real site.</em>
  <br>
  <br>
  
  <li>As it turns out, the <code>capability.policy</code> items are <em>not</em> visible using <strong>about:config</strong> -- due to <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=284673" rel="nofollow noreferrer">Bugzilla Bug 284673 - about:config hides "capability.policy" preferences</a>. However, I do think these <strong>user.js</strong> items "worked" and that the file is in the correct folder, because afterwards:
  <ul>
  <li><strong>about:config</strong> showed the <code>just.mike</code> entry
  <li><strong>prefs.js</strong> contained all the new lines <em>but in a different order</em> -- alphabetized:
  <pre>
  user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
  user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess");
  user_pref("capability.policy.allowclipboard.sites", "https://my-site.com");
  user_pref("capability.policy.policynames", "allowclipboard");
  <em>...</em>
  user_pref("just.mike", "test to see if user.js works.");
  </pre>
  </ul>
  (FYI, I also tried <code>Zallowclipboard</code> everywhere so that the <code>policynames</code> line came first due to alphabetization, but that didn't work either.)
  <br>
  <br>
  
  <li><em>Attempted</em> to <a href="https://developer.mozilla.org/en/Using_the_Clipboard#Pasting_Clipboard_Contents" rel="nofollow noreferrer"><strong>read</strong> the clipboard in Firefox</a> after an <code>onpaste</code> event with the following:
  <pre>
  var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
  if (!clipboard) {
      throw new Error('internal error -- could not create clipboard object');
  }
  
  var transferable = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
  if (!transferable) {
      throw new Error('internal error -- could not create transferable object');
  }
  
  transferable.addDataFlavor("text/unicode");
  clipboard.getData(transferable, clipboard.kGlobalClipboard);
  
  var clipboard_data = new Object();
  var clipboard_length = new Object();
  
  transferable.getTransferData("text/unicode", clipboard_data, clipboard_length);
  
  var clipboard_text = '';
  if (!!clipboard_data) {
      var clipboard_nsISupportsString = clipboard_data.value.QueryInterface(Components.interfaces.nsISupportsString);
      clipboard_text = clipboard_nsISupportsString.data.substring(0, clipboard_length.value / 2);
  }
  
  return(clipboard_text);
  </pre>
  <br>
  
  <li>The code fails on the <em>first</em> line, returning this error in the Firebug console:
  <pre>
  Permission denied for &lt;https://my-site.com&gt; to get property XPCComponents.classes
  </pre>
  <em>Note: I am actually running it on the real URL, which does get shown in the error message -- I've just changed it to a dummy name here.</em>
  <br>
  <br>
  
  <li>I also saw <a href="https://stackoverflow.com/questions/7713182/copy-to-clipboard-for-all-browsers-using-javascript">some</a> other examples where <code>createInstance</code> was used on the first line instead of <code>getService</code>, but the <em>first</em> line still generates the same error text:
  <pre>
  var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
  // <em>...</em>
  </pre>
  </ol>
  
  <p><br></p>
  
  <p><strong>Environment</strong> <em>(This is out of my control)</em></p>
  
  <ul>
  <li>No admin access to computer
  <li>Windows Vista
  <li>Mozilla Firefox 10
  </ul>
  
  <p><strong>References</strong></p>
  
  <ul>
  <li><a href="https://developer.mozilla.org/en/Midas/Security_preferences" rel="nofollow noreferrer">Enable clipboard in Firefox</a>
  <li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=284673" rel="nofollow noreferrer">Bugzilla Bug 284673 - about:config hides "capability.policy" preferences</a>
  <li><a href="https://developer.mozilla.org/en/Using_the_Clipboard#Pasting_Clipboard_Contents" rel="nofollow noreferrer">Access the clipboard in Firefox</a>
  <li><a href="https://stackoverflow.com/questions/7713182/copy-to-clipboard-for-all-browsers-using-javascript">Access the clipboard in Firefox in a <em>slightly</em> different way</a>
  <li><a href="http://www.google.com/search?q=%22Permission+denied+for%22+%22to+get+property+XPCComponents.classes%22" rel="nofollow noreferrer">google: "Permission denied for" "to get property XPCComponents.classes"</a>
  </ul>