# Can't select if iframe context?

**URL:** <https://discourse.mozilla.org/t/cant-select-if-iframe-context/40488>\
**Category:** Development\
**Created:** [May 17, 2019, 7:04am UTC](https://discourse.mozilla.org/t/cant-select-if-iframe-context/40488 "2019-05-17T07:04:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddawson](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ddawson/32/21727_2.png) [@ddawson](https://discourse.mozilla.org/u/ddawson)\
**Post date:** [May 17, 2019, 7:04am UTC](https://discourse.mozilla.org/t/cant-select-if-iframe-context/40488/1 "2019-05-17T07:04:01Z")

</div>

In order to copy some text into the clipboard, I have some code in a content script like this:

```
let copyBox = document.createElement("input");
copyBox.type = "text";
copyBox.style.display = "block";
copyBox.style.position = "absolute";
copyBox.style.top = "-1000px";
copyBox.style.left = "-1000px";
copyBox.style.opacity = "0";
copyBox.value = url;
document.body.appendChild(copyBox);
copyBox.select();
document.execCommand("copy");
document.body.removeChild(copyBox);

```

(BTW, is there a better way to do this than going through an invisible element?)

This is run ultimately from a context menu command. It works fine _as long as the target is in the same document_. But if it’s in an iframe, I find the selection usually doesn’t work, even though the element being created is outside the iframe. Or maybe because of that? What can I do to fix this?

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [May 17, 2019, 10:10am UTC](https://discourse.mozilla.org/t/cant-select-if-iframe-context/40488/2 "2019-05-17T10:10:28Z")

</div>

> [@ddawson](#):
>
> (BTW, is there a better way to do this than going through an invisible element?)

You can use [Navigator: clipboard property - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard) as an extension.

> [@ddawson](#):
>
> But if it’s in an iframe, I find the selection usually doesn’t work, even though the element being created is outside the iframe. Or maybe because of that? What can I do to fix this?

The context menu event gives you the frameId where the element that was targeted is. That way you can inject the script where it’s needed resp. send the message to the script in the correct frame.

---

<div class="post-metadata">

**Author:** ![ddawson](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ddawson/32/21727_2.png) [@ddawson](https://discourse.mozilla.org/u/ddawson)\
**Post date:** [May 17, 2019, 12:04pm UTC](https://discourse.mozilla.org/t/cant-select-if-iframe-context/40488/3 "2019-05-17T12:04:19Z")

</div>

> You can use [Navigator: clipboard property - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard) as an extension.

Right. I’ll do that. I don’t remember it being available when I was doing most of the development on this add-on. It’s good that it is now.

> The context menu event gives you the frameId where the element that was targeted is. That way you can inject the script where it’s needed resp. send the message to the script in the correct frame.

Turns out this is unnecessary with the above. I can use the Clipboard API without involving a content script.

Thanks for the help.
