forked from dafizilla/firefox-table2clipboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
185 lines (168 loc) · 5.27 KB
/
background.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
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
let parentId = browser.menus.create({
id: "table-2-clipboard",
title: "Table2Clipboard",
contexts: ["all"],
});
let copySelectedCellsId = browser.menus.create({
id: "copy-selected-cells",
title: browser.i18n.getMessage("copySelectedCells") + ' Alt+C',
contexts: ["all"],
parentId,
onclick: copySelectedCells
});
let copyWholeTableId = browser.menus.create({
id: "copy-whole-table",
title: browser.i18n.getMessage("copyWholeTable"),
contexts: ["all"],
parentId,
onclick: copyWholeTable
});
let separatorId = browser.menus.create({
type: "separator",
id: "separator",
contexts: ["all"],
parentId,
});
let selectTableRowId = browser.menus.create({
id: "select-row",
title: browser.i18n.getMessage("selectRow"),
contexts: ["all"],
parentId,
onclick: selectTableRow
});
let selectTableColumnId = browser.menus.create({
id: "select-column",
title: browser.i18n.getMessage("selectColumn"),
contexts: ["all"],
parentId,
onclick: selectTableColumn
});
let selectTableId = browser.menus.create({
id: "select-table",
title: browser.i18n.getMessage("selectTable"),
contexts: ["all"],
parentId,
onclick: selectTable
});
function loadScripts() {
return browser.tabs.executeScript({
code: "typeof gTable2Clip === 'object';",
}).then((results) => {
// 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
// t2cOverlay.js to define function gTable2Clip.
if (!results || results[0] !== true) {
return browser.tabs.executeScript({
file: "src/main/content/t2c/prefs.js"
}).then(() => {
return browser.tabs.executeScript({
file: "src/main/content/t2c/common.js"
});
}).then(() => {
return browser.tabs.executeScript({
file: "src/main/content/t2c/tableInfo.js"
});
}).then(() => {
return browser.tabs.executeScript({
file: "src/main/content/t2c/htmlFilter.js"
});
}).then(() => {
return browser.tabs.executeScript({
file: "src/main/content/t2c/htmlBuilder.js"
});
}).then(() => {
return browser.tabs.executeScript({
file: "src/main/content/t2c/cssUtils.js"
});
}).then(() => {
return browser.tabs.executeScript({
file: "src/main/content/t2c/formatters.js"
});
}).then(() => {
return browser.tabs.executeScript({
file: "src/main/content/t2c/t2cOverlay.js"
});
}).then(() => {
return executeScript("gTable2Clip.onLoad();");
});
}
});
}
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'loading') {
loadScripts();
}
});
browser.tabs.onActivated.addListener((activeInfo) => {
loadScripts();
});
browser.menus.onShown.addListener((info, tab) => {
let isOnTable, hasSelectedCells, isOnCell;
executeScript("gTable2Clip.isOnTable();")
.then((results) => {
isOnTable = results && results[0];
}).then(() => {
return executeScript("gTable2Clip.hasSelectedCells();")
.then((results) => {
hasSelectedCells = results && results[0];
});
}).then(() => {
return executeScript("gTable2Clip.isOnCell();")
.then((results) => {
isOnCell = results && results[0];
});
}).then(() => {
let shouldShow = hasSelectedCells || isOnTable;
Promise.all([
browser.menus.update(parentId, {
enabled: shouldShow
}),
browser.menus.update(copySelectedCellsId, {
enabled: hasSelectedCells
}),
browser.menus.update(selectTableId, {
enabled: isOnTable
}),
browser.menus.update(copyWholeTableId, {
enabled: isOnTable
}),
browser.menus.update(selectTableRowId, {
enabled: isOnTable
}),
browser.menus.update(selectTableColumnId, {
enabled: isOnTable && isOnCell
}),
]).then(() => {
browser.menus.refresh();
});
});
});
browser.commands.onCommand.addListener((name) => {
if (name === 'copy-selected-cells') {
copySelectedCells();
}
});
function copySelectedCells() {
executeScript("gTable2Clip.copySelectedCells();");
}
function copyWholeTable() {
executeScript("gTable2Clip.copyWholeTable();");
}
function selectTableRow() {
executeScript("gTable2Clip.selectTableRow();");
}
function selectTableColumn() {
executeScript("gTable2Clip.selectTableColumn();");
}
function selectTable() {
executeScript("gTable2Clip.selectTable();");
}
function executeScript(code) {
if (code) {
return browser.tabs.executeScript({
code
}).catch((error) => {
console.error("Failed to executeScript: " + code + " : " + error);
});
}
}