-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki-table.js
84 lines (73 loc) · 3.02 KB
/
wiki-table.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
javascript: (function() {
let download = function(content, fileName, mimeType) {
let a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';
if (navigator.msSaveBlob) {
navigator.msSaveBlob(new Blob([content], {
type: mimeType
}), fileName);
} else if (URL && 'download' in a) {
a.href = URL.createObjectURL(new Blob([content], {
type: mimeType
}));
a.setAttribute('download', fileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
location.href = 'data:application/octet-stream,' + encodeURIComponent(content);
}
};
let sanitise_filename = function(name) {
let sanitised_name = name.toLowerCase().replace(/[\/\?<>\\:\*\|":]/g, '').replaceAll(' ', '_');
return sanitised_name.substring(0, 251) + '.csv';
};
let clean_text = function(t) {
return t.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/\s{2,}/g," ");
};
let css_selector_table = "table[class~='wikitable']";
let tables = document.querySelectorAll(css_selector_table);
for (let i = 0; i < tables.length; i++) {
let csv_content = [];
let table = tables[i];
if (table.style.fontSize === '90%') {
continue;
}
let csv_filename = '';
let caption = table.querySelector("caption");
if (caption !== null) {
csv_filename = clean_text(caption.innerText)
} else {
let cur_prev_sibling = table.previousElementSibling;
while (cur_prev_sibling.nodeName[0] != "H") {
cur_prev_sibling = cur_prev_sibling.previousElementSibling;
}
let headline = cur_prev_sibling.querySelector("span[class='mw-headline']");
csv_filename = clean_text(headline.innerText);
}
let headers = table.querySelectorAll('thead > tr > th');
if (headers.length > 0) {
headers = Array.from(headers).map(h => h.innerText).join(',');
csv_content.push(headers);
}
let table_rows = table.querySelectorAll('tbody > tr');
if (table_rows !== null) {
for (let j = 0; j < table_rows.length; j++) {
let row = table_rows[j].querySelectorAll('th,td');
let content = Array.from(row).map(h => {
let text = h.innerText.replaceAll('"', '\'');
let text_array = text.split("\n");
if (text_array.length > 1) {
text = text_array.join("|");
}
return '"' + text + '"';
});
csv_content.push(content.join(','));
}
}
if (csv_content.length > 0) {
csv_content = csv_content.join("\r\n");
download(csv_content, sanitise_filename(csv_filename), 'text/csv;encoding:utf-8');
}
}
})();