Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Pasting password disabled at certain websites
    • Source of "yes paste" script
  • How to enable paste on HTML page with locked “Cmd+V”?

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

like image 728
dma_k Avatar asked Sep 25 '15 14:09

dma_k


People also ask

How do I enable paste on a website?

Enable copy paste on websites that have disabled copy paste. How to use: - Click on the extension icon - After a popup is opened, use the “Enable copy paste for all websites” checkbox. - Manually refresh the page and see if the extension has successfully enabled copy paste functionality on the website.

How do you copy paste on websites that don't allow it?

Press Ctrl + U (PC) or ⌘ Cmd + ⌥ Option + U (Mac). This option will display the website's source code in any web browser. As long as the text you want to copy isn't an image, you'll be able to copy it from the source code and paste it anywhere.

How do I enable copy and paste in HTML?

In a HTML page user should not be allowed to copy a text, but at the same time I want to give option for the user to select a particular text (for highlighting purpose). That means CTRL + C should be disabled and CTRL + A should be enabled.


2 Answers

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);
like image 175
jchavannes Avatar answered Oct 25 '22 08:10

jchavannes


  • 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() };
    
like image 37
wOxxOm Avatar answered Oct 25 '22 09:10

wOxxOm