-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.js
193 lines (171 loc) · 6.2 KB
/
element.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
window.userscript_util = window.userscript_util || {};
window.userscript_util.element = {};
window.userscript_util.element.pseudoBodyButtonWith = (msg) => {
const button = `
body:before {
background-color: rgba(163, 60, 214, 0.6);
border-radius: 0 0 5px 0;
color: #fff;
content: '${msg}';
font-size: 20px;
font-weight: bold;
left: 0;
line-height: 12px;
padding: 15px 10px;
position: fixed;
text-align: center;
top: 3em;
left: 3em;
z-index: 1000001;
pointer-events: auto;
cursor: pointer;
}
`;
return button;
};
window.userscript_util.element.ClearDiv = "<div style='clear: both;'> </div>";
window.userscript_util.element.CopyDiv = `
<div>
<button id='copy-button' data-clipboard-target='#article_link_list' style='margin: 5px;
color: #fff;
background-color: #5cb85c;
border-color: #4cae4c;
display: inline-block;
padding: 6px 12px;
margin-bottom: 2em;
font-size: 14px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
-webkit-appearance: button;
overflow: visible;'>Copy</button>
</div>
`;
window.userscript_util.element.NthCopyDiv = (index) => {
return `
<div>
<button id='copy-button${index}' data-clipboard-target='#article_link_list${index}' style='margin: 5px;
color: #fff;
background-color: #5cb85c;
border-color: #4cae4c;
display: inline-block;
padding: 6px 12px;
margin-bottom: 2em;
font-size: 14px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
-webkit-appearance: button;
overflow: visible;'>Copy</button>
</div>
`;
};
window.userscript_util.element.ListDiv = `
<div id='article_link_list' style='
margin-bottom: 2em;
padding: 15px;
background-color: #d9edf7;
font-size: 14px;
line-height: 1.42857143;
color: #333;
box-sizing: border-box;'>
<ul></ul>
</div>`;
window.userscript_util.element.NthListDiv = (index) => {
return `
<div id='article_link_list${index}' style='
margin-bottom: 2em;
padding: 15px;
background-color: #d9edf7;
font-size: 14px;
line-height: 1.42857143;
color: #333;
box-sizing: border-box;'>
<ul></ul>
</div>`;
};
window.userscript_util.element.attachLinkAreaTo = (sel, index, insertAfterEnd) => {
const div = document.createElement("div");
if (index === "" || index === undefined || index === null) {
div.innerHTML = userscript_util.element.ClearDiv + userscript_util.element.CopyDiv + userscript_util.element.ListDiv;
} else {
div.innerHTML = userscript_util.element.ClearDiv + userscript_util.element.NthCopyDiv(index) + userscript_util.element.NthListDiv(index);
}
if (insertAfterEnd) {
document.querySelector(sel).insertAdjacentElement("afterend", div);
} else {
document.querySelector(sel).insertAdjacentElement("beforeend", div);
}
};
window.userscript_util.element.attachLinkAddress = (linkAnchorEl, index, useLiInsteadOfBr) => {
if (useLiInsteadOfBr) {
const li = document.createElement("li");
li.append(linkAnchorEl);
document.querySelector(`#article_link_list${index} ul`).append(li);
} else {
document.querySelector(`#article_link_list${index}`).append(linkAnchorEl);
document.querySelector(`#article_link_list${index}`).append(document.createElement("br"));
}
};
window.userscript_util.element.attachLinkAddressExtractedFrom = (fromSel,
toSel,
lineBreakPredicate,
skipPredicate,
urlBuilder,
indexPredicate,
skipBlank,
useLiInsteadOfBr,
insertAfterEnd) => {
if (!indexPredicate) {
indexPredicate = () => "";
}
const indexSet = new Set();
let lastIndex = "";
document.querySelectorAll(fromSel).forEach((e, i) => {
//여러구역일때, 이전 구역이 끝났으면
if (lineBreakPredicate && lineBreakPredicate(i) && i !== 0) {
if (!skipBlank) {
userscript_util.element.attachLinkAddress("about:blank", lastIndex, useLiInsteadOfBr);
}
userscript_util.clipboard.bindClipboardAction(null, lastIndex);
}
if (!indexSet.has(indexPredicate(i))) {
userscript_util.element.attachLinkAreaTo(toSel, indexPredicate(i), useLiInsteadOfBr, insertAfterEnd);
indexSet.add(indexPredicate(i));
}
if (skipPredicate && skipPredicate(e, i)) {
//do nothing;
} else {
let url;
if (urlBuilder) {
url = urlBuilder(e, i);
} else {
url = e.href;
}
userscript_util.element.attachLinkAddress(url, indexPredicate(i), useLiInsteadOfBr);
}
lastIndex = indexPredicate(i);
});
//통채로 한구역일때 혹은
//여러 구역일때 마지막 구역은 처리가 안되서 한번 더 처리.
if (!skipBlank) {
userscript_util.element.attachLinkAddress("about:blank", lastIndex, useLiInsteadOfBr);
}
userscript_util.clipboard.bindClipboardAction(null, lastIndex);
};