ThatPurpleGuy
ThatPurpleGuy

Reputation: 456

How to copy without user click

Im using tampermonkey to make a script in js to copy and password and open the site for that password so i can easily paste it in. However, I am having some trouble with the copying part. from what i see there is a need user interaction to copy something and i understand this is for security purposes and people dont want random things going into their clipboard. Is there a way to turn this off in my browser (Chrome). If not i have one more idea. I use a key combo (CTRL DOWN ARROW) to activate the script to copy and open a new tab and i use that user interaction to copy the text.

This is what i have so far

(function() {
  'use strict';

  function KeyPress(e) {
    var evtobj = window.event ? event : e
    if (evtobj.keyCode == 40 && evtobj.ctrlKey) {
      var aTags = document.getElementsByTagName("td");
      var searchText = "WP-Admin Password";

      //Finding The Password
      var pass;
      var found1;
      for (var i = 0; i < aTags.length; i++) {
        if (aTags[i].textContent == searchText) {
          //found 1 is the element that has the search phrase
          found1 = aTags[i];
          //This is where im trying to copy to my clipboard the text content.
          found1.nextSibling.firstChild.textContent; //wp Admin Password
          ;

          break;
        }
      }
      //Finding a Dev url if they have one
      var pass3;
      var found3;
      var dev;
      for (var k = 0; k < aTags.length; k++) {
        if (aTags[k].textContent == "Development URL") {
          found3 = aTags[k];
          if (found3.nextSibling.firstChild.firstChild !== "") {
            found3.nextSibling.firstChild.firstChild.click();
          } else {
            dev = false;
          }
          break;
        }
      }
      //fining the live url and going to it
      var pass2;
      var found2;
      if (!dev) {
        for (var j = 0; j < aTags.length; j++) {
          if (aTags[j].textContent == "Website") {
            found2 = aTags[j];
            found2.nextSibling.firstChild.firstChild.click();; //url
            break;
          }
        }
      }
    }
  }
  document.onkeydown = KeyPress;
})();

Upvotes: 0

Views: 140

Answers (2)

T&#237;nh Ng&#244; Quang
T&#237;nh Ng&#244; Quang

Reputation: 4652

Try GM_setClipboard

Detail at: https://github.com/scriptish/scriptish/wiki/GM_setClipboard

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant    GM_setClipboard
// ==/UserScript==

GM_setClipboard ("The clipboard now contains this sentence.");

Upvotes: 2

ThatPurpleGuy
ThatPurpleGuy

Reputation: 456

Alright I figured out a solution

This solution ONLY works if you are using tamper monkey or grease monkey

you can add

// @grant    GM_setClipboard

To your script and it will allow you to use the Gm_setclipboard() func. to copy things to clipboard.

Upvotes: 0

Related Questions