admin管理员组

文章数量:1426017

I'm trying to use navigator.clipboard.readText() in a tab opened by my Google Chrome extension. When I do, the below permission alert appears on every page load, even though I have granted the permission already:

This is the relevant code:

<button onclick="getClipboardContents()">Add</button>

<script>
  async function getClipboardContents() {
    try {
      const text = await navigator.clipboard.readText();
      console.log('Pasted content: ', text);
    } catch (err) {
      console.error('Failed to read clipboard contents: ', err);
    }
  }
</script>

Is there a way to stop this message from appearing every time?

I'm trying to use navigator.clipboard.readText() in a tab opened by my Google Chrome extension. When I do, the below permission alert appears on every page load, even though I have granted the permission already:

This is the relevant code:

<button onclick="getClipboardContents()">Add</button>

<script>
  async function getClipboardContents() {
    try {
      const text = await navigator.clipboard.readText();
      console.log('Pasted content: ', text);
    } catch (err) {
      console.error('Failed to read clipboard contents: ', err);
    }
  }
</script>

Is there a way to stop this message from appearing every time?

Share Improve this question edited Jun 6, 2023 at 18:41 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked May 25, 2022 at 14:26 Consider Non-Trivial CasesConsider Non-Trivial Cases 692 silver badges12 bronze badges 2
  • I've updated my answer with a working example. – Besworks Commented May 27, 2022 at 21:10
  • 2 Why is this tagged google-chrome-extension? Because for a page in an extension, the answer may be different. But there isn't any indication in the question that this is the case. – Xan Commented May 31, 2022 at 9:24
Add a ment  | 

2 Answers 2

Reset to default 4 +50

You don't specify how you are opening the page. The permission dialog indicates that you are accessing it via a file:// URL. Tabs, opened by an extension should have a chrome-extension:// URL and the permission dialog should show the name of your extension :

You need to add the clipboardRead permission to your manifest.json and open the tab with chrome.tabs.create() and chrome.runtime.getURL(). When done in this manner, the permission dialog should only appear on the first page load and not every time.

Here is a minimal reproducible example :

manifest.json

{
  "manifest_version": 3,
  "name": "Clipboard Test",
  "version": "0.1",
  "permissions": [
    "clipboardRead"
  ],
  "action": {},
  "background": {
    "service_worker": "background.js"
  }
}

background.js

chrome.action.onClicked.addListener(currentTab => {
  chrome.tabs.create({
    'url': chrome.runtime.getURL("test.html")
  });
});

test.html

<!DOCTYPE html>
<title> Clipboard Test </title>
<button id="test"> TEST </button>
<script src="clip.js"></script>

clip.js

const button = document.querySelector('button#test');

async function paste() {
  let text = await navigator.clipboard.readText();
  alert(text);
}

button.addEventListener('click', paste);

This is simply impossible. Google Chrome displays such an alert so that the user can either grant clipboard privileges or deny the privileges.

本文标签: