Skip to content

Clipboard doesn't work in a webview #8170

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

Closed
Sunvas opened this issue Mar 6, 2024 · 9 comments
Closed

Clipboard doesn't work in a webview #8170

Sunvas opened this issue Mar 6, 2024 · 9 comments

Comments

@Sunvas
Copy link

Sunvas commented Mar 6, 2024

Current/Missing Behavior

navigator.clipboard.writeText doesn't work in webviews. See how to reproduce section for demo.

Expected/Proposed Behavior

It should work

Additional Info

  • Operating System: any
  • NW.js Version: 0.85
  • Code snippet: included

How to reproduce

package.json

{
	"main": "/main.html",
	"name": "demo"
}

main.html

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Demo</title>
</head>
<body style="width:100vw;height:100vh">
	<h2>Clipboard doesn't work in webviews:</h2>
	<webview style="width:100%;height:100%" src="https://www.w3schools.com/howto/howto_js_copy_clipboard.asp"></webview>
</body>
</html>

Demo opens page with live demostraion of "Copy Text to Clipboard" which perfectly works in browser, but doesn't work in webviews.

@rogerwang
Copy link
Member

close with the same fix in #7855 (comment)

@Sunvas
Copy link
Author

Sunvas commented Mar 29, 2025

@rogerwang, it did not help: I've rewritten package.json into this:

{
	"main": "/main.html",
	"name": "demo",
	"node-main": "--no-experimental-global-navigator"
}

And clipboard still doesn't work.

@rogerwang
Copy link
Member

Here is the code I'm using:

{
  "main": "index.html",
  "name": "bug",
  "node-main": "--no-experimental-global-navigator",
  "webview": {
    "partitions": [
      {
          "name": "trusted*",
          "accessible_resources": ["*"]
      }
    ]
  }

}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <style type="text/css">
            html, body, webview {
                display: block;
                margin: 0; padding: 0; border: 0;
                width: 100%; height: 100%;
            }

            html, body {
                overflow: hidden;
            }

            webview {
                position: absolute;
                top: 0; left: 0;
            }
        </style>
    </head>
    <body>
        <webview src="webview.html" partition="persist:trusted" allownw></webview>
    </body>
</html>
<!DOCTYPE html>
<html>
<body>

<p>Click on the button to copy the text from the text field. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect.</p>

<input type="text" value="Hello World" id="myInput">
<button onclick="myFunction()">Copy text</button>

<script>
function myFunction() {
  // Get the text field
  var copyText = document.getElementById("myInput");

  // Select the text field
  copyText.select();
  copyText.setSelectionRange(0, 99999); // For mobile devices

  // Copy the text inside the text field
  navigator.clipboard.writeText(copyText.value);

  // Alert the copied text
  alert("Copied the text: " + copyText.value);
}
</script>

</body>
</html>

@Sunvas
Copy link
Author

Sunvas commented Mar 29, 2025

@rogerwang , your code works with local resources but it doesn't work with remote resources... Why? 😟

@Sunvas
Copy link
Author

Sunvas commented Mar 29, 2025

@rogerwang I've also tested code from issue #7855. Everything works with local resources, but nothing with remote.

@rogerwang
Copy link
Member

rogerwang commented Mar 29, 2025

try this with remote urls, which is under different permissions than local extension resources

{
  "main": "index.html",
  "name": "bug",
  "node-main": "--no-experimental-global-navigator",
  "content_capabilities": {
           "matches": [
             "https://*.w3schools.com/"
           ],
           "permissions": [
             "clipboardRead",
             "clipboardWrite"
           ]
  },
  "webview": {
    "partitions": [
      {
          "name": "trusted*",
          "accessible_resources": ["*"]
      }
    ]
  }

}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <style type="text/css">
            html, body, webview {
                display: block;
                margin: 0; padding: 0; border: 0;
                width: 100%; height: 100%;
            }

            html, body {
                overflow: hidden;
            }

            webview {
                position: absolute;
                top: 0; left: 0;
            }
        </style>
    </head>
    <body>
        <webview src="https://www.w3schools.com/howto/howto_js_copy_clipboard.asp" partition="persist:trusted"></webview>
    </body>
</html>

@Sunvas
Copy link
Author

Sunvas commented Mar 29, 2025

@rogerwang worked with:

"matches": [
            "https://*.w3schools.com/*"
],

Could you provide more information about param content_capabilities? Official documentation says nothing about it. Does it have any params related to #7855 ?

@rogerwang
Copy link
Member

It's from the spec of Chrome extension. A NW app is an extension under the hood.

@Sunvas
Copy link
Author

Sunvas commented Mar 29, 2025

@rogerwang that's strange because even here I can't find anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants