-
-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathcopyTable.user.js
More file actions
120 lines (103 loc) · 3.43 KB
/
copyTable.user.js
File metadata and controls
120 lines (103 loc) · 3.43 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
/* eslint-env browser */
// ==UserScript==
// @name []copyTable
// @description copyTable
// @include *
// @version 1.0.7
// @modified 2024-07-09 21:13:38
// @author dodying
// @namespace https://github.com/dodying/UserJs
// @supportURL https://github.com/dodying/UserJs/issues
// @icon https://github.com/dodying/UserJs/raw/master/Logo.png
// @run-at document-end
// @grant GM_setClipboard
// require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js
// ==/UserScript==
/* eslint-disable no-debugger */
(function () {
const tagName = ['HTML', 'TABLE', 'THEAD', 'TBODY', 'TR', 'TH', 'TD'];
window.addEventListener('keyup', (e) => {
if (e.key === 'c' && e.ctrlKey) {
let node = window.getSelection().anchorNode || window.getSelection().focusNode;
if (!node) return;
while (true) {
if (tagName.includes(node.tagName)) break;
node = node.parentNode;
}
if (node.tagName === 'HTML') return;
// console.log(node)
let tableNode = node;
while (true) {
if (tableNode.tagName === 'TABLE') break;
tableNode = tableNode.parentNode;
}
// console.log(tableNode)
const xs = tableNode.querySelectorAll('tr');
const table = new Array(xs.length);
const tableTxt = new Array(xs.length);
for (let x = 0; x < xs.length; x++) { // 生成二维数组
tableTxt[x] = [];
table[x] = [];
}
for (let x = 0; x < xs.length; x++) {
const ys = xs[x].querySelectorAll('th,td');
for (let y = 0; y < ys.length; y++) {
const ele = ys[y];
const txt = ele.textContent.trim();
let yT = y;
while (tableTxt[x][yT]) {
yT = yT + 1;
}
table[x][yT] = ele;
tableTxt[x][yT] = txt;
let col = ele.getAttribute('colspan') * 1;
let row = ele.getAttribute('rowspan') * 1;
if (col || row) {
col = col || 1;
row = row || 1;
for (let x1 = 0; x1 < row; x1++) {
for (let y1 = 0; y1 < col; y1++) {
table[x + x1][yT + y1] = ele;
tableTxt[x + x1][yT + y1] = txt;
}
}
}
}
}
// console.log({ table, tableTxt })
const toCopy = [];
if (['TH', 'TD'].includes(node.tagName)) {
const has = table.filter((i) => i.includes(node))[0];
const x = table.indexOf(has);
const y = has.indexOf(node);
// console.log(x, y, node, table[x][y], node === table[x][y])
toCopy.push({
name: '单元格',
txt: tableTxt[x][y],
});
toCopy.push({
name: '列',
txt: tableTxt.map((i) => i[y]).join('\n'),
});
node = node.parentNode;
}
if (['TR'].includes(node.tagName)) {
const has = table.filter((i) => i.includes(node.children[0]))[0];
const x = table.indexOf(has);
// console.log(x, node, table[x])
toCopy.push({
name: '行',
txt: tableTxt[x].join('\t'),
});
node = node.parentNode;
}
toCopy.push({
name: '表',
txt: tableTxt.map((i) => i.join('\t')).join('\n'),
});
// console.log(toCopy)
const prompt = window.prompt(toCopy.map((i, j) => `${j + 1}: ${i.name}`).join('\n'));
if (prompt) GM_setClipboard(toCopy[prompt - 1].txt);
}
});
}());