Skip to content

Commit 3d9d99d

Browse files
authored
Merge pull request #50478 from nextcloud/backport/49141/stable30
2 parents 6cac474 + aa10d46 commit 3d9d99d

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

core/src/init.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { setUp as setUpContactsMenu } from './components/ContactsMenu.js'
1414
import { setUp as setUpMainMenu } from './components/MainMenu.js'
1515
import { setUp as setUpUserMenu } from './components/UserMenu.js'
1616
import { interceptRequests } from './utils/xhr-request.js'
17+
import { initFallbackClipboardAPI } from './utils/ClipboardFallback.ts'
1718

1819
// keep in sync with core/css/variables.scss
1920
const breakpointMobileWidth = 1024
@@ -58,6 +59,7 @@ moment.locale(locale)
5859
*/
5960
export const initCore = () => {
6061
interceptRequests()
62+
initFallbackClipboardAPI()
6163

6264
$(window).on('unload.main', () => { OC._unloadCalled = true })
6365
$(window).on('beforeunload.main', () => {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
import { t } from '@nextcloud/l10n'
6+
7+
/**
8+
*
9+
* @param text
10+
*/
11+
function unsecuredCopyToClipboard(text) {
12+
const textArea = document.createElement('textarea')
13+
const textAreaContent = document.createTextNode(text)
14+
textArea.appendChild(textAreaContent)
15+
document.body.appendChild(textArea)
16+
17+
textArea.focus({ preventScroll: true })
18+
textArea.select()
19+
20+
try {
21+
// This is a fallback for browsers that do not support the Clipboard API
22+
// execCommand is deprecated, but it is the only way to copy text to the clipboard in some browsers
23+
document.execCommand('copy')
24+
} catch (err) {
25+
window.prompt(t('core', 'Clipboard not available, please copy manually'), text)
26+
console.error('[ERROR] core: files Unable to copy to clipboard', err)
27+
}
28+
29+
document.body.removeChild(textArea)
30+
}
31+
32+
/**
33+
*
34+
*/
35+
function initFallbackClipboardAPI() {
36+
if (!window.navigator?.clipboard?.writeText) {
37+
console.info('[INFO] core: Clipboard API not available, using fallback')
38+
Object.defineProperty(window.navigator, 'clipboard', {
39+
value: {
40+
writeText: unsecuredCopyToClipboard,
41+
},
42+
writable: false,
43+
})
44+
}
45+
}
46+
47+
export { initFallbackClipboardAPI }

dist/core-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)