Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Clipboard being undefined in insecure contexts #15

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Notable changes to this project are documented in this file. The format is based

Breaking changes:

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

New features:

Bugfixes:
Expand Down
10 changes: 8 additions & 2 deletions src/Web/Clipboard/Clipboard.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export function clipboard(navigator) {
export function clipboardWrapper(just, nothing, navigator) {
return function () {
return navigator.clipboard;
var clp = navigator.clipboard;
if (typeof clp === "undefined") {
// This is expected if we don't have a SecureContext (see w3 spec).
return nothing;
} else {
return just(clp);
}
};
}

Expand Down
17 changes: 14 additions & 3 deletions src/Web/Clipboard/Clipboard.purs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
module Web.Clipboard where
module Web.Clipboard
( clipboard
, Clipboard
, toEventTarget
, fromEventTarget
, readText
, writeText
) where

import Prelude

import Data.Maybe (Maybe)
import Data.Function.Uncurried (Fn3, runFn3)
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Promise (Promise)
import Unsafe.Coerce (unsafeCoerce)
import Web.Event.Internal.Types (EventTarget)
import Web.HTML (Navigator)
import Web.Internal.FFI (unsafeReadProtoTagged)

foreign import clipboard :: Navigator -> Effect Clipboard
foreign import clipboardWrapper :: Fn3 (forall a. a -> Maybe a) (forall a. Maybe a) Navigator (Effect (Maybe Clipboard))

clipboard :: Navigator -> Effect (Maybe Clipboard)
clipboard = runFn3 clipboardWrapper Just Nothing

foreign import data Clipboard :: Type

Expand Down
Loading