clippie
Advanced tools
Comparing version 4.0.1 to 4.0.2
33
index.js
export async function clippie(content, {reject = false} = {}) { | ||
try { | ||
if (!navigator?.clipboard?.write) { // navigator.clipboard is undefined on insecure origin | ||
return fallback(content); | ||
} | ||
if (Array.isArray(content)) { | ||
@@ -16,15 +19,3 @@ await navigator.clipboard.write([ | ||
} catch { | ||
if (!document.execCommand) return false; | ||
const el = document.createElement("textarea"); | ||
el.value = String(content); | ||
el.style.clipPath = "inset(50%)"; | ||
el.ariaHidden = "true"; | ||
document.body.append(el); | ||
try { | ||
el.select(); | ||
const success = document.execCommand("copy"); | ||
if (success) return true; | ||
} finally { | ||
el.remove(); | ||
} | ||
fallback(content); | ||
} | ||
@@ -38,1 +29,17 @@ return false; | ||
} | ||
function fallback(content) { | ||
if (!document.execCommand) return false; | ||
const el = document.createElement("textarea"); | ||
el.value = String(content); | ||
el.style.clipPath = "inset(50%)"; | ||
el.ariaHidden = "true"; | ||
document.body.append(el); | ||
try { | ||
el.select(); | ||
const success = document.execCommand("copy"); | ||
if (success) return true; | ||
} finally { | ||
el.remove(); | ||
} | ||
} |
{ | ||
"name": "clippie", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"description": "Teeny-tiny browser clipboard copy", | ||
@@ -16,9 +16,10 @@ "author": "silverwind", | ||
"blob-polyfill": "7.0.20220408", | ||
"eslint": "8.40.0", | ||
"eslint-config-silverwind": "73.0.0", | ||
"jsdom": "22.0.0", | ||
"updates": "14.1.0", | ||
"versions": "11.0.0", | ||
"vitest": "0.31.1" | ||
"eslint": "8.44.0", | ||
"eslint-config-silverwind": "73.1.2", | ||
"jsdom": "22.1.0", | ||
"updates": "14.3.2", | ||
"versions": "11.0.1", | ||
"vitest": "0.33.0", | ||
"vitest-config-silverwind": "2.1.0" | ||
} | ||
} |
# clippie | ||
[![](https://img.shields.io/npm/v/clippie.svg?style=flat)](https://www.npmjs.org/package/clippie) [![](https://img.shields.io/npm/dm/clippie.svg)](https://www.npmjs.org/package/clippie) [![](https://img.shields.io/bundlephobia/minzip/clippie.svg)](https://bundlephobia.com/package/clippie) | ||
[![](https://img.shields.io/npm/v/clippie.svg?style=flat)](https://www.npmjs.org/package/clippie) [![](https://img.shields.io/npm/dm/clippie.svg)](https://www.npmjs.org/package/clippie) [![](https://img.shields.io/bundlephobia/minzip/clippie.svg)](https://bundlephobia.com/package/clippie) [![](https://packagephobia.com/badge?p=clippie)](https://packagephobia.com/result?p=clippie) | ||
@@ -17,3 +17,3 @@ `clippie` copies text and images to clipboard in browsers. Text copy works on both secure and insecure contexts (via fallback), image copy requires a secure context (https or localhost). | ||
// copy image and text | ||
// copy image and text at once | ||
const success = await clippie([imageBlob, "image description"]); | ||
@@ -25,3 +25,3 @@ ``` | ||
- `content` *String*, *Blob*, *Array[String,Blob]*: Content to copy. If an Array is passed, will construct a `ClipboardItem` with all items. Only one item per `type` is allowed. | ||
- `content` *String*, *Blob*, *Array[String,Blob]*: Content to copy. If an Array is passed, will construct a `ClipboardItem` with all items. Only one item per `type` (`blob.type` or `text/plain` for strings) is allowed. | ||
- `options` *Object* | ||
@@ -28,0 +28,0 @@ - `reject`: *Boolean*: Whether to reject on unexpected errors. Default: `false`. |
3425
42
8