Bookmarklet that writes to clipboard works once per window

Description of the issue:
Bookmarklets that write to the clipboard using the JavaScript call ‘navigator.clipboard.writeText’ have stopped working properly in the latest release. The first bookmarklet clicked per window (not per tab) works as expected and then no other bookmarklet doing this will work in any tab in the window until the clipboard is loaded from a different source (e.g. copying text from a web page).

How can this issue be reproduced?

  1. Create a bookmarklet with the URL: javascript:navigator.clipboard.writeText(“first text to paste”);
  2. Create a second bookmarklet with the URL: javascript:navigator.clipboard.writeText(“second text to paste”);
  3. Open a simple web page
  4. Click the first bookmarklet
  5. Observe that the clipboard contains the text “first text to paste”
  6. Click the second bookmarklet
  7. Observe that the clipboard still contains the text “first text to paste”

Expected result:
After step 6 above, the contents of the clipboard should be “second text to paste”

Brave Version( check About Brave):
Brave 1.96.59 (Official Build) (64-bit)
Chromium: 154.0.8037.58

Additional Information:
Chrome Version 154.0.8037.93 (Official Build) (64-bit) does not have this issue so it appears to specific to the latest version of Brave.

@MeritoriousMarmot

I use Brave Browser (MacOS) v1.96.59

For the following bookmarklets of yours:

javascript:navigator.clipboard.writeText('first text to paste');

javascript:navigator.clipboard.writeText('second text to paste');

Whichever I use, I get what they intend:

javascript:navigator.clipboard.writeText('first text to paste');

… always produces in the Mac’s Clipboard, character string: first text to paste

javascript:navigator.clipboard.writeText('second text to paste');

… always produces in the Mac’s Clipboard, character string: second text to paste

NOTE: The single quotes in the JavaScripts are valid JavaScript string delimiters.

Oops. I made the classic mistake of assuming that the code was correct because it had been doing what I wanted – for years in this case – in both Chrome and Firefox. Thank you for catching the single-quote vs double-quote error. I know several other languages and in all of them strings are defined with double-quotes. I’m just learning JavaScript and obviously was mislead by old habits.

Sorry, but both single and double quotes are valid string delimiters in JavaScript, so this is not the problem. I checked the ECMA standard to verify this.

How can this issue be reproduced?
The problem has changed behaviour with the latest release of Brave:
Brave 1.97.56 (Official Build) (64-bit)
Chromium: 155.0.8059.40

Unfortunately, there is no specific procedure that causes it and its behaviour appears to vary depending on the web page contents. The best I can figure out is something like this:

  1. On this page (the one you’re reading now) https://community.brave.app/t/bookmarklet-that-writes-to-clipboard-works-once-per-window/658986/2, highlight and copy text from the web page (always works)
  2. Click to run the first bookmarklet. This may or may not work
  3. Click to run the second bookmarklet. This may or may not work, but is much less likely to work
  4. Repeat 1, 2 & 3 as necessary. At some point, the clipboard contents will be incorrect

Expected result:
After each click of one of the bookmarklets, the clipboard should contain the data defined in the bookmarklet.

Sorry I can’t be more specific. I suspect a buffer overrun or uninitialized variable or ???

Trick: I view the clipboard contents by left-clicking in the address field and then right-clicking it. The clipboard contents appear in the “Paste and search for…” selection.