-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
215 lines (175 loc) · 5.06 KB
/
Copy pathbackground.js
File metadata and controls
215 lines (175 loc) · 5.06 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
function htmlToElement(html) {
const template = document.createElement("template");
template.innerHTML = html.trim();
return template.content.firstChild;
}
function downloadAsCSV(tableData) {
const tagNameParser = /(<([^>]+)>)/ig;
const tableParser = /<(table([^>]*)>)(\s|.)*?<\/table>/ig;
const tableRowParser = /<(tr([^>]*)>)(\s|.)*?<\/tr>/ig;
const tableElementsParser = /<(t[dh])([^>]*)>(.*?)<\/\1>/ig;
let csvContents = "data:text/csv;charset=utf-8,";
const tables = tableData.match(tableParser);
tables.forEach(table => {
const rows = table.match(tableRowParser);
if (rows) {
rows.forEach(row => {
const elements = row.match(tableElementsParser);
if (elements) {
elements.forEach(element => {
csvContents += `${element.replace(tagNameParser, "")},`;
});
}
csvContents += "\n";
});
} else {
const elements = table.match(tableElementsParser);
if (elements) {
elements.forEach(element => {
csvContents += `${element.replace(tagNameParser, "")},`;
});
}
}
csvContents += "\n";
});
const encodedUri = encodeURI(csvContents);
whale.downloads.download({
url: encodedUri,
filename: "data.csv",
});
}
function downloadAsXLS(tablesHTML) {
const tableParser = /<(table([^>]*)>)(\s|.)*?<\/table>/ig;
const tables = tablesHTML.match(tableParser);
const dataTypeUri = "data:application/vnd.ms-excel;charset=UTF-8,";
const tmplWorkbookXML =
`<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet?">
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Editable</Author>
<Created>{created}</Created>
</DocumentProperties>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Center"/>
</Style>
</Styles>
{worksheets}
</Workbook>`;
const tmplWorksheetXML =
`<Worksheet ss:Name="{nameWS}">
<Table>{rows}</Table>
</Worksheet>`;
const tmplCellXML =
`<Cell{attr}>
<Data ss:Type="{nameType}">{data}</Data>
</Cell>`;
function format(s, c) {
return s.replace(/{(\w+)}/g, (m, p) => c[p]);
}
let ctx;
let worksheetsXML = "";
let rowsXML = "";
tables.forEach((tableHTML, idx) => {
const table = htmlToElement(tableHTML);
for (let i = 0; i < table.rows.length; ++i) {
const row = table.rows[i];
let index = 1;
rowsXML += "<Row>";
for (let j = 0; j < row.cells.length; ++j, ++index) {
const cell = row.cells[j];
let attributes = "";
if (cell.getAttribute("index")) {
attributes += ` ss:Index="${cell.getAttribute("index")}"`;
index = parseInt(cell.getAttribute("index"), 10);
}
if (cell.colSpan > 1) {
attributes += ` ss:MergeAcross="${cell.colSpan - 1}"`;
}
if (cell.rowSpan > 1) {
attributes += ` ss:MergeDown="${cell.rowSpan - 1}"`;
for (let k = 1; k < cell.rowSpan; ++k) {
const spanedRow = table.rows[i + k];
let tmp = index;
let ci;
for (ci = 0; ci < spanedRow.cells.length && tmp > 0; ++ci) {
if (spanedRow.cells[ci].getAttribute("index")) {
tmp -= parseInt(spanedRow.cells[ci].getAttribute("index"), 10);
} else {
--tmp;
}
if (tmp < 1) { break; }
}
spanedRow.cells[ci].setAttribute("index", index + cell.colSpan);
}
}
const dataValue = cell.textContent;
const dataType = !dataValue || isNaN(dataValue) ? "String" : "Number";
ctx = {
attr: attributes,
nameType: dataType,
data: dataValue,
};
rowsXML += format(tmplCellXML, ctx);
}
rowsXML += "</Row>";
}
ctx = {
rows: rowsXML,
nameWS: `Sheet${idx}`,
};
worksheetsXML += format(tmplWorksheetXML, ctx);
rowsXML = "";
});
ctx = {
created: (new Date())
.toISOString()
.replace(/(\.)/, ":")
.replace(/(\dZ)/, "Z"),
worksheets: worksheetsXML,
};
const workbookXML = format(tmplWorkbookXML, ctx);
const encodedUri = encodeURI(dataTypeUri + workbookXML);
whale.downloads.download({
url: encodedUri,
filename: "data.xls",
});
}
function edit() {
const editorURL = whale.runtime.getURL("./editor/index.html");
whale.tabs.create({url: editorURL});
}
const RESULTHANDLING = {
"edit": edit,
"download-csv": downloadAsCSV,
"download-xls": downloadAsXLS,
};
whale.commands.onCommand.addListener(command => {
whale.tabs.query({active: true}, tabs => {
whale.tabs.sendMessage(tabs[0].id, {
msg: "startCapture",
type: command,
}, success => {
if (success) {
whale.browserAction.setBadgeText({text: "capture"});
}
});
});
});
whale.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.msg === "resultHandling") {
const handlingMethod = RESULTHANDLING[message.type];
const success = typeof handlingMethod === "function";
sendResponse(success);
if (success) {
whale.storage.local.get("tableData", items => {
if (items.tableData) {
handlingMethod(items.tableData);
}
});
}
} else if (message.msg === "endCapture") {
whale.browserAction.setBadgeText({text: ""});
}
});