mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
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:
21
selection-to-clipboard/README.md
Normal file
21
selection-to-clipboard/README.md
Normal 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.)
|
||||
15
selection-to-clipboard/content-script.js
Normal file
15
selection-to-clipboard/content-script.js
Normal 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);
|
||||
2
selection-to-clipboard/icons/LICENSE
Normal file
2
selection-to-clipboard/icons/LICENSE
Normal 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/.
|
||||
BIN
selection-to-clipboard/icons/clipboard-48.png
Normal file
BIN
selection-to-clipboard/icons/clipboard-48.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 540 B |
22
selection-to-clipboard/manifest.json
Normal file
22
selection-to-clipboard/manifest.json
Normal 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"]
|
||||
}]
|
||||
}
|
||||
Reference in New Issue
Block a user