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 0000000..387f55f Binary files /dev/null and b/selection-to-clipboard/icons/clipboard-48.png differ diff --git a/selection-to-clipboard/manifest.json b/selection-to-clipboard/manifest.json new file mode 100644 index 0000000..b6a4822 --- /dev/null +++ b/selection-to-clipboard/manifest.json @@ -0,0 +1,22 @@ +{ + "manifest_version": 2, + "name": "selection-to-clipboard", + "description": "Example of WebExtensionAPI for writing to the clipboard", + "version": "1.0", + "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/selection-to-clipboard", + + "icons": { + "48": "icons/clipboard-48.png" + }, + + "applications": { + "gecko": { + "strict_min_version": "51.0a1" + } + }, + + "content_scripts": [{ + "matches": [""], + "js": ["content-script.js"] + }] +} \ No newline at end of file