2015-09-25 63 views
2

该问题涉及特定网站:tickets order on NS.nl。在该页面上有输入电子邮件的文本输入字段,并且该字段禁用了Ctrl-V(粘贴)。在网站上启用“粘贴”的脚本

问题:什么Greasemonkey脚本将启用粘贴在字段上?

我已经看过成各种解决方案,即:

,来到下面的脚本(不幸)做不工作给定的位点(与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); 

延迟的执行(通过setTimeout())的使用,因为该网站动态地构建的形式。在“有问题”的元素显示在下面的截图:

Form field element that has disabled paste

回答

2
  • 解除绑定的事件,你应该提供原始处理程序由页面设置:

    // ==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); 
    }); 
    
  • 另一种方法,原来只在Chrome中工作的是为元素属性分配直接事件侦听器,因此它将具有比连接到的站点侦听器更高的优先级。在你的听众阻止其他侦听器通过stopImmediatePropagation看到事件:

    var input = document.querySelector("[data-regex=email], [data-regex=emailRepeat]"); 
    input.onpaste = input.oncopy = function(event) { event.stopImmediatePropagation() }; 
    
+0

太好了!我认为我在某处看到了这个解决方案,但是肯定搞砸了。我注意到这个脚本只有在我从上面提供的setTimeout()中调用时才起作用(意思是说,我按照您提供的字面意思,它不起作用,至少在FF中)。我尝试使用'$(document).on('copy paste','[data-regex = email],[data-regex = emailRepeat]',function(event){event.stopImmediatePropagation();});''但那不起作用。有任何想法吗? –

+0

查看最新的答案。请注意,代替使用计时器,只需将代码包装在'$(function(){.......});'中,以便在加载文档和jQuery时执行。 – wOxxOm

+0

不幸的是,包装到'$(function(){})'不适合我。它适合你吗?实际上,我不明白为什么它应该这样做,因为一旦给定节点连接到DOM,'$(document).on('events','selector',function)'也会被触发(所以我认为这是更好的选择工作正常)。 –