Find out if paste ClipboardEvent is "paste as plain text"

Viewed 39

If you have a ClipboardEvent, that you know is type "paste", is there any way to determine if it was "paste as plain text"?

Through the console, when Shift is not pressed down: (don't run, just json data)

isTrusted: true
bubbles: true
cancelBubble: false
cancelable: true
clipboardData: DataTransfer
getData: (format) => {…}
dropEffect: "none"
effectAllowed: "uninitialized"
files: FileList {length: 0}
items: DataTransferItemList {length: 0}
types: []
[[Prototype]]: DataTransfer
composed: true
currentTarget: null
defaultPrevented: true
eventPhase: 0
path: (30) [span, span.syntaxBefore-3epS3C, span, div, div.markup-eYLPri.editor-H2NA06.slateTextArea-27tjG0.fontSize16Padding-XoMpjI, div, div.textArea-2CLwUE.textAreaSlate-9-y-k2.slateContainer-3x9zil, div.inner-NQg18Y.sansAttachButton-1ERHue, div.scrollableContainer-15eg7h.webkit-QgSAqd, div.channelTextArea-1FufC0.channelTextArea-1VQBuV, div, div, form.form-3gdLxP, main.chatContent-3KubbW, div.content-1jQy2l, div.chat-2ZfjoI, div.content-1SgpWY, div.base-2jDfDU, div.container-1eFtFS, div.layer-86YKbF.baseLayer-W6S8cY, div.layers-OrUESM.layers-1YQhyW, div.app-2CXKsg, div.app-3xd6d0, div.notDevTools-1zkgfK, div.appDevToolsWrapper-1QxdQf, div#app-mount.appMount-2yBXZl, body, html.full-motion.disable-forced-colors.theme-dark.platform-web.font-size-16, document, Window]
returnValue: false
srcElement: span
target: span
timeStamp: 1790263.0999999046
type: "paste"
[[Prototype]]: ClipboardEvent

when Shift is not pressed down

sTrusted: true
bubbles: true
cancelBubble: false
cancelable: true
clipboardData: DataTransfer
getData: (format) => {…}
dropEffect: "none"
effectAllowed: "uninitialized"
files: FileList {length: 0}
items: DataTransferItemList {length: 0}
types: []
[[Prototype]]: DataTransfer
composed: true
currentTarget: null
defaultPrevented: true
eventPhase: 0
path: (30) [span, span.syntaxAfter-2_BrZf, span, div, div.markup-eYLPri.editor-H2NA06.slateTextArea-27tjG0.fontSize16Padding-XoMpjI, div, div.textArea-2CLwUE.textAreaSlate-9-y-k2.slateContainer-3x9zil, div.inner-NQg18Y.sansAttachButton-1ERHue, div.scrollableContainer-15eg7h.webkit-QgSAqd, div.channelTextArea-1FufC0.channelTextArea-1VQBuV, div, div, form.form-3gdLxP, main.chatContent-3KubbW, div.content-1jQy2l, div.chat-2ZfjoI, div.content-1SgpWY, div.base-2jDfDU, div.container-1eFtFS, div.layer-86YKbF.baseLayer-W6S8cY, div.layers-OrUESM.layers-1YQhyW, div.app-2CXKsg, div.app-3xd6d0, div.notDevTools-1zkgfK, div.appDevToolsWrapper-1QxdQf, div#app-mount.appMount-2yBXZl, body, html.full-motion.disable-forced-colors.theme-dark.platform-web.font-size-16, document, Window]
returnValue: false
srcElement: span
target: span
timeStamp: 1791999.399999857
type: "paste"
[[Prototype]]: ClipboardEvent

(the only difference is the path and timeStamp properties)

The listener is created like this:

const pasteListener = (event) => {
  // use event here
};
document.body.addEventListener("paste", pasteListener, true);

The only option I've thought of is to create global event listeners for keydown & keyup to listen for the Shift key, but that's open to potential issues like when a user loses focus on the page and presses down/releases Shift (and doesn't detect when you right-click and "paste as plain text").

The use case of this is to change the contents of what a web app thinks the clipboard content is, but don't change it when a user intentionally does a "paste as plain text" (e.g. context menu, Ctrl+Shift+v

Related: How to override paste notification for Paste as plain text button?, but that question isn't for detection, it's for overriding.

0 Answers
Related