-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.js
148 lines (137 loc) · 4.01 KB
/
common.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
const FORMAT_MAX_COUNT = 9;
const DEFAULT_OPTIONS = {
"defaultFormat": "1",
"title1": "Markdown",
"format1": "[{{text.s(\"\\\\[\",\"\\\\[\").s(\"\\\\]\",\"\\\\]\")}}]({{url.s(\"\\\\(\",\"%28\").s(\"\\\\)\",\"%29\")}})",
"html1": 0,
"title2": "reST",
"format2": "`{{text}} <{{url}}>`_",
"html2": 0,
"title3": "Text",
"format3": "{{text}}\\n{{url}}",
"html3": 0,
"title4": 'HTML',
"format4": "<a href=\"{{url.s(\"\\\"\",\""\")}}\">{{text.s(\"<\",\"<\")}}</a>",
"html4": 1,
"title5": "LaTeX",
"format5": "\\\\href\\{{{url}}\\}\\{{{text}}\\}",
"html5": 0,
"title6": "",
"format6": "",
"html6": 0,
"title7": "",
"format7": "",
"html7": 0,
"title8": "",
"format8": "",
"html8": 0,
"title9": "",
"format9": "",
"html9": 0,
"page_url_format": "",
"selected_text_format": "",
"text_format": "",
"title_format": "",
"url_format": "",
"enableContextMenus": true,
"createSubmenus": false
};
const VARIABLE_NAMES = [
"page_url",
"selected_text",
"text",
"title",
"url",
];
async function gettingOptions() {
let options = await browser.storage.sync.get(null);
if (Object.keys(options).length === 0) {
options = DEFAULT_OPTIONS;
}
return options;
}
function getFormatCount(options) {
let i;
for (i = 1; i <= 9; ++i) {
let optTitle = options['title' + i];
let optFormat = options['format' + i];
if (optTitle === '' || optFormat === '') {
break;
}
}
return i - 1;
}
async function copyLinkToClipboard(format, asHTML, options, linkUrl, linkText) {
try {
const results = await browser.tabs.executeScript({
code: "typeof FormatLink_formatLink === 'function';",
});
// The content script's last expression will be true if the function
// has been defined. If this is not the case, then we need to run
// clipboard-helper.js to define functions FormatLink_formatLink
// and FormatLink_copyHTMLToClipboard.
if (!results || results[0] !== true) {
await browser.tabs.executeScript({
file: "clipboard-helper.js",
});
}
const newline = browser.runtime.PlatformOs === 'win' ? '\r\n' : '\n';
let code = 'FormatLink_formatLink(' + JSON.stringify(format) + ',' +
JSON.stringify(options) + ',' +
JSON.stringify(newline) + ',' +
(linkUrl ? JSON.stringify(linkUrl) + ',' : '') +
(linkText ? JSON.stringify(linkText) + ',' : '') +
');';
const result = await browser.tabs.executeScript({code});
const data = result[0];
console.log('before copying to clipboard, data=', data, ', asHTML=', asHTML);
if (asHTML) {
await browser.tabs.executeScript({
code: 'FormatLink_copyHTMLToClipboard(' + JSON.stringify(data) + ');'
});
} else {
await navigator.clipboard.writeText(data);
}
console.log('clipboard successfully set by FormatLink');
return data;
} catch (err) {
// This could happen if the extension is not allowed to run code in
// the page, for example if the tab is a privileged page.
console.error('Failed to copy text: ' + err);
}
}
function creatingContextMenuItem(props) {
return new Promise((resolve, reject) => {
browser.contextMenus.create(props, () => {
const err = browser.runtime.lastError;
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
async function createContextMenus(options) {
await browser.contextMenus.removeAll();
if (options.enableContextMenus) {
if (options.createSubmenus) {
const count = getFormatCount(options);
for (let i = 0; i < count; i++) {
let format = options['title' + (i + 1)];
await creatingContextMenuItem({
id: "format-link-format" + (i + 1),
title: "as " + format,
contexts: ["all"]
});
}
} else {
const defaultFormat = options['title' + options['defaultFormat']];
await creatingContextMenuItem({
id: "format-link-format-default",
title: "Format Link as " + defaultFormat,
contexts: ["all"]
});
}
}
}