summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/copy-to-clipboard.ts
blob: 216c0464b3d5a0d300debef9bd70020853418ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

/**
 * Clipboardに値をコピー(TODO: 文字列以外も対応)
 */
export default val => {
	// 空div 生成
	const tmp = document.createElement('div');
	// 選択用のタグ生成
	const pre = document.createElement('pre');

	// 親要素のCSSで user-select: none だとコピーできないので書き換える
	pre.style.webkitUserSelect = 'auto';
	pre.style.userSelect = 'auto';

	tmp.appendChild(pre).textContent = val;

	// 要素を画面外へ
	const s = tmp.style;
	s.position = 'fixed';
	s.right = '200%';

	// body に追加
	document.body.appendChild(tmp);
	// 要素を選択
	document.getSelection().selectAllChildren(tmp);

	// クリップボードにコピー
	const result = document.execCommand('copy');

	// 要素削除
	document.body.removeChild(tmp);

	return result;
};