## 同一ドメインの場合
```ts
navigator.clipboard.writeText(textToCopy);
```
## ドメインが異なる場合
[[iframeタグ]]などでドメインが異なる場合は[[CORS]]ではじかれる。
```ts
navigator.clipboard.writeText(textToCopy);
```
https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-permissions-in-cross-origin-iframes
以下の関数をコンテンツスクリプトに記載する。
```ts
function copyToTheClipboard(textToCopy) {
const el = document.createElement("textarea");
el.value = textToCopy;
el.setAttribute("readonly", "");
el.style.position = "absolute";
el.style.left = "-9999px";
const ifc = document.querySelector("iframe内の一意に特定できるセレクタ");
ifc.appendChild(el);
el.select();
document.execCommand("copy");
ifc.removeChild(el);
}
```
`ifc`で取得したiframe内のDOMに対してappendすることにより `el.select()` が機能するようにするのがミソ。
## 参考
- [javascript \- Copy to clipboard in chrome extension V3 \- Stack Overflow](https://stackoverflow.com/questions/71321983/copy-to-clipboard-in-chrome-extension-v3)