plugins: HFS.copyTextToClipboard
Massimo Melina committed
Nov 12, 2024 at 14:51 UTC
c6d1d432dfb7bf211536b319ca1593dffcc7c8bb
4 files changed
+19
-4
admin/src/ConfigFilePage.ts
+2
-2
@@ -3,7 +3,7 @@
3
import { createElement as h, Fragment, useEffect, useState } from 'react';
4
import { apiCall, useApiEx } from './api'
5
import { Alert, Box } from '@mui/material'
6
-import { focusSelector, isCtrlKey, KeepInScreen } from './misc';
6
+import { copyTextToClipboard, focusSelector, isCtrlKey, KeepInScreen } from './misc'
7
import { Btn, Flex, IconBtn, reloadBtn } from './mui';
8
import { Save, ContentCopy, Edit } from '@mui/icons-material'
9
import { TextEditor } from './TextEditor';
@@ -62,7 +62,7 @@ export default function ConfigFilePage() {
62
63
function copy() {
64
if (!text) return
65
- navigator.clipboard.writeText(text.replace(/^(\s*(\w*password\w*|srp):\s*).+\n/gm, '$1: removed\n'))
65
+ copyTextToClipboard(text.replace(/^(\s*(\w*password\w*|srp):\s*).+\n/gm, '$1: removed\n'))
66
toast("copied")
67
}
68
}
admin/src/FileForm.ts
+2
-2
@@ -9,7 +9,7 @@ import { apiCall, UseApi } from './api'
9
import {
10
basename, defaultPerms, formatBytes, formatTimestamp, isWhoObject, newDialog, objSameKeys,
11
onlyTruthy, prefix, VfsPerms, wantArray, Who, WhoObject, matches, HTTP_MESSAGES, xlate, md, Callback,
12
- useRequestRender, splitAt, IMAGE_FILEMASK
12
+ useRequestRender, splitAt, IMAGE_FILEMASK, copyTextToClipboard
13
} from './misc'
14
import { isModifiedConfig } from './AccountForm'
15
import { Btn, Flex, IconBtn, LinkBtn, propsForModifiedValues, useBreakpoint, wikiLink } from './mui'
@@ -356,7 +356,7 @@ function LinkField({ value, statusApi }: LinkFieldProps) {
356
icon: ContentCopy,
357
title: "Copy",
358
disabled: !link,
359
- onClick: () => navigator.clipboard.writeText(link)
359
+ onClick: () => copyTextToClipboard(link)
360
}),
361
h(IconBtn, { icon: QrCode2, title: "QR Code", onClick: showQr, disabled: !link }),
362
h(IconBtn, { icon: Edit, title: "Change", onClick() { changeBaseUrl().then(reload) } }),
dev-plugins.md
+3
@@ -282,6 +282,7 @@ The HFS objects contains many properties:
282
- `DirEntry: class_constructor(n :string, otherProps?: DirEntry)` this is the class of the objects inside `HFS.state.list`;
283
in case you need to add to the list, do it by instantiating this class. E.g. `new HFS.DirEntry(name)`
284
- `fileShow(entry: DirEntry, options?: { startPlaying: true )` open file-show on the specified entry.
285
+- `copyTextToClipboard(text: string)` self-explanatory
286
287
The following properties are accessible only immediately at top-level; don't call it later in a callback.
288
- `getPluginConfig()` returns object of all config keys that are declared frontend-accessible by this plugin.
@@ -672,6 +673,8 @@ If you want to override a text regardless of the language, use the special langu
673
674
## API version history
675
676
+- 10 (v0.55.0)
677
+ - HFS.copyTextToClipboard
678
- 9.6 (v0.54.0)
679
- frontend event: showPlay
680
- api.addBlock
shared/index.ts
+12
@@ -31,6 +31,7 @@ Object.assign(HFS, {
31
loadScript: (uri: string) => loadScript(uri.includes('//') || uri.startsWith('/') ? uri : HFS.getPluginPublic() + uri),
32
userBelongsTo: (groupOrUser: string) => HFS.state.expandedUsername.includes(groupOrUser),
33
cpuSpeedIndex,
34
+ copyTextToClipboard,
35
})
36
37
export const IMAGE_FILEMASK = '*.jpg|*.jpeg|*.gif|*.svg'
@@ -177,6 +178,17 @@ export function createDurationFormatter({ locale=undefined, unitDisplay='narrow'
178
}
179
}
180
181
+export function copyTextToClipboard(text: string) {
182
+ //navigator.clipboard.writeText(text) // this method works only in https and localhost
183
+ const d = document
184
+ const ta = d.createElement("textarea")
185
+ ta.textContent = text
186
+ d.body.appendChild(ta)
187
+ ta.select()
188
+ d.execCommand("copy")
189
+ d.body.removeChild(ta)
190
+}
191
+
192
Element.prototype.replaceChildren ||= function(this:Element, addNodes) { // polyfill
193
while (this.lastChild) this.removeChild(this.lastChild);
194
if (addNodes !== undefined) this.append(addNodes);