15

The problem concerns the particular site: tickets order on NS.nl. On that page there is text input field to enter the email, and that field has Ctrl-V (paste) disabled.

Question: What Greasemonkey script will enable paste on the field?

I have looked into various solutions, namely:

and came to the following script which (unfortunately) does not work for the given site (testing with FF v40, Greasemonkey v3.4):

// Taken from http://userscripts-mirror.org/scripts/review/40760
unsafeWindow.disable_paste = function() { return true; };

// jQuery is already available on the page:
var $j = jQuery.noConflict();

// Site generates the form on-the-fly, so we schedule the necessary modifications for later:
setTimeout(function() {
    $j(document).off('copy paste', '[data-regex=email], [data-regex=emailRepeat]');
    $j(document).off('keyup keydown keypress cut copy paste');

    // Taken from https://stackoverflow.com/questions/28266689/how-to-enable-paste-on-html-page-with-locked-cmdv
    $j('*').each(function(){                                                
        $j(this).unbind('paste');
    });
}, 2000);

Delayed execution (via setTimeout()) is used because the site builds the form dynamically. The "problematic" element is shown on the following screenshot:

Form field element that has disabled paste

Community
  • 1
  • 1
dma_k
  • 10,431
  • 16
  • 76
  • 128

2 Answers2

20

The selected answer did not work for me. I found this simple script did work though:

document.addEventListener('paste', function(e) {
  e.stopImmediatePropagation();
  return true;
}, true);

Which was found here: https://www.howtogeek.com/251807/how-to-enable-pasting-text-on-sites-that-block-it/

Edit: May need to stop propagation for keydown as well for some websites.

document.addEventListener('keydown', function(e) {
  e.stopImmediatePropagation();
  return true;
}, true);
jchavannes
  • 2,440
  • 1
  • 26
  • 15
5
  • To unbind the events you should provide the original handler set by the page:

    // ==UserScript==
    // @name         Re-enable copy/paste of emails
    // @include      https://www.ns.nl/producten/s/railrunner*
    // @grant        none
    // ==/UserScript==
    
    $(function() {
        $(document).off("copy paste",
                        "[data-regex=email], [data-regex=emailRepeat]", 
                        $._data(document, "events").paste[0].handler);
    });
    
  • Another method that turned out to work only in Chrome is to assign a direct event listener for oncopy/onpaste element properties, thus it'll have a higher priority than the site's listeners attached to the document. In your listener prevent other listeners from seeing the event via stopImmediatePropagation:

    var input = document.querySelector("[data-regex=email], [data-regex=emailRepeat]");
    input.onpaste = input.oncopy = function(event) { event.stopImmediatePropagation() };
    
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Great! I think I saw this solution somewhere, but certainly messed up things. I have noticed that the script works only when called from `setTimeout()` as I provided above (meaning that it I take it literally as you provide, it does not work, at least in FF). I tried to use `$(document).on('copy paste', '[data-regex=email], [data-regex=emailRepeat]', function(event) { event.stopImmediatePropagation(); });` but that does not work. Any ideas? – dma_k Sep 28 '15 at 07:57
  • See the updated answer. Note that instead of using a timer it's sufficient to wrap the code in `$(function() { ....... });` so that it'll be executed once both the document and jQuery are loaded. – wOxxOm Sep 28 '15 at 08:41
  • Unfortunately, wrapping into `$(function() { })` does not work for me. It worked for you? Actually, I don't see why it should, as `$(document).on('events', 'selector', function)` is also triggered once given node is attached to DOM (so I would say it's better candidate to work fine). – dma_k Sep 28 '15 at 10:24
  • Yes, it works for me on FF42+Greasemonkey3.5 and Chrome+Tampermonkey. Have you tried the exact userscript I posted or have you modified something? – wOxxOm Sep 28 '15 at 10:39