summaryrefslogtreecommitdiffstats
path: root/browser/extensions/screenshots/clipboard.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /browser/extensions/screenshots/clipboard.js
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/extensions/screenshots/clipboard.js')
-rw-r--r--browser/extensions/screenshots/clipboard.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/browser/extensions/screenshots/clipboard.js b/browser/extensions/screenshots/clipboard.js
new file mode 100644
index 0000000000..313a3e6372
--- /dev/null
+++ b/browser/extensions/screenshots/clipboard.js
@@ -0,0 +1,58 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* globals catcher, assertIsBlankDocument */
+
+"use strict";
+
+this.clipboard = (function() {
+ const exports = {};
+
+ exports.copy = function(text) {
+ return new Promise((resolve, reject) => {
+ const element = document.createElement("iframe");
+ element.src = browser.extension.getURL("blank.html");
+ // We can't actually hide the iframe while copying, but we can make
+ // it close to invisible:
+ element.style.opacity = "0";
+ element.style.width = "1px";
+ element.style.height = "1px";
+ element.style.display = "block";
+ element.addEventListener("load", catcher.watchFunction(() => {
+ try {
+ const doc = element.contentDocument;
+ assertIsBlankDocument(doc);
+ const el = doc.createElement("textarea");
+ doc.body.appendChild(el);
+ el.value = text;
+ if (!text) {
+ const exc = new Error("Clipboard copy given empty text");
+ exc.noPopup = true;
+ catcher.unhandled(exc);
+ }
+ el.select();
+ if (doc.activeElement !== el) {
+ const unhandledTag = doc.activeElement ? doc.activeElement.tagName : "No active element";
+ const exc = new Error("Clipboard el.select failed");
+ exc.activeElement = unhandledTag;
+ exc.noPopup = true;
+ catcher.unhandled(exc);
+ }
+ const copied = doc.execCommand("copy");
+ if (!copied) {
+ catcher.unhandled(new Error("Clipboard copy failed"));
+ }
+ el.remove();
+ resolve(copied);
+ } finally {
+ element.remove();
+ }
+ }), {once: true});
+ document.body.appendChild(element);
+ });
+ };
+
+ return exports;
+})();
+null;