-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
313 lines (269 loc) · 6.07 KB
/
background.js
File metadata and controls
313 lines (269 loc) · 6.07 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* BB Code Helper
* Copyright 2017 Mark Vincent
* https://github.com/WildcardSearch/BB-Code-Helper
*
* this file contains content scripts for clipboard manipulation
*/
browser.contextMenus.create({
id: "mft-selection-quote",
title: browser.i18n.getMessage("menuItemQuote"),
contexts: ["selection"],
});
browser.contextMenus.create({
id: "mft-selection-code",
title: browser.i18n.getMessage("menuItemCode"),
contexts: ["selection"],
});
browser.contextMenus.create({
id: "mft-image-full",
title: browser.i18n.getMessage("menuItemImageFull"),
contexts: ["image"],
});
browser.contextMenus.create({
id: "mft-image",
title: browser.i18n.getMessage("menuItemImage"),
contexts: ["image"],
});
browser.contextMenus.create({
id: "mft-link",
title: browser.i18n.getMessage("menuItemLink"),
contexts: ["link"],
});
browser.contextMenus.create({
id: "mft-clear-clipboard",
title: browser.i18n.getMessage("menuClearClipboard"),
contexts: ["all"],
});
var multiMode = false;
browser.contextMenus.create({
id: "mft-multi-mode",
title: browser.i18n.getMessage("menuItemMultiMode"),
type: "checkbox",
contexts: ["all"],
checked: multiMode,
});
browser.contextMenus.onClicked.addListener(onClicked);
/**
* event handler for context menu click
*
* @param object
* @param object
* @return void
*/
function onClicked(info, tab) {
var text = "",
sep = "\n\n",
outputMarkdown = false,
promiseChain;
if (info.menuItemId == "mft-multi-mode") {
multiMode = !multiMode;
return;
}
if (["mft-selection-quote", "mft-selection-code", "mft-image-full", "mft-image", "mft-link", "mft-clear-clipboard"].indexOf(info.menuItemId) === -1) {
return;
}
// load content script and settings
promiseChain = loadContentScript(tab).then(() => {
return browser.storage.local.get(["lineBreaks", "outputFormat"]);
});
if (info.menuItemId == "mft-clear-clipboard") {
// clear clipboard
promiseChain.then(() => {
sendToClipboard(tab);
});
return;
}
promiseChain.then((result) => {
if (typeof result === "undefined") {
return;
}
if (!result.lineBreaks ||
result.lineBreaks == 0) {
sep = "";
} else if (result.lineBreaks == 1) {
sep = "\n";
}
text = processClick(info, result.outputFormat == 1);
if (!text) {
return;
}
return browser.tabs.executeScript(tab.id, {
code: "getClipboardData();",
});
}).then(() => {
return sendToClipboard(tab, text, multiMode, sep);
}).catch((error) => {
console.error(browser.i18n.getMessage("errorGeneric") + error);
});
}
/**
* load the content script, if necessary
*
* @param Object
* @return Promise
*/
function loadContentScript(tab) {
return browser.tabs.executeScript(tab.id, {
code: "typeof copyTag === 'function';",
}).then((results) => {
if (!results ||
results[0] !== true) {
return browser.tabs.executeScript(tab.id, {
file: "clipboard-helper.js",
});
}
});
}
/**
* glue all options together and call the content script
*
* @param Object
* @param String
* @param Boolean
* @param String
* @return Promise
*/
function sendToClipboard(tab, text, append, sep) {
var copyCode = "";
text = text ? JSON.stringify(text) : '""';
append = append === true ? "true" : "false";
sep = sep ? JSON.stringify(sep) : '""';
copyCode = `copyTag(${text}, ${append}, ${sep});`;
return browser.tabs.executeScript(tab.id, {
code: copyCode,
});
}
/**
* determine which tag has bee requested and return the appropriate string
*
* @param object
* @param boolean
* @return string
*/
function processClick(info, outputMarkdown) {
var text = "";
switch (info.menuItemId) {
case "mft-selection-quote":
if (typeof info.selectionText == "undefined" ||
!info.selectionText) {
return;
}
text = wrapQuote(info.selectionText, outputMarkdown);
break;
case "mft-selection-code":
if (typeof info.selectionText == "undefined" ||
!info.selectionText) {
return;
}
text = wrapCode(info.selectionText, outputMarkdown);
break;
case "mft-image-full":
if (typeof info.srcUrl === "undefined" ||
!info.srcUrl) {
return;
}
text = wrapImage(info.srcUrl, outputMarkdown);
if (typeof info.linkUrl !== "undefined" &&
info.linkUrl) {
text = wrapLink(info.linkUrl, text, outputMarkdown);
}
break;
case "mft-image":
if (typeof info.srcUrl == "undefined" ||
!info.srcUrl) {
return;
}
text = wrapImage(info.srcUrl, outputMarkdown);
break;
case "mft-link":
if (typeof info.linkUrl == "undefined" ||
!info.linkUrl) {
return;
}
text = wrapLink(info.linkUrl, info.linkText, outputMarkdown);
break;
}
return text;
}
/**
* return representation of quoted text
*
* @param string
* @param boolean
* @return string
*/
function wrapQuote(text, useMarkdown) {
text = sanitizeText(text);
if (useMarkdown !== true) {
return `[quote]${text}[/quote]`;
}
return `> ${text}`;
}
/**
* return representation of code block
*
* @param string
* @param boolean
* @return string
*/
function wrapCode(text, useMarkdown) {
text = sanitizeText(text);
if (useMarkdown !== true) {
return `[code]${text}[/code]`;
}
return "```\n" + text + "\n```";
}
/**
* return representation of link
*
* @param string
* @param string
* @param boolean
* @return string
*/
function wrapLink(url, caption, useMarkdown) {
if (typeof url === "undefined" ||
(typeof url === "string" && url.length == 0)) {
return;
}
caption = sanitizeText(caption);
if (useMarkdown !== true) {
if (caption) {
return `[url=${url}]${caption}[/url]`;
}
return `[url]${url}[/url]`;
}
return `[${caption}](${url})`;
}
/**
* return representation of link
*
* @param string
* @param boolean
* @return string
*/
function wrapImage(url, useMarkdown) {
if (typeof url === "undefined" ||
(typeof url === "string" && url.length == 0)) {
return;
}
if (useMarkdown !== true) {
return `[img]${url}[/img]`;
}
return ``;
}
/**
* return sanitized string
*
* @param string
* @return string
*/
function sanitizeText(text) {
return text
.replace(/&/g, "&")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/</g, "<")
.replace(/>/g, ">");
}