Skip to content

Commit 144f99f

Browse files
authored
Handle Clipboard being undefined in insecure contexts (#15)
* Handle Clipboard being undefined in insecure contexts * Add changelog entry for Maybeness of window.clipboard * Add explicit export list * Use double quotes for string literal
1 parent 9f90147 commit 144f99f

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Notable changes to this project are documented in this file. The format is based
66

77
Breaking changes:
88

9+
- `clipboard` now returns `Effect (Maybe Clipboard)` instead of `Effect Clipboard`.
10+
This is because insecure contexts don't have the clipboard available, see
11+
[MDN](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard).
12+
913
New features:
1014

1115
Bugfixes:

src/Web/Clipboard/Clipboard.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
export function clipboard(navigator) {
1+
export function clipboardWrapper(just, nothing, navigator) {
22
return function () {
3-
return navigator.clipboard;
3+
var clp = navigator.clipboard;
4+
if (typeof clp === "undefined") {
5+
// This is expected if we don't have a SecureContext (see w3 spec).
6+
return nothing;
7+
} else {
8+
return just(clp);
9+
}
410
};
511
}
612

src/Web/Clipboard/Clipboard.purs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
module Web.Clipboard where
1+
module Web.Clipboard
2+
( clipboard
3+
, Clipboard
4+
, toEventTarget
5+
, fromEventTarget
6+
, readText
7+
, writeText
8+
) where
29

310
import Prelude
411

5-
import Data.Maybe (Maybe)
12+
import Data.Function.Uncurried (Fn3, runFn3)
13+
import Data.Maybe (Maybe(..))
614
import Effect (Effect)
715
import Promise (Promise)
816
import Unsafe.Coerce (unsafeCoerce)
917
import Web.Event.Internal.Types (EventTarget)
1018
import Web.HTML (Navigator)
1119
import Web.Internal.FFI (unsafeReadProtoTagged)
1220

13-
foreign import clipboard :: Navigator -> Effect Clipboard
21+
foreign import clipboardWrapper :: Fn3 (forall a. a -> Maybe a) (forall a. Maybe a) Navigator (Effect (Maybe Clipboard))
22+
23+
clipboard :: Navigator -> Effect (Maybe Clipboard)
24+
clipboard = runFn3 clipboardWrapper Just Nothing
1425

1526
foreign import data Clipboard :: Type
1627

0 commit comments

Comments
 (0)