0

I am trying to get my script to simulate pressing "CTRL+1" at the end so that the Chrome Browser jumps back to the first tab open - Once the main script is finished.

I have been using the robot class? not sure if this is the best thing to be using or not!

This is what I have, but not sure of correct or where to insert it?

<script>
    function trimAndCopyHandler(event) {

        // The barcode input text box
        const barcodeBox = event.target
            // The barcode input string
        const barcodeText = barcodeBox.value

        // if the string is shorter than 22 chars then exit
        if (barcodeText.length < 22) return

        const start = 6 // start index in string
        const end = 20 // end index in string

        // Select the text field
        barcodeBox.select()
        barcodeBox.setSelectionRange(start, end)

        // assign extracted section of barcode to trimmedString
        const trimmedString = barcodeText.slice(start, end)

        // Copy the trimmedString
        navigator.clipboard.writeText(trimmedString)

        // using template strings `${my variable here}`
        //alert(`Copied ${trimmedString} to clipboard`)

        alertTimeout(`Shipment No. ${trimmedString} Copied!`,2000)





        // clear the text box
        barcodeBox.value = ''



    }

    // find and assign the trim button element to 'trimAndCopyButton'.
    const barcode = document.querySelector('#barcode')

    // add a listener to the barcode element, which fires
    // onchange and executes the trimAndCopyHandler
    barcode.addEventListener('change', trimAndCopyHandler)


    document.getElementById('barcode').focus();











    function alertTimeout(mymsg,mymsecs)
{
 var myelement = document.createElement("div");
myelement.setAttribute("style","background-color: yellow color:red; width: 800px;height: 200px;position: absolute;top:0;bottom:0;left:0;right:0;margin:auto;border: 0px solid black;font-family:arial;font-size:30px;font-weight:bold;display: flex; align-items: center; justify-content: center; text-align: center;");
 myelement.innerHTML = mymsg;
 setTimeout(function(){
  myelement.parentNode.removeChild(myelement);
 },mymsecs);
 document.body.appendChild(myelement);
}







  
 


</script>








<script>
//Wait for 1 sec
Thread.sleep(1000);



Robot robot = new Robot();
        
        //Press CTRL key 
        robot.keyPress(KeyEvent.VK_CONTROL);
        //Press 1 , it gets typed as CTRL key is pressed
        robot.keyPress(KeyEvent.VK_1);
        
        robot.keyPress(KeyEvent.VK_1);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        
         

        

</script>

  • `Robot robot = new Robot();` this doesn't look like JavaScript. Where comes that `Robot()` class from? – Cid Sep 08 '22 at 10:57
  • Good point! This was an example I found online. Is there a better way to do this? – user14619981 Sep 08 '22 at 11:00
  • 1
    `Thread.sleep(1000);` is not javascript neither. What's your source? – Cid Sep 08 '22 at 11:03
  • I don't have one. I have just been trying random code snippets to try and bolt something together. – user14619981 Sep 08 '22 at 11:28
  • Can you point me in the right direction of what I should be looking at please? – user14619981 Sep 08 '22 at 11:28
  • I doubt you're going to be able to simulate a keypress that causes the browser to switch tabs from within a web page running in one of those tabs. Imagine if a malware site was able to do that; you go to a site, all of a sudden you're on another tab, or your browser closes all of the tabs. No, that's a security nightmare. – Heretic Monkey Sep 08 '22 at 11:35
  • 1
    You should look at Java**Script**, not Java, they are **different languages**. As far as I know, you can't use JS to handle something outside of the webpage. Imagine the huge security breach if you could send keystrokes to the system – Cid Sep 08 '22 at 11:36
  • What you are asking is not possible. – CherryDT Sep 08 '22 at 11:38
  • `I have just been trying random code snippets to try and bolt something together`...a word of advice: that is not a sensible approach to programming. It's an engineering discipline, not an art form or a guessing game. While re-using code can be a good thing and save lots of time, you need to be systematic and also ensure you have (or gain, through research) some basic understanding of what you're copying, otherwise you just end up in a mess, and/or as a [cargo cult programmer](https://en.wikipedia.org/wiki/Cargo_cult_programming) – ADyson Sep 08 '22 at 11:40
  • Ok, fair comments. I can see the security risks if this was possible – user14619981 Sep 08 '22 at 11:52
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 08 '22 at 12:19

0 Answers0