You can try the following code, it writes text to the clipboard
As an example, I wrote Sample to the clipboard
Exit

manifest.json
the manifest file is the key for all chrome extensions, it is guaranteed that it has all permissions
{ "name": "Copy to ClipBoard Demo", "description" : "This is used for demonstrating Copy to Clip Board Functionality", "version": "1", "browser_action": { "default_popup": "popup.html" }, "permissions":["clipboardWrite"], "manifest_version": 2 }
popup.html
Trivial action of a browser HTML file with input field and button
<html> <head> <script src="popup.js"></script> </head> <body> <input type="text" id="text" placeHolder="Enter Text To Copy"></input> <button id="copy">Copy</button> </body> </html>
popup.js
It copies the contents of the <input> to the clipboard
function copy() { //Get Input Element var copyDiv = document.getElementById('text'); //Give the text element focus copyDiv.focus(); //Select all content document.execCommand('SelectAll'); //Copy Content document.execCommand("Copy", false, null); } //Add Event Listeners to Button Click document.addEventListener("DOMContentLoaded", function () { document.getElementById("copy").onclick = copy; });
OR
function copy(){ //Get Input Element document.getElementById("text").select(); //Copy Content document.execCommand("Copy", false, null); } //Add Event Listeners to Button Click document.addEventListener("DOMContentLoaded", function () { document.getElementById("copy").onclick = copy; });
Sudarshan
source share