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. Sigh...
tl;dr: capability.policy
items in user.js make it into prefs.js but aren't taking effect.
FYI, I can reasonably have all users install user.js. But none of us have admin access to our machines, so that's about all I can do regarding configuration.
Thanks for any help!
Note:
Steps followed
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");Note: https://my-site.com isn't the real site.
capability.policy
items are not visible using about:config -- due to Bugzilla Bug 284673 - about:config hides "capability.policy" preferences. However, I do think these user.js items "worked" and that the file is in the correct folder, because afterwards:
just.mike
entry
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"); ... user_pref("just.mike", "test to see if user.js works.");
Zallowclipboard
everywhere so that the policynames
line came first due to alphabetization, but that didn't work either.)
onpaste
event with the following:
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);
Permission denied for <https://my-site.com> to get property XPCComponents.classesNote: 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.
createInstance
was used on the first line instead of getService
, but the first line still generates the same error text:
var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard); // ...
Environment (This is out of my control)
References
In short, I don't think it's possible to do what you want to do how you want to do it. Mozilla (and most modern browsers) limit clipboard access for security reasons. That you seem to have easily gotten around this restriction in IE is not a comforting thought.
That being said, plugins have different restrictions than web pages, and you may be able to get around this limitation by writing one (or maybe even leveraging flash). Even with a plugin, though, I suspect that the only way for you to block someone from pasting something into a web form (or whatever) would be to preemptively scrub their clipboard.
If all you want to do is prevent certain strings from being entered into a text box, you're best bet is to monitor events on the text box itself.
If I'm reading you right, though, it sounds like you want to force your users to type (and only type) something into a form, and I can't think of a trivial way of doing this. A couple non-trivial options:
readonly
, and pop-up a virtual keyboard to force the user to "type" into
the box using their mouse. (You'd have to build the keyboard yourself out of HTML and
JavaScript, or find a suitable solution somewhere.)oninput
event; if the textbox changes more quickly then what a person could
reasonably be expected to type, reject the changes.Neither of those are exactly pretty, but if you want to create solutions using open web technologies, then you have to accept the limitations of the system you're building on top of, as well as its benefits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With