-
Notifications
You must be signed in to change notification settings - Fork 1
/
content-script.js
157 lines (129 loc) · 4.19 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
function copyTextToClipboard(text) {
//Create a textbox field where we can insert text to.
const copyFrom = document.createElement("textarea");
//Set the text content to be the text you wished to copy.
copyFrom.textContent = text;
//Append the textbox field into the body as a child.
//"execCommand()" only works when there exists selected text, and the text is inside
//document.body (meaning the text is part of a valid rendered HTML element).
document.body.appendChild(copyFrom);
//Select all the text!
copyFrom.select();
//Execute command
document.execCommand("copy");
//(Optional) De-select the text using blur().
copyFrom.blur();
//Remove the textbox field from the document.body, so no other JavaScript nor
//other elements can get access to this.
document.body.removeChild(copyFrom);
}
function getDataFromTableRow(row) {
const arr = [];
const length = row.length;
// First column is Row data and Last column is empty data, so ignore those data.
for (let col = 1; col < length - 1; col++) {
const column = row[col];
arr.push(column.textContent.trim());
}
return arr;
}
function getQueryResultsFromTable() {
const bqResultTable = document.getElementsByTagName("bq-nestable-table")[0];
const table = bqResultTable.getElementsByTagName("table")[0];
const thead = table.getElementsByTagName("thead")[0];
const headers = getDataFromTableRow(thead.getElementsByTagName("th"));
const tbody = table.getElementsByTagName("tbody")[0];
const bodyRows = tbody.getElementsByTagName("tr");
const body = [];
for (let row = 0; row < bodyRows.length; row++) {
body.push(getDataFromTableRow(bodyRows[row].getElementsByTagName("td")));
}
return {
headers,
body,
};
}
function getSQLFromCodeMirror() {
let str = "";
const lines = document.getElementsByClassName("view-line");
if (!lines || lines.length === 0) return null;
Array.from(Array(lines.length), function (_, index) {
const line = lines[index];
// To format lines.
const isLast = index === lines.length - 1;
const isEmptyLine = !line.textContent.trim();
if (isLast && isEmptyLine) return;
str += line.textContent;
str += "\n";
});
return (
"<details>\n<summary>Query</summary>\n\n```sql\n" +
str +
"```\n\n</details>"
);
}
function joinTableLineData(lineData) {
if (lineData.length === 0) return null;
return lineData.join(" | ") + " | ";
}
function convertDataToMarkdownTableText(tableData) {
const convertLine = function (lineData) {
return joinTableLineData(lineData);
};
const createEmptyLine = function (columnSize) {
const emptyLineData = Array.from({ length: columnSize }, (_, i) => "--");
return joinTableLineData(emptyLineData);
};
return [
convertLine(tableData.headers),
createEmptyLine(tableData.headers.length),
...tableData.body.map(convertLine),
]
.filter(Boolean)
.join("\n");
}
function copy(callback = null) {
try {
const tableData = getQueryResultsFromTable();
const tableText = convertDataToMarkdownTableText(tableData);
const codeText = getSQLFromCodeMirror();
const text = [tableText, codeText].filter(Boolean).join("\n\n");
copyTextToClipboard(text);
typeof callback === "function" && callback({ type: "COPIED", text });
} catch (error) {
typeof callback === "function" && callback({ type: "ERROR", error });
}
}
const copyText = document.createTextNode("Copy");
const copyButton = document.createElement("button");
copyButton.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
background-color: white;
border-radius: 24px;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.02), 0 0 0 1px rgba(0, 0, 0, 0.1);
height: 36px;
line-height: 36px;
padding: 0 12px;
font-size: 14px;
text-align: center;
color: rgba(0, 0, 0, 0.56);
outline: none;
min-width: 60px;
border-style: none;
z-index: 1;
`;
copyButton.addEventListener("click", () => {
copy((response) => {
console.log(response);
if (response.type === "COPIED") {
copyButton.textContent = "Copied!";
setTimeout(() => {
copyButton.textContent = "Copy";
}, 2000);
}
});
});
copyButton.appendChild(copyText);
document.body.appendChild(copyButton);