Skip to content

Commit

Permalink
Add browser support (#61)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Richienb and sindresorhus authored Mar 19, 2020
1 parent d68694f commit e4a82ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
16 changes: 16 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-env browser */
'use strict';

export const write = async text => {
await navigator.clipboard.writeText(text);
};

export const read = async () => navigator.clipboard.readText();

export const readSync = () => {
throw new Error('`.readSync()` is not supported in browsers!');
};

export const writeSync = () => {
throw new Error('`.writeSync()` is not supported in browsers!');
};
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export function write(text: string): Promise<void>;
/**
Write (copy) to the clipboard synchronously.
Doesn't work in browsers.
@param text - The text to write to the clipboard.
@example
Expand All @@ -29,5 +31,7 @@ export function read(): Promise<string>;

/**
Read (paste) from the clipboard synchronously.
Doesn't work in browsers.
*/
export function readSync(): string;
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"files": [
"index.js",
"index.d.ts",
"browser.js",
"lib",
"fallbacks"
],
Expand All @@ -43,5 +44,10 @@
"ava": "^2.1.0",
"tsd": "^0.10.0",
"xo": "^0.25.3"
},
"browser": "browser.js",
"exports": {
"browser": "./browser.js",
"default": "./index.js"
}
}
9 changes: 4 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

> Access the system clipboard (copy/paste)
Cross-platform. Supports: macOS, Windows, Linux, OpenBSD, FreeBSD, Android with [Termux](https://termux.com/).

Cross-platform. Supports: macOS, Windows, Linux, OpenBSD, FreeBSD, Android with [Termux](https://termux.com/), [modern browsers](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API#Browser_compatibility).

## Install

```
$ npm install clipboardy
```


## Usage

```js
Expand All @@ -23,7 +21,6 @@ clipboardy.readSync();
//=> '🦄'
```


## API

### clipboardy
Expand All @@ -46,6 +43,8 @@ Read (paste) from the clipboard asynchronously. Returns a `Promise`.

Write (copy) to the clipboard synchronously.

Doesn't work in browsers.

##### text

Type: `string`
Expand All @@ -56,14 +55,14 @@ The text to write to the clipboard.

Read (paste) from the clipboard synchronously.

Doesn't work in browsers.

## FAQ

#### Where can I find the source of the bundled binaries?

The [Linux binary](https://github.com/sindresorhus/clipboardy/blob/master/fallbacks/linux/xsel) is just a bundled version of [`xsel`](https://linux.die.net/man/1/xsel). The source for the [Windows binary](https://github.com/sindresorhus/clipboardy/blob/master/fallbacks/windows/clipboard_x86_64.exe) can be found [here](https://github.com/sindresorhus/win-clipboard).


## Related

- [clipboard-cli](https://github.com/sindresorhus/clipboard-cli) - CLI for this module
Expand Down

0 comments on commit e4a82ba

Please sign in to comment.