Skip to content

Instantly share code, notes, and snippets.

@Vaisakhkm2625
Created April 15, 2024 20:31
Show Gist options
  • Save Vaisakhkm2625/cb27c47005c417e9a7b4093f947a257e to your computer and use it in GitHub Desktop.
Save Vaisakhkm2625/cb27c47005c417e9a7b4093f947a257e to your computer and use it in GitHub Desktop.

make qr code for selected text

let selection = window.getSelection().toString();
let encodedSelection = encodeURIComponent(selection)

let popupContent = `<html lang="en">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"> </script>
</head>
<body>
    <main>
        <h1> qr code for current selection</h1>
        <div id="qrcode"></div>

        <div id="whatsappQrcodeContainer" style="display: none">
        <h1> Detected phone number, so here is whatsapp link - </h1>
        <div id="whatsappQrcode"></div>
        </div>

    </main>

    <script>

        let selection = decodeURIComponent("${encodedSelection}");

        var qrcode = new QRCode("qrcode",selection);

        let phPattern=/^[+]?[0-9]{8,13}$/;
        let possiblePhNumber = selection.replace(/[^+0-9]/g, '');
        if(phPattern.test(possiblePhNumber)){
            document.getElementById("whatsappQrcodeContainer").style.display = "block";
            let qrcode = new QRCode('whatsappQrcode','https://wa.me/'+possiblePhNumber);

        }

    </script>
</body>

</html>`;

myWindow = window.open("",'header', 'menubar=0', 'toolbar=0');
myWindow.document.write(popupContent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment