Code for post https://barrysimpson.net/posts/copy-paste-chrome-ext
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="background.js"></script> | |
</head> | |
<body> | |
<textarea id="ta"></textarea> | |
</body> | |
</html> |
'use strict'; | |
function getClipboard() { | |
var result = null; | |
var textarea = document.getElementById('ta'); | |
textarea.value = ''; | |
textarea.select(); | |
if (document.execCommand('paste')) { | |
result = textarea.value; | |
} else { | |
console.error('failed to get clipboard content'); | |
} | |
textarea.value = ''; | |
return result; | |
} | |
function setClipboard(value) { | |
var result = false; | |
var textarea = document.getElementById('ta'); | |
textarea.value = value; | |
textarea.select(); | |
if (document.execCommand('copy')) { | |
result = true; | |
} else { | |
console.error('failed to get clipboard content'); | |
} | |
textarea.value = ''; | |
return result; | |
} | |
chrome.runtime.onMessageExternal.addListener(function(request, sender, sendResponse) { | |
switch (request.method) { | |
case 'getClipboard': | |
sendResponse(getClipboard()); | |
break; | |
case 'setClipboard': | |
sendResponse(setClipboard(request.value)); | |
break; | |
default: | |
console.error('Unknown method "%s"', request.method); | |
break; | |
} | |
}); |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title></title> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width"> | |
</head> | |
<body> | |
<h4>Current Clipboard Text:</h4> | |
<p id="clipboard-content"></p> | |
<hr> | |
<button id="copyButton">Set System Clipboard To The Following:</button> | |
<p id="para">Ut scelerisque posuere sem, non aliquam ipsum sodales et. Fusce luctus, mauris ut volutpat varius, leo dui posuere ligula, vitae rhoncus leo ipsum eu neque.</p> | |
<script src="main.js"></script> | |
</body> | |
</html> |
'use strict'; | |
var extensionId = 'YOUR EXTENSION ID HERE'; | |
document.getElementById('copyButton').addEventListener('click', function() { | |
var text = document.getElementById('para').textContent; | |
chrome.runtime.sendMessage( | |
extensionId, | |
{ method: 'setClipboard', value: text }, | |
function(response) { | |
console.log('extension setClipboard response', response); | |
} | |
); | |
}); | |
document.addEventListener('DOMContentLoaded', function() { | |
readClipboard(); | |
// Re-read the clipboard every time the page becomes visible. | |
document.addEventListener('visibilitychange', function() { | |
if (!document.hidden) { | |
readClipboard(); | |
} | |
}); | |
}); | |
function readClipboard() { | |
chrome.runtime.sendMessage( | |
extensionId, | |
{ method: 'getClipboard' }, | |
function(response) { | |
document.getElementById('clipboard-content').textContent = response; | |
console.log('extension getClipboard response', response); | |
} | |
); | |
} |
{ | |
"manifest_version": 2, | |
"name": "Copy clipboard.", | |
"description": "Copy clipboard programmatically at any time.", | |
"version": "1.0", | |
"background": { | |
"persistent": false, | |
"page": "background.html" | |
}, | |
"permissions": [ | |
"clipboardRead", | |
"clipboardWrite" | |
], | |
"externally_connectable": { | |
"matches": [ | |
"http://localhost/*" | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment