"Failed to copy to clipboard!" from tiddler opened in new window

Hello,

In a single-file-based TiddlyWiki in Chrome, I was using the macro:

\define clipVariable()
<$wikify name=wikified-input text=<<variable_set_from_surrounding_list_widget>> output="text">
<$button message="tm-copy-to-clipboard" param=<<wikified-input>> > <<variable_set_from_surrounding_list_widget>> {{$:/core/images/copy-clipboard}}</$button>
</$wikify>
\end

This seems to work well in the main Story River. However, when I open the containing tiddler as a new window, I get the message “Failed to copy to clipboard!”.

I’ve tried to wrap the whole tiddler in a tiddler widget to give it the right context (something that helps in the sidebar) and removing the wikify widget to see if that was interfering … but no luck.

Anyone have an idea on what is failing here? Is this browser cross-window security intervening?

Thanks,
/Mike

Can you try building up from one that does work rather than reducing from a broken one?

I was able to successfully copy to clipboard from this simplified variant, both from the StoryRiver and from an external window:

Clipboard Test.json (406 Bytes) (We need to save after importing it.)

I launched the window by right clicking on the link in the Recent tab and choosing “Open in new window”. Are you launching it differently? The difference could certainly be there.

The tm-copy-to-clipboard message is handled in $:/core/modules/utils/dom.js by the exports.copyToClipboard function which uses document.execCommand("copy") to actually perform the clipboard copy action.

According to https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard:

When opening a tiddler as a new window (via the more > open in new window command in the tiddler’s toolbar), the window’s “context” is “about:blank”, which is NOT considered to be a secure context. Thus, the exports.copyToClipboard code invoked from $:/core/modules/utils/dom.js fails, and throws an error, which is reported using text from $:/language/Notifications/CopiedToClipboard/Failed.

Note that, in contrast, when a tiddler is opened in a new window by right-clicking on the tiddler title and choosing “Open in new window” (as @Scott_Sauyet has done), the resulting window has the same “context” as the originating window. Thus, if the originating window uses https:// or file://, then so will the new window… and, since those are considered to be secure contexts, the tm-copy-to-clipboard function will succeed in the new window without throwing an error, which is reported using text from $:/language/Notifications/CopiedToClipboard/Succeeded.

TL/DR: yes, this is “browser cross-window security intervening”

-e

1 Like

Thank you for the great explanation.

Never before have I used that link. I don’t think I’ve even //noticed// it!

Always more to learn.

Thank you @EricShulman and @Scott_Sauyet .

I was actually opening the new window neither way.

The background reason I was opening a new window to view the tiddler was that I had a wide table that would spill over into the sidebar if opened in the Story River so I wanted it to have it’s own resizeable and scrollable window to make it easy to navigate and click a cell to copy it to clipboard. So I built a fake link (which happens to be displayed in the sidebar) via:

<$button message="tm-open-window" param="Wide Table Tiddler" class="tc-btn-invisible tc-tiddlylink" tooltip="Opens in new window to allow width">Open the Wide Table&nbsp;{{$:/core/images/open-window}}</$button>

It would seem that the tm-open-window message uses the same mechanism as more > open in new window so I was thwarted from doing something seemingly benign.

I guess I either have to figure out how to have a wide table in a tiddler not overflow into the sidebar (which might lead me to figure out how to keep the header row and column always visible while scrolling around) or see if a modal or popup window will suit my purposes.

As @Scott_Sauyet said, always more to learn.

Thanks for your help and explanation.
/Mike