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
This commit is contained in:
Peter Banjo
2016-11-03 21:30:44 +00:00
committed by wbamberg
parent a3782445bf
commit b1154b6caf
5 changed files with 60 additions and 0 deletions

View File

@@ -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.)

View File

@@ -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);

View File

@@ -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/.

Binary file not shown.

After

Width:  |  Height:  |  Size: 540 B

View File

@@ -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": ["<all_urls>"],
"js": ["content-script.js"]
}]
}