Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.29k stars 424 forks source link

Implement clipboard readText #1503

Open AntoineTurmel opened 2 years ago

AntoineTurmel commented 2 years ago

Ability to retrieve clipboard content using GM.getClipboard

Available in most of browsers, see: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText

7nik commented 2 years ago

But you already can do it

navigator.clipboard.readText().then(console.log)

But the document must be focused on this moment (you can use GM.notification({ highlight: true }); to focus the tab), and the user must provide permission for reading the clipboard for every site where the clipboard is read.

AntoineTurmel commented 2 years ago
$( window ).scroll(function() {

    GM.notification({ highlight: true });
    navigator.clipboard.readText().then(console.log);
}

I have : Uncaught TypeError: GM.notification is not a function and without GM.notification I have : Uncaught TypeError: navigator.clipboard.readText is not a function

7nik commented 2 years ago

Works for me:

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        *
// ==/UserScript==

window.addEventListener("scroll", () => navigator.clipboard.readText().then(console.log));

What is your environment? (OS, browser, etc)

AntoineTurmel commented 2 years ago

ok so I added @grant GMnotification and replaced GM. by GM for clipboard.readText, an additional permission must be set for Firefox in manifest.json: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions clipboardRead

@7nik Firefox 100/Ubuntu/LInux

7nik commented 2 years ago

Clipboard API for web extensions is deprecated and replaced by Clipboard API for all websites (which is used in the code above) so you don't need this permission.

7nik commented 2 years ago

https://caniuse.com/mdn-api_clipboard_readtext

Firefox only supports reading the clipboard in browser extensions, using the "clipboardRead" extension permission.

Meh, FF still lives in the past century and is oversecured :(

nk9 commented 1 year ago

I'm running into this on FF as well. How does one add the additional permission to manifest.json in a Tampermonkey Userscript? I'm not packaging or distributing anything, and I don't see a place to add the manifest, so I'm not able to interact with the clipboard.

derjanb commented 1 year ago

How does one add the additional permission to manifest.json in a Tampermonkey Userscript?

Permissions in manifest.json are set when the extensions is bundled/created.

What is your use case for reading the clipboard?

ernstki commented 1 year ago

Meh, FF still lives in the past century and is oversecured :(

When your password manager uses the clipboard for data transfer, or you copy-paste secrets frequently, you become sensitive to these things. I for one don't want random websites being able to read clipboard data, so I actually kind of appreciate Mozilla dragging their heels on this API.

@derjanb The current use case I have is to enumerate page elements and copy some JSON to the clipboard. It's in the context of some infrequent web administration tasks where I don't feel the need to use a more heavyweight solution like Selenium. Edit: Oops, it appears what I was trying to do is already possible with GM_setClipboard. Never mind!