UNPKG

5.17 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2019 RedHat and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.BrowserClipboardService = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const browser_1 = require("./browser");
22const logger_1 = require("../common/logger");
23const message_service_1 = require("../common/message-service");
24const nls_1 = require("../common/nls");
25let BrowserClipboardService = class BrowserClipboardService {
26 async readText() {
27 let permission;
28 try {
29 permission = await this.queryPermission('clipboard-read');
30 }
31 catch (e1) {
32 this.logger.error('Failed checking a clipboard-read permission.', e1);
33 // in FireFox, Clipboard API isn't gated with the permissions
34 try {
35 return await this.getClipboardAPI().readText();
36 }
37 catch (e2) {
38 this.logger.error('Failed reading clipboard content.', e2);
39 if (browser_1.isFirefox) {
40 this.messageService.warn(nls_1.nls.localize('theia/navigator/clipboardWarnFirefox',
41 // eslint-disable-next-line max-len
42 "Clipboard API is not available. It can be enabled by '{0}' preference on '{1}' page. Then reload Theia. Note, it will allow FireFox getting full access to the system clipboard.", 'dom.events.testing.asyncClipboard', 'about:config'));
43 }
44 return '';
45 }
46 }
47 if (permission.state === 'denied') {
48 // most likely, the user intentionally denied the access
49 this.messageService.warn(nls_1.nls.localize('theia/navigator/clipboardWarn', "Access to the clipboard is denied. Check your browser's permission."));
50 return '';
51 }
52 return this.getClipboardAPI().readText();
53 }
54 async writeText(value) {
55 let permission;
56 try {
57 permission = await this.queryPermission('clipboard-write');
58 }
59 catch (e1) {
60 this.logger.error('Failed checking a clipboard-write permission.', e1);
61 // in FireFox, Clipboard API isn't gated with the permissions
62 try {
63 await this.getClipboardAPI().writeText(value);
64 return;
65 }
66 catch (e2) {
67 this.logger.error('Failed writing to the clipboard.', e2);
68 if (browser_1.isFirefox) {
69 this.messageService.warn(nls_1.nls.localize('theia/core/navigator/clipboardWarnFirefox',
70 // eslint-disable-next-line max-len
71 "Clipboard API is not available. It can be enabled by '{0}' preference on '{1}' page. Then reload Theia. Note, it will allow FireFox getting full access to the system clipboard.", 'dom.events.testing.asyncClipboard', 'about:config'));
72 }
73 return;
74 }
75 }
76 if (permission.state === 'denied') {
77 // most likely, the user intentionally denied the access
78 this.messageService.warn(nls_1.nls.localize('theia/core/navigator/clipboardWarn', "Access to the clipboard is denied. Check your browser's permission."));
79 return;
80 }
81 return this.getClipboardAPI().writeText(value);
82 }
83 async queryPermission(name) {
84 if ('permissions' in navigator) {
85 return navigator['permissions'].query({ name: name });
86 }
87 throw new Error('Permissions API unavailable');
88 }
89 getClipboardAPI() {
90 if ('clipboard' in navigator) {
91 return navigator['clipboard'];
92 }
93 throw new Error('Async Clipboard API unavailable');
94 }
95};
96(0, tslib_1.__decorate)([
97 (0, inversify_1.inject)(message_service_1.MessageService),
98 (0, tslib_1.__metadata)("design:type", message_service_1.MessageService)
99], BrowserClipboardService.prototype, "messageService", void 0);
100(0, tslib_1.__decorate)([
101 (0, inversify_1.inject)(logger_1.ILogger),
102 (0, tslib_1.__metadata)("design:type", Object)
103], BrowserClipboardService.prototype, "logger", void 0);
104BrowserClipboardService = (0, tslib_1.__decorate)([
105 (0, inversify_1.injectable)()
106], BrowserClipboardService);
107exports.BrowserClipboardService = BrowserClipboardService;
108//# sourceMappingURL=browser-clipboard-service.js.map
\No newline at end of file