Text changes when using clipboard.writeText

Description of the issue: The navigator.clipboard.writeText(text0) function, if text0 contains a URL, a change is made to text0 when the paste action is performed. example:
https://www.abc.com/<br>abc
changes to
https://www.abc.com/%3Cbr%3Eabc
This change happens when I update to the current version of brave, it does not happen in other browsers.
An easy way to check this is by using the w3school website test “howto_js_copy_clipboard”.
Brave Version (check About Brave): 1.69.160
Operating System: Windows 10

3 Likes

I think I’m also facing the same underlying issue, for me newline characters \n are getting removed when I’m trying to copy a list of URLs, which is quite annoying… This didn’t happen in older versions of Brave.

Example of non-URLs working as expected:
Screenshot 2024-09-02 200726

Example of URLs not working as expected:

Code used in examples:

// For trying non-URLs
document.addEventListener("click", () => {
  const nonUrls = ["hello", "world", "testing"];

  navigator.clipboard
    .writeText(nonUrls.join("\n"))
    .then(() => {
      console.log(`Copied ${nonUrls.length} non-URLs`);
    })
    .catch((err) => {
      console.warn("Failed to copy non-URLs!", err);
    });
  
  navigator.clipboard
    .readText().then((copiedText) => {
      console.log(copiedText);
    });
});

// For trying URLs
document.addEventListener("click", () => {
  const urls = ["https://www.example.com/page1", "https://www.example.com/page2", "https://www.example.com/page3"];

  navigator.clipboard
    .writeText(urls.join("\n"))
    .then(() => {
      console.log(`Copied ${urls.length} URLs`);
    })
    .catch((err) => {
      console.warn("Failed to copy URLs!", err);
    });
  
    navigator.clipboard
      .readText().then((copiedText) => {
        console.log(copiedText);
      });
});

Brave version: 1.69.160
Operating System: Windows 11 (23H2)

Had the same issue here.

my expected copied text would be

Mean: 5.5
Standard Deviation: 3.0277
Coefficient of Variation: 0.5505 (55.05%)

while it gave me this:

mean: 5.5Standard Deviation: 3.0277Coefficient of Variation: 0.5505 (55.05%)

Brave version: 1.69168
Operating system: macOS

Thank you all for reporting and apologies for the long response time here. Looks like I can reproduce this on my end as well. Taking a look at this now — appreciate your patience.

We have had a similar report here:

1 Like

Looks like our devs are on it and should have a fix for this soon. Thank you all again for reporting.

I would like to quickly mention that this issue occurs in extensions as well, not just on websites. Just to make sure the fix is applied to extensions as well.

I originally encountered this issue in an extension I maintain. Which runs as an extension popup and not as injected JS.

Extension manifest if relevant (name, description, version fields have been changed):

{
  "manifest_version": 3,
  "name": "Extension title",
  "description": "Extension description",
  "version": "0.0.0",
  "permissions": ["tabs", "storage"],
  "action": {
    "default_title": "Extension title",
    "default_popup": "extension_menu.html",
    "browser_style": true,
    "icons": {
      "16": "/images/icon_16.png",
      "32": "/images/icon_32.png",
      "48": "/images/icon_48.png",
      "128": "/images/icon_128.png"
    }
  },
  "icons": {
    "16": "/images/icon_16.png",
    "32": "/images/icon_32.png",
    "48": "/images/icon_48.png",
    "128": "/images/icon_128.png"
  }
}

The extension then uses navigator.clipboard.writeText, which currently is broken.

I added a temporary patch to the extension to allow for correct copying.

The temporary patch:

navigator.clipboard.writeText(`# Copied data (${getCurrentDateTime()})\n` + data.join("\n"))

It simply adds # Copied data ... as the first line of the copied text, this seems to stop Brave from thinking it’s a URL that needs to be modified.

+1
This is also impacting my application

I tried putting # at the beginning of the text and it certainly doesn’t convert.
I had already tried another temporary code.

normal
navigator.clipboard.writeText(Text00)

new
navigator.clipboard.write([new ClipboardItem({‘text/plain’:new Blob([Text00],{type:‘text/plain’})})])

Copy to clipboard call eats non-printable characters. My code creates a string with \n or \r\n characters to break text up into three lines for the user.
navigator.clipboard.writeText(xxx) strips out all the non-printable characters, leaving the input text as a single line with no breaks between the original elements. This does not occur in Google Chrome or MS Edge, where the text in the buffer retains the supplied formatting.