Search code examples
javascriptbrowsergreasemonkeyclipboardcopy-paste

Script to enable "paste" on Website


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


Solution

    • 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() };