-
Notifications
You must be signed in to change notification settings - Fork 8
/
clipboard-helper.js
157 lines (146 loc) · 4.5 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// This function must be called in a visible page, such as a browserAction popup
// or a content script. Calling it in a background page has no effect!
function FormatLink_copyHTMLToClipboard(text) {
function oncopy(event) {
document.removeEventListener("copy", oncopy, true);
// Hide the event from the page to prevent tampering.
event.stopImmediatePropagation();
// Overwrite the clipboard content.
event.preventDefault();
event.clipboardData.setData("text/plain", text);
event.clipboardData.setData("text/html", text);
}
document.addEventListener("copy", oncopy, true);
// Requires the clipboardWrite permission, or a user gesture:
document.execCommand("copy");
}
function FormatLink_formatLink(format, options, newline, linkUrl, linkText) {
function getFirstLinkInSelection(selection) {
return selection.anchorNode.parentNode.href;
}
function formatURL(format, url, title, text, selectedText, newline, isVar) {
let result = '';
let i = 0, len = format.length;
function parseLiteral(str) {
if (format.substr(i, str.length) === str) {
i += str.length;
return str;
} else {
return null;
}
}
function parseString() {
let str = '';
if (parseLiteral('"')) {
while (i < len) {
if (parseLiteral('\\')) {
if (i < len) {
str += format.substr(i++, 1);
} else {
throw new Error('parse error expected "');
}
} else if (parseLiteral('"')) {
return str;
} else {
if (i < len) {
str += format.substr(i++, 1);
} else {
throw new Error('parse error expected "');
}
}
}
} else {
return null;
}
}
function processVar(name, value) {
let work = value;
if (!isVar) {
const varFormat = options[name + '_format'];
if (varFormat) {
work = formatURL(varFormat, url, title, text, selectedText, newline, true);
}
}
while (i < len) {
if (parseLiteral('.s(')) {
let arg1 = parseString();
if (arg1 != null && parseLiteral(',')) {
let arg2 = parseString();
if (arg2 != null && parseLiteral(')')) {
let regex = new RegExp(arg1, 'g');
work = work.replace(regex, arg2);
} else {
throw new Error('parse error');
}
} else {
throw new Error('parse error');
}
} else if (parseLiteral('}}')) {
result += work;
return;
} else {
throw new Error('parse error');
}
}
}
while (i < len) {
if (parseLiteral('\\')) {
if (parseLiteral('n')) {
result += newline;
// isWindows ? "\r\n" : "\n";
} else if (parseLiteral('t')) {
result += "\t";
} else {
result += format.substr(i++, 1);
}
} else if (parseLiteral('{{')) {
if (parseLiteral('title')) {
processVar('title', title);
} else if (parseLiteral('url')) {
processVar('url', url);
} else if (parseLiteral('page_url')) {
processVar('page_url', window.location.href);
} else if (parseLiteral('selected_text')) {
processVar('selected_text', selectedText);
} else if (parseLiteral('text')) {
processVar('text', text);
}
} else {
result += format.substr(i++, 1);
}
}
return result;
}
let title = document.title;
let text = linkText;
let href = linkUrl;
let selectedText = '';
let selection = window.getSelection();
if (selection.rangeCount > 0) {
selectedText = selection.toString().trim();
console.log('set selectedText from window.getSelection', selectedText);
if (selectedText) {
let hrefInSelection = getFirstLinkInSelection(selection);
if (!href && hrefInSelection) {
href = hrefInSelection;
}
}
}
if (!selectedText) {
const elem = document.activeElement;
if (elem && elem.selectionStart !== elem.selectionEnd) {
selectedText = elem.value.substring(elem.selectionStart, elem.selectionEnd);
console.log('set selectedText from activeElement', selectedText);
}
}
if (selectedText) {
text = selectedText;
}
if (!text) {
text = title;
}
if (!href) {
href = window.location.href;
}
return formatURL(format, href, title, text, selectedText, newline);
}