-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontent-script.js
205 lines (183 loc) · 5.27 KB
/
content-script.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* global browser */
/* eslint-disable no-useless-escape */
(function () {
if (typeof window.tbl2csv_hasRun !== "undefined") {
return;
}
window.tbl2csv_hasRun = true;
let tableStyleSheet = document.createElement("style");
document.head.appendChild(tableStyleSheet);
const highlightCSS = `.divTbl, ol, ul, table { border: 3px dotted red !important; padding:1px !important; margin:1px !important; }`;
tableStyleSheet.sheet.insertRule(highlightCSS, 0);
tableStyleSheet.disabled = true;
// export type (text,html)
let mode = "text";
const seperator = ",";
const CRLF = "\r\n";
// add empty data link
let link = document.createElement("a");
link.style.display = "none";
link.setAttribute("target", "_blank");
link.setAttribute(
"download",
encodeURIComponent(document.location.href) + ".csv",
);
document.body.append(link);
// consts
const re_quote = new RegExp('"', "gm");
const re_break = new RegExp(/(\r\n|\n|\r)/, "gm");
const re_space = new RegExp(/\s+/, "gm");
const tblrowdsps = ["table-row", "table-header-group", "table-footer-group"];
const convert = {
div: div2csv,
table: table2csv,
ul: list2csv,
ol: list2csv,
};
function getDataFromNode(node) {
let data = mode.endsWith("html") ? node.innerHTML : node.innerText;
return data
.replace(re_break, " ")
.replace(re_space, " ")
.trim()
.replace(re_quote, '""');
}
function div2csv(tbl) {
let csv = [];
tbl.querySelectorAll("div").forEach((tr) => {
if (tblrowdsps.includes(getStyle(tr, "display"))) {
let row = [];
tr.querySelectorAll("div").forEach((td) => {
if (getStyle(td, "display") === "table-cell") {
const data = getDataFromNode(td);
row.push('"' + data + '"');
}
});
if (row.length > 0) {
csv.push(row.join(seperator));
}
}
});
return csv.join(CRLF);
}
function table2csv(tbl) {
let csv = [];
tbl.querySelectorAll("tr").forEach((tr) => {
// skip rows in subtables
if (!tbl.isSameNode(tr.closest("table"))) {
return;
}
let row = [];
tr.querySelectorAll("td, th").forEach((td) => {
const data = getDataFromNode(td);
row.push('"' + data + '"');
// add colspan padding
for (let i = 1, n = td.getAttribute("colspan"); i < n; i++) {
row.push('""');
}
});
// skip rows without cells
if (row.length > 0) {
csv.push(row.join(seperator));
}
});
return csv.join(CRLF);
}
function list2csv(ul) {
let csv = [];
ul.querySelectorAll("li").forEach((li) => {
const data = getDataFromNode(li);
csv.push('"' + data + '"');
});
return csv.join(CRLF);
}
function getClosestExportableParent(node) {
while (
node !== null &&
typeof node.tagName === "string" &&
node.tagName.toLowerCase() !== "table" &&
node.tagName.toLowerCase() !== "ol" &&
node.tagName.toLowerCase() !== "ul"
) {
if (
node.tagName.toLowerCase() === "div" &&
getStyle(node, "display") === "table"
) {
break;
}
node = node.parentNode;
}
return node;
}
function simulateClick(elem) {
const evt = new MouseEvent("click", {
bubbles: false,
cancelable: false,
view: window,
});
elem.dispatchEvent(evt);
}
function highlightDivTables() {
document.querySelectorAll("div").forEach((div) => {
if (getStyle(div, "display") === "table") {
if (!hasClass(div, "divTbl")) {
addClass(div, "divTbl");
}
}
});
}
function getStyle(node, attr) {
return window.getComputedStyle(node, null)[attr];
}
function hasClass(ele, cls) {
return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}
function addClass(ele, cls) {
ele.className += " " + cls;
}
// register message listener
let doOnce = true;
browser.runtime.onMessage.addListener(async (message) => {
if (message.action === "highlight") {
if (tableStyleSheet.disabled) {
if (doOnce) {
// add classes on first click
doOnce = false;
highlightDivTables();
}
tableStyleSheet.disabled = false; // toggle stylesheet
} else {
tableStyleSheet.disabled = true; // togglestylesheet
}
}
if (message.action === "export") {
mode = message.mode;
const clickTarget = browser.menus.getTargetElement(
message.targetElementId,
);
const exportableTarget = getClosestExportableParent(clickTarget);
if (exportableTarget === null) {
alert(
"No exportable target found!\nHint: Click the toolbar icon to highlight exportable targets",
);
return;
}
const str =
convert[exportableTarget.tagName.toLowerCase()](exportableTarget);
if (mode.startsWith("export")) {
link.setAttribute(
"href",
"data:text/csv;charset=utf-8," + encodeURIComponent(str),
);
/*
link.href = window.URL.createObjectURL(new Blob([str], {
type: "text/csv"
}));
*/
simulateClick(link);
} else {
navigator.clipboard.writeText(str);
}
}
});
})();