From b1154b6caf3148f77678c23f6530db852e8dfadd Mon Sep 17 00:00:00 2001 From: Peter Banjo Date: Thu, 3 Nov 2016 21:30:44 +0000 Subject: [PATCH] Selection to clipboard (#124) * selection-to-text example * update icon * update icon * use clipboard icon * update with requested changes * updated strict version and wrapped clipboard copy in setTimeout to force permissions * add note about clipboardWrite permissions inside browser event. removed permissions and setTimeout --- selection-to-clipboard/README.md | 21 +++++++++++++++++ selection-to-clipboard/content-script.js | 15 ++++++++++++ selection-to-clipboard/icons/LICENSE | 2 ++ selection-to-clipboard/icons/clipboard-48.png | Bin 0 -> 540 bytes selection-to-clipboard/manifest.json | 22 ++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 selection-to-clipboard/README.md create mode 100644 selection-to-clipboard/content-script.js create mode 100644 selection-to-clipboard/icons/LICENSE create mode 100644 selection-to-clipboard/icons/clipboard-48.png create mode 100644 selection-to-clipboard/manifest.json diff --git a/selection-to-clipboard/README.md b/selection-to-clipboard/README.md new file mode 100644 index 0000000..b9f5912 --- /dev/null +++ b/selection-to-clipboard/README.md @@ -0,0 +1,21 @@ +# selection-to-clipboard + +## What it does + +This extension includes: + +* a content script, "content-script.js", that is injected into all pages + +The content script listens for text selections in the page it's attached to and copies the text to the clipboard on mouse-up. + +## What it shows + +* how to inject content scripts declaratively using manifest.json +* how to write to the [clipboard](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Interact_with_the_clipboard) + +## Note +* If the `copySelection` function was in a browser event `clipboardWrite` permissions would be required e.g. +``` +"permissions": ["clipboardWrite"] +``` +See [Interact with the clipboard](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Interact_with_the_clipboard.) diff --git a/selection-to-clipboard/content-script.js b/selection-to-clipboard/content-script.js new file mode 100644 index 0000000..8fb2598 --- /dev/null +++ b/selection-to-clipboard/content-script.js @@ -0,0 +1,15 @@ +/* +copy the selected text to clipboard +*/ +function copySelection(e) { + var selectedText = window.getSelection().toString().trim(); + + if (selectedText) { + document.execCommand("Copy"); + } +} + +/* +Add copySelection() as a listener to mouseup events. +*/ +document.addEventListener("mouseup", copySelection); \ No newline at end of file diff --git a/selection-to-clipboard/icons/LICENSE b/selection-to-clipboard/icons/LICENSE new file mode 100644 index 0000000..20e821d --- /dev/null +++ b/selection-to-clipboard/icons/LICENSE @@ -0,0 +1,2 @@ + +The "page-32.png" and "page-48.png" icons are taken from the miu iconset created by Linh Pham Thi Dieu, and are used under the terms of its license: http://linhpham.me/miu/. diff --git a/selection-to-clipboard/icons/clipboard-48.png b/selection-to-clipboard/icons/clipboard-48.png new file mode 100644 index 0000000000000000000000000000000000000000..387f55fe6d37c3c8981f0a0a3bb743ddd5df243f GIT binary patch literal 540 zcmV+%0^|LOP)Px$)=5M`RA>e5nY~IxK@5dgv9=UJt!=bWR0L~l3wxi)XIP)W*2>Dp7Z5>IQ2fD0 zRM0~FJs-@#vbcAyxw)`P0*4tUnUl=P-DcD2g#Awi*7U4>;UR%NXMe(0ROPGi)d$Y- z4ZZEELWQyLUG%|neZu4On*V^Iy)D}g+Yez zTG;3b$Pb1OZgP-|oGylXB+W%XYfn;eC1g9%CdaV;^a*AEX}+Wtj?4#ljv*(x8{3W; zAUzhAE7&gN%gIaYWd+ZYd`F3^TM<{=g+dgxSh%q%|80t`dceJ6whCNxiKn;2g)4*I ze3vhS-K}{Z#r#oXuk#}w5y^Xrs0IIy_w$tDFY1=zu{q*DKKhS2+!2(E)>Ku2-T1u5ud4q5}reT(3k2T;(*7MF$L` zxn7A5xXNiDiw+n>bG=fh1N^-{=9;x089B+_*z$m5p)pf^f?0(!BykyVbQ2k4pLwBe zV=i_e@BngG8pDf{L3k)krNBF_#mPyE{q4dhJt(>}za;#;jGdFpH0G-hIs4 e`1L_hp};rZ`iLK-xP9pW0000"], + "js": ["content-script.js"] + }] +} \ No newline at end of file