Add clipboard script commands Closes #3958

6625e4036ecca52a072465ab614741bd4ec09ac8

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +40 -1Showing whitespace changes
public/scripts/slash-commands.js+40 -1
@@ -1,5 +1,5 @@
1import { Fuse, DOMPurify } from '../lib.js';1import { Fuse, DOMPurify } from '../lib.js';
2import { flashHighlight } from './utils.js';2import { copyText, flashHighlight } from './utils.js';
33
4import {4import {
5 Generate,5 Generate,
@@ -2284,6 +2284,45 @@ export function initDefaultSlashCommands() {
2284 `,2284 `,
2285 }));2285 }));
22862286
2287 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2288 name: 'clipboard-get',
2289 returns: 'clipboard text',
2290 callback: async () => {
2291 if (!navigator.clipboard) {
2292 toastr.warning('Clipboard API not available in this context.');
2293 return '';
2294 }
2295
2296 try {
2297 const text = await navigator.clipboard.readText();
2298 return text;
2299 }
2300 catch (error) {
2301 console.error('Error reading clipboard:', error);
2302 toastr.warning('Failed to read clipboard text. Have you granted the permission?');
2303 return '';
2304 }
2305 },
2306 helpString: 'Retrieves the text from the OS clipboard. Only works in secure contexts (HTTPS or localhost). Browser may ask for permission.',
2307 }));
2308
2309 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2310 name: 'clipboard-set',
2311 callback: async (_, text) => {
2312 await copyText(text.toString());
2313 return '';
2314 },
2315 unnamedArgumentList: [
2316 SlashCommandArgument.fromProps({
2317 description: 'text to copy to the clipboard',
2318 typeList: [ARGUMENT_TYPE.STRING],
2319 isRequired: true,
2320 acceptsMultiple: false,
2321 }),
2322 ],
2323 helpString: 'Copies the provided text to the OS clipboard. Returns an empty string.',
2324 }));
2325
2287 registerVariableCommands();2326 registerVariableCommands();
2288}2327}
22892328