Skip to content

Commit

Permalink
feat(core): offer clipboard fallback for non-secure environments
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Nov 8, 2024
1 parent 5193ca4 commit 6296b03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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'

Check failure on line 17 in core/src/init.js

View workflow job for this annotation

GitHub Actions / NPM lint

Unable to resolve path to module './utils/ClipboardFallback'

Check failure on line 17 in core/src/init.js

View workflow job for this annotation

GitHub Actions / NPM lint

"./utils/ClipboardFallback" is not found

// 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
35 changes: 35 additions & 0 deletions core/src/utils/ClipboardFallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
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 {
document.execCommand('copy')
} catch (err) {
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 }

0 comments on commit 6296b03

Please sign in to comment.