Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clipboard API call throws NotAllowedError without invoking onPermissionRequest()

I have a simple page with a button, that when pressed, uses the Async Clipboard API to write to the clipboard.

<body>   <button type="button" onclick="testClipboard();">     Test Clipboard   </button> </body> 
function testClipboard() {   navigator.clipboard.writeText("Clipboard API Test").then(     v => alert("Success"),     e => alert("Fail\n" + e)); } 

This works on both Chrome and Firefox, desktop and mobile. However on Android Webview it throws the following error:

NotAllowError: Write permission denied.


I figured I need to override WebChromeClient.onPermissionRequest() to grant the permission, but strangely onPermissionRequest() does not seem to have been invoked, and the same error is still thrown.

public class WebChromeController extends WebChromeClient {   @Override   public void onPermissionRequest(PermissionRequest request) {     Log.d("myTag", "Permission request");     Log.d("myTag", request.getResources().toString());     request.grant(request.getResources());   } } protected void initWebView() {   // ...   myWebView.setWebChromeClient(new WebChromeController()); } 

I still get the same error:

NotAllowError: Write permission denied.

Also Logcat logged nothing.


I suspected maybe my Android App requires additional permissions to access the clipboard, but according to https://developer.android.com/about/versions/10/privacy/changes#clipboard-data, my App should have permission when it has focus. Indeed, the following code works:

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("MyLbl", "I have permission"); clipboard.setPrimaryClip(clip); 

I also declared the following in AndroidManifest.xml in case the action of requesting permission requires permission:

<uses-permission android:name="android.webkit.PermissionRequest" /> 

This did nothing.

So it is probably not an issue with App level permission.


What is happening?

How can I get Async Clipboard API calls to work in Webview?


OS: Android 10 Q

Webview: v. 81.0.4044.111

like image 248
cyqsimon Avatar asked Apr 16 '20 06:04

cyqsimon


People also ask

Is there a way to request clipboard permission in JS?

It states "attempting to read or write clipboard data will automatically prompt the user for permission if it hasn't already been granted", implying there is no explicit way to request this permission in JS other than just trying to use the clipboard, which I believe is true.

How to get clipboard-write permission in WebView?

Clipboard API's writeText method docs says, we need to obtain clipboard-write permission using Permissions API but navigator.permission is undefined in WebView, maybe because they don't want to mix web permissions with Android OS permissions.

What version of iOS do you use for clipboard API?

I use iOS 13.5 and 13.4 should support the Clipboard API. javascriptiosapisafariclipboard Share Improve this question Follow asked Jun 11 '20 at 15:01 DanielDaniel 5111 silver badge22 bronze badges

How to copy text from Android WebView to clipboard?

There is one more way by which we can copy text to clipboard from Android WebView: by calling native Java method from WebView JavaScript (JS) code. myWebView.getSettings ().setJavaScriptEnabled (true); myWebView.addJavascriptInterface (new WebAppInterface (), "NativeAndroid");


1 Answers

Clipboard API's writeText method docs says, we need to obtain clipboard-write permission using Permissions API but navigator.permission is undefined in WebView, maybe because they don't want to mix web permissions with Android OS permissions.

There is one more way by which we can copy text to clipboard from Android WebView: by calling native Java method from WebView JavaScript (JS) code.

Enable JS in WebView:

myWebView.getSettings().setJavaScriptEnabled(true); 

Add JS interface:

myWebView.addJavascriptInterface(new WebAppInterface(), "NativeAndroid"); 

Create a method to copy the text to clipboard using android.content.ClipboardManager:

public class WebAppInterface {     @JavascriptInterface     public void copyToClipboard(String text) {         ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);         ClipData clip = ClipData.newPlainText("demo", text);         clipboard.setPrimaryClip(clip);     } } 

Then call the above method using testClipboard:

function testClipboard() {   navigator.clipboard.writeText("Clipboard API Test").then(     v => alert("Success"),     e => alert("Fail\n" + e));        NativeAndroid.copyToClipboard("Clipboard API Test"); } 
like image 145
Ankit Makwana Avatar answered Sep 22 '22 07:09

Ankit Makwana