-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard-helper.js
70 lines (63 loc) · 1.46 KB
/
clipboard-helper.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* BB Code Helper
* Copyright 2017 Mark Vincent
* https://github.com/WildcardSearch/BB-Code-Helper
*
* this file contains content scripts for clipboard manipulation
*/
/**
* @var string
*/
var clipboardText = "";
/**
* launch a fake copy event and hijack it with our own text
*
* @param string
* @param boolean
* @return void
*/
function copyTag(text, append, sep) {
/**
* event handler for synthetic copy event
*
* @param event
* @return void
*/
function onCopy(e) {
document.removeEventListener("copy", onCopy, true);
e.stopImmediatePropagation();
e.preventDefault();
// multi-mode
if (typeof append !== "undefined" &&
append === true &&
clipboardText.length > 0) {
text = clipboardText + sep + text;
}
e.clipboardData.setData("text/plain", text);
e.clipboardData.setData("text/html", "");
}
text = text || "";
sep = sep || "";
document.addEventListener("copy", onCopy, true);
document.execCommand("copy");
}
/**
* launch a fake paste event and use it to fetch the clipboard data
*
* @return void
*/
function getClipboardData() {
/**
* event handler for synthetic paste event
*
* @return void
*/
function onPaste(e) {
document.removeEventListener("paste", onPaste, true);
e.stopImmediatePropagation();
// set global variables to be used when copying tags
clipboardText = e.clipboardData.getData("text/plain");
}
document.addEventListener("paste", onPaste, true);
document.execCommand("paste");
}