2

Note 1: I do not own the website that I'm trying to modify so the manual page edit is not possible.
Note 2: I need a PURE JavaScript (NOT jquery) solution.

I'll modify a web page by using a tampermonkey extension for Firefox v56.

Description: There is a webpage that has an <input> box. With a javascript code I've managed to create and insert a button near that <input> box. So after the button is inserted, page source code looks like this:

<form id="some_form" method="post" name="some_form">
    <section id="content">
        <div class="row"></div>
        <div class="row"></div>
        <div class="row">
            <div>
                <div>
                    <input id="info" name="info" placeholder="Enter info" value="" type="text">
                </div>
            </div>
            <button id="btn_p">Paste</button>
        </div>
    </section>
</form>

What I want: Once I click the <button> I want clipboard content to be pasted into <input> box. (I don't want to use CTRL+V)

What is the problem: At mozilla.org I've found a page that explains how to Interact with the clipboard by using document.execCommand("paste"). I've tried to use a javascript code that I've found as part of their example and modified it to work for my needs. JS code looks like this:

function paste() {
  var pasteText = document.querySelector("#info");
  pasteText.focus();
  document.execCommand("paste");
}

document.querySelector("#btn_p").addEventListener("click", paste);

That should work, BUT if I understood correctly, it can only work if the clipboard content is inserted between <textarea> tags and not into <input> field.

Question: Is there any (pure) javascript code that can be used to paste the clipboard content into an <input> field (with the click on the button), instead of pasting it with CTRL+V keyboard key combination?

Thanks in advance to anyone who will (hopefully) try to help me with this.

sarah997
  • 63
  • 1
  • 6
  • 1
    The MDN article you quote says that you need the `clipboardRead` permission, which must be set in an extension's manifest. i.e you won't be able to even paste in a textarea from web content. But since you are building an user-script, the manager is actually an extension, so there may be something allowing your script to access it, but for this you'd need to ask tampermonkey experts (added the tag for you). – Kaiido May 14 '18 at 05:16
  • Ps: according to [this Q/A](https://stackoverflow.com/questions/23475342/get-clipboard-contents-with-greasemonkey), they don't allow it either, so you'd have to build your own extension if you really need this feature (which sounds weird btw). – Kaiido May 14 '18 at 05:19
  • @Kaiido: dear Kaiido, 'thanks' for stopping me from getting any help here by marking my question as 'duplicate'. I've looked around and among many pages, the one you posted as 'duplicated by me' was asked in 2014 (4 years ago). As you see (if you tried to read it more carefully) at that mozilla MDN page it sais: "Firefox supports the "clipboardRead" permission from version 54, but does require an element in content editable mode, which for content scripts only works with a – sarah997 May 14 '18 at 06:06
  • 1
    ^^ You also stated that 'greasemonkey' do not supports it. Well, this is not greasemonkey. Not being able to do some things with greasemonkey is the reason why I (finally) after so many years, decided to start using tampermonkey. I mantioned that I'm gonna use javascript code with tampermonkey just because I had a situation where people will ask me 'how you plan to use JS' since you don't want jquery. That is the reason why I clearly stated every possible fact (in BOLD) so there will not be any missunderstandings. – sarah997 May 14 '18 at 06:09
  • But if you click on the "*[permission](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/permissions)*" link, you'll see that this permission is only available for extensions, the rest of the sentence even makes its availability narrower since even in an extension, content-scripts can only access it from a textarea. And regarding TamperMonkey vs GM, to this regard, reading TM docs shows that they didn't moved from GM rules here. – Kaiido May 14 '18 at 06:10
  • ^^^ Also, saying for some feature request that is 'weird'... well you have a right to have an oppinion, but to call my 'request' for a button to paste the clipboard instead using CTRL+V weird, when there is so many much 'wierder' things that people do with scripts, I don't know what kind of comment I should write regarding that. – sarah997 May 14 '18 at 06:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170978/discussion-between-kaiido-and-sarah997). – Kaiido May 14 '18 at 06:14
  • I'm sure there is a way that what I'm searching for can be done. Bad thing is that I"m a newbie so I would have to look around to find it. Since its supported from Firefox v54 I guess not many people tried to 'exploit' such feature yet, which may explain why I didn't found a 'sample code' yet. It is 'weird' that for my own purpose, (if what you're saying is true), now I have to 'learn' how to make extensions just so I can get a simple 'paste' option as a feature for my browser. – sarah997 May 14 '18 at 06:17
  • @Kaiido is correct, [even Tampermonkey chooses not to implement a way to **get** clipboard contents](https://tampermonkey.net/documentation.php). (The potential for abuse (high) outweighs the need (very low).) You must write an extension to take advantage of that `execCommand`. But I suspect this is an "X Y problem" anyway. – Brock Adams May 14 '18 at 18:27
  • @BrockAdams yes, you can check the chat feed to get the gist of this x-y issue, maybe you'd know of a way using user scripts. But OP seems to need their own crossdomain storage, that's it. – Kaiido May 14 '18 at 22:36
  • 1
    @Kaiido, Yes, it looks like it might be solvable. But **harvesting phone numbers from Craigslist** is scuzzy behavior and I will have no part in assisting such. – Brock Adams May 14 '18 at 23:22
  • @BrockAdams what I said in chat was because I really have nothing to hide. What I need can be applied to ANYTHING. I just had no reason to lie. And calling it 'harvesting' is a joke. 'Harvesting' is when you make a python script to 'scratch' it in seconds. What I'm doing is doing it one link at a time. And it is not for my own abuse or whatever. The job I'm working for was allowed by AMAZON, so that requester (job giver) is allowed to do that. I attached a screenshot in the chat of how my amazon mturk area looks like. You can see there is over 10K HITs (jobs). – sarah997 May 14 '18 at 23:28
  • ^^ @BrockAdams in 20-25 mintues all 10K+ HITs were done, while I only managed to do 65. Few days ago, 18K HITs someone 'cleaned' in less then 5 minutes. 'THAT' is 'harvesting'. And I'm doing it all manually, one CL link at a time. The reason why I said why I needed it is because I wanted to show that I'm making a script for my own purpose, not ot 'abuse' clipboard paste for some bad things (like making others install the script). I really need it for my own purpose. And such 'paste' (that I described in chat) can be used for whatever situation, I was just honest for what I need it for. – sarah997 May 14 '18 at 23:32
  • sarah997, nothing you have said negates the evidence that you -- and (unwittingly?) Amazon are helping that "James Martin" jerk violate Craigslist's TOS and collect contact information for nefarious purposes. Maybe you didn't realize you were a cog in an evil machine, but now you should know. ... Anyway, this is a true "X Y" problem and the clipboard would not be the way to solve it. – Brock Adams May 14 '18 at 23:35
  • @BrockAdams Amazon has an option to report abuse if you feel that some job-giver is breaking the ToS and no one yet, reported him to Amazon among so many mturk workers. ...OR maybe someone did, but as you see he is still posting HIT jobs. He is not 'new' job-giver. He is doing that for quite some time, so most probably Amazon decided he is 'clear to do that'. – sarah997 May 14 '18 at 23:43
  • @BrockAdams I'm a newbie, so I don't know how to do it. Could you initiate a chat session with me, please? – sarah997 May 14 '18 at 23:44
  • FFR: Chat: https://chat.stackoverflow.com/transcript/171036 – Brock Adams May 15 '18 at 00:52

1 Answers1

0

Given that your statement "only pasting between <textarea> is possible" is true:

  1. Adabt your script to insert a <textarea style="display: none" id="temp"></textarea>
  2. Paste your clipboard in there with your working code
  3. Now you can grab the data with js .value (or whatever for textareas)
  4. Cipy the value into the input

Sounds easy?

Zim84
  • 3,404
  • 2
  • 35
  • 40
  • I like your idea and I think I saw simmilar implementation somewhere here for something else, but unfortunatelly it seams it will not work for my case. I've tried to implement it but it does nothing. I mean ' – sarah997 May 15 '18 at 00:11