-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): offer clipboard fallback for non-secure environments
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import { t } from '@nextcloud/l10n' | ||
|
||
/** | ||
* | ||
* @param text | ||
*/ | ||
function unsecuredCopyToClipboard(text) { | ||
const textArea = document.createElement('textarea') | ||
const textAreaContent = document.createTextNode(text) | ||
textArea.appendChild(textAreaContent) | ||
document.body.appendChild(textArea) | ||
|
||
textArea.focus({ preventScroll: true }) | ||
textArea.select() | ||
|
||
try { | ||
// This is a fallback for browsers that do not support the Clipboard API | ||
// execCommand is deprecated, but it is the only way to copy text to the clipboard in some browsers | ||
document.execCommand('copy') | ||
} catch (err) { | ||
window.prompt(t('core', 'Clipboard not available, please copy manually'), text) | ||
console.error('[ERROR] core: files Unable to copy to clipboard', err) | ||
} | ||
|
||
document.body.removeChild(textArea) | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
function initFallbackClipboardAPI() { | ||
if (!window.navigator?.clipboard?.writeText) { | ||
console.info('[INFO] core: Clipboard API not available, using fallback') | ||
Object.defineProperty(window.navigator, 'clipboard', { | ||
value: { | ||
writeText: unsecuredCopyToClipboard, | ||
}, | ||
writable: false, | ||
}) | ||
} | ||
} | ||
|
||
export { initFallbackClipboardAPI } |