Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): offer clipboard fallback for non-secure environments #49141

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { setUp as setUpContactsMenu } from './components/ContactsMenu.js'
import { setUp as setUpMainMenu } from './components/MainMenu.js'
import { setUp as setUpUserMenu } from './components/UserMenu.js'
import { interceptRequests } from './utils/xhr-request.js'
import { initFallbackClipboardAPI } from './utils/ClipboardFallback.ts'

// keep in sync with core/css/variables.scss
const breakpointMobileWidth = 1024
Expand Down Expand Up @@ -58,6 +59,7 @@ moment.locale(locale)
*/
export const initCore = () => {
interceptRequests()
initFallbackClipboardAPI()

$(window).on('unload.main', () => { OC._unloadCalled = true })
$(window).on('beforeunload.main', () => {
Expand Down
47 changes: 47 additions & 0 deletions core/src/utils/ClipboardFallback.ts
skjnldsv marked this conversation as resolved.
Show resolved Hide resolved
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 }
4 changes: 2 additions & 2 deletions dist/core-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-main.js.map

Large diffs are not rendered by default.

Loading