-2

I am trying to make a webpage for Copy-to-Clipboard for my Project. I got this code from Codepen. I tried to make it according to me, less understanding of JavaScript I can't make it. I am trying to get these all: -

1. get the substring in the box from URL.

2. copy the substring from the box by clicking copy button.

I am using the given below code to copy the URL.

https://oauth2.example.com/code=abcd..

This is the complete URL. But I wanted to copy only code=**abcd** in my copy to the clipboard box.

index.html

<div class="container">
    <div class="label">
    Authorization Code
    </div>
    <div class="copy-text">
        <input type="text" class="text" value="code" />
        <button><i class="fa fa-clone"></i></button>
    </div>
</div>

index.js

let copyText = document.querySelector(".copy-text");
copyText.querySelector("button").addEventListener("click", function () {
    let input = copyText.querySelector("input.text");
    input.select();
    document.execCommand("copy");
    copyText.classList.add("active");
    window.getSelection().removeAllRanges();
    setTimeout(function () {
        copyText.classList.remove("active");
    }, 2500);
});

style.css

* {
    margin: 0px;
    padding: 0px;
    box-sizing: border-box;
}
body {
    font-family: "Roboto", sans-serif;
    background: #f0f2f7;
}
.container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
.label {
    padding: 10px;
    font-size: 18px;
    color: #111;
}
.copy-text {
    position: relative;
    padding: 10px;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 10px;
    display: flex;
}
.copy-text input.text {
    padding: 10px;
    font-size: 18px;
    color: #555;
    border: none;
    outline: none;
}
.copy-text button {
    padding: 10px;
    background: #5784f5;
    color: #fff;
    font-size: 18px;
    border: none;
    outline: none;
    border-radius: 10px;
    cursor: pointer;
}

.copy-text button:active {
    background: #809ce2;
}
.copy-text button:before {
    content: "Copied";
    position: absolute;
    top: -45px;
    right: 0px;
    background: #5c81dc;
    padding: 8px 10px;
    border-radius: 20px;
    font-size: 15px;
    display: none;
}
.copy-text button:after {
    content: "";
    position: absolute;
    top: -20px;
    right: 25px;
    width: 10px;
    height: 10px;
    background: #5c81dc;
    transform: rotate(45deg);
    display: none;
}
.copy-text.active button:before,
.copy-text.active button:after {
    display: block;
}
footer {
    position: fixed;
    height: 50px;
    width: 100%;
    left: 0;
    bottom: 0;
    background-color: #5784f5;
    color: white;
    text-align: center;
}
footer p {
    margin: revert;
    padding: revert;
}
Ankit
  • 123
  • 9

1 Answers1

2

Get url query string

const queryString = window.location.search;

Get code parameter value

const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code');

Save code parameter into clipboard

navigator.clipboard.writeText(code);
Butch
  • 56
  • 4