1

greetings to all forum users

I'd like to create a user script for Firefox that allow me to paste the content of the clipboard by a pressure of the central mouse button (I'm on Windows 8.1).

I'm a newbie and I found a script that maybe it can be a good starting point Endor8 /userChrome.js

// ==UserScript==
// @name           overwriteMiddleMousePaste.uc.js
// @namespace      http://d.hatena.ne.jp/Griever/
// @include        main
// ==/UserScript==

document.documentElement.addEventListener("click", function(event){
    if (event.button !== 1 || !gPrefService.getBoolPref('middlemouse.paste')) return;

    var localName = event.target.localName.toLowerCase();
    // 元のコード
    // if ((localName === 'input' || localName === 'textarea' || localName === 'textbox') && 
     if ((localName === 'input' || localName === 'textarea' || localName === 'textbox' || localName === 'tabbrowser' || localName === 'searchbar') && 
            document.commandDispatcher.getControllerForCommand("cmd_paste") ){
        goDoCommand("cmd_paste");
        event.preventDefault();
    }
}, true);

I modded it, however this isn't still a working script

// ==UserScript==
// @name           overwriteMiddleMousePaste.uc.js
// @namespace      http://d.hatena.ne.jp/Griever/
// @include        main
// ==/UserScript==

document.addEventListener("click", function(event){
    if (event.button !== 1) return;

    var localName = event.target.localName.toLowerCase();
    var command='cmd_paste'
    var controller=document.commandDispatcher.getControllerForCommand(command);

     if ((localName === 'input' || localName === 'textarea' || localName === 'textbox' || localName === 'tabbrowser' || localName === 'searchbar' || localName === 'iframe' || localName === 'frame' || localName === 'input:nth-child(3)') && controller.isCommandEnabled(command)){
        controller.doCommand(command);
        event.stopPropagation();
        event.preventDefault();
    }
}, true);

It doesn't work on any html element (search boxes, etc.) nor on Firefox sidebar. It only works on Firefox urlbar and findbar

ntfox
  • 11
  • 2
  • Combine this: [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) with this: [How to listen to middle-click in JavaScript?](https://stackoverflow.com/a/49255319/10549827). Does that answer your question? – matthew-e-brown Apr 30 '21 at 06:04
  • Thanks, I read the articles you suggested me for a long time, however I was not able to achieve my goal. I don't understand why I cannot paste the clipboard content on the input box of any webpages – ntfox Apr 30 '21 at 13:34
  • Oh, sorry about that! It seems like I misread your question last night -- you're looking to *paste*, not *copy*... I guess I was on StackOverflow a bit later than I should have been last night [How about this one](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard#reading_from_the_clipboard)? – matthew-e-brown Apr 30 '21 at 13:41

0 Answers0