2015-09-25 64 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)'也會被觸發(所以我認爲這是更好的選擇工作正常)。 –