Skip to content

Commit

Permalink
Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
rohinivsenthil committed Jul 1, 2020
1 parent b3a32b0 commit 5690112
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
35 changes: 20 additions & 15 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
const codeBlocks = Array.from(document.getElementsByTagName('pre'));

const enabledButton = "height:.5rem; width:4rem; font-size:.7rem; border:none; margin-bottom:.5rem";
const disabledButton = "height:.5rem; width:4rem; font-size:.7rem; border:none; margin-bottom:.5rem; color:gray";

codeBlocks.forEach(codeBlock => {
const copyButton = document.createElement("button")
const wrapper = document.createElement("div")
const copyButton = document.createElement("button");
const wrapper = document.createElement("div");

copyButton.innerHTML = "Copy";
copyButton.style = enabledButton;
wrapper.appendChild(copyButton);
wrapper.style = "width:100%; display:flex; justify-content:flex-end";
codeBlock.parentElement.insertBefore(wrapper, codeBlock);

copyButton.innerHTML = "Copy"
copyButton.style = "height:.5rem; width:4rem; font-size:.7rem; border:none; margin-bottom:.5rem"
wrapper.appendChild(copyButton)
wrapper.style = "width:100%; display:flex; justify-content:flex-end"
codeBlock.parentElement.insertBefore(wrapper, codeBlock)
copyButton.onclick = async (event) => {
await navigator.clipboard.writeText(codeBlock.innerText)
copyButton.innerHTML = "Copied!"
copyButton.style = disabledButton;
copyButton.disabled = true;

copyButton.onclick = (event) => {
const range = document.createRange();
range.selectNode(codeBlock);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand("copy");
window.getSelection().removeAllRanges()
setTimeout(() => {
copyButton.innerHTML = "Copy";
copyButton.style = enabledButton;
copyButton.disabled = false;
}, 2000);
}

})
5 changes: 4 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "StackCopy",
"version": "1.0",
"version": "1.1",
"description": "Copy code snippets from any StackExchange site with ease.",
"icons": {
"48": "icons/icon.png",
Expand All @@ -23,5 +23,8 @@
"main.js"
]
}
],
"permissions": [
"clipboard-write"
]
}

0 comments on commit 5690112

Please sign in to comment.