-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixiv__share-helper.js
198 lines (178 loc) · 6.17 KB
/
pixiv__share-helper.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
// ==UserScript==
// @name pixiv share helper
// @name:zh-TW pixiv 分享助手
// @namespace https://github.com/zica87/self-made-userscipts
// @version 2.1.1
// @description Convert sharing link to text with format: title | creator link
// @description:zh-TW 將分享連結轉換為文字,格式:標題 | 作者 連結
// @author zica
// @match https://www.pixiv.net/artworks/*
// @match https://www.pixiv.net/en/artworks/*
// @match https://www.pixiv.net/novel/*
// @match https://www.pixiv.net/users/*
// @match https://www.pixiv.net/en/users/*
// @grant none
// @license GPL-3.0
// ==/UserScript==
(function () {
"use strict";
const container = generate_container();
const [result, set_result] = generate_result();
const input_wrapper = generate_input_wrapper();
const paste_button = generate_paste_button();
const box = generate_box();
const prompt_no_permission = set_result.bind(
undefined,
"no clipboard permission",
"pink"
);
paste_button.onclick = () => {
navigator.clipboard.readText().then(
(text) => {
if (text.length === 0) {
set_result("not text", "yellow");
} else {
copyText(process(text));
}
},
() => {
prompt_no_permission();
}
);
};
box.oninput = (e) => {
copyText(process(e.target.value));
box.value = "";
};
function copyText(text) {
navigator.clipboard.writeText(text).then(
() => {
set_result("text copied", "lightgreen");
},
() => {
prompt_no_permission();
}
);
}
container.append(result);
input_wrapper.append(paste_button);
input_wrapper.append(box);
container.append(input_wrapper);
document.body.prepend(container);
let copyButton;
let previousURL;
const observer = new MutationObserver((_, observerInstance) => {
const shareOptions = document.getElementsByClassName("sc-1o8nozx-2");
const shareToPawoo = shareOptions[shareOptions.length - 1];
const rawURL = shareToPawoo?.firstElementChild?.href;
if (!rawURL?.startsWith("https://pawoo.net/share?text=")) {
return;
}
if (copyButton === undefined) {
copyButton = document.createElement("li");
copyButton.classList = shareToPawoo.classList;
copyButton.style.cursor = "pointer";
const copyButtonText = document.createElement("span");
copyButtonText.textContent = "copy text";
copyButtonText.style.height = "24px";
shareToPawoo.firstElementChild.classList.forEach((currentClass) => {
// actually I don't know what it is
if (!currentClass.startsWith("gtm")) {
copyButtonText.classList.add(currentClass);
}
});
copyButton.append(copyButtonText);
}
if (rawURL !== previousURL) {
previousURL = rawURL;
copyButton.onclick = copyText.bind(undefined, process(rawURL));
}
shareToPawoo.after(copyButton);
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
function process(originalUrl) {
const decoded = decodeURIComponent(originalUrl);
// https://pawoo.net/share?text=お姉さん | 朱雷@Fantia更新中 #pixiv https://www.pixiv.net/artworks/108688782
return decoded.substring(decoded.search("=") + 1).replace("#pixiv", "");
// お姉さん | 朱雷@Fantia更新中 https://www.pixiv.net/artworks/108688782
}
function generate_container() {
const container = document.createElement("div");
Object.assign(container.style, {
position: "fixed",
bottom: "5px",
right: "5px",
zIndex: 1,
display: "flex",
flexDirection: "column",
alignItems: "flex-end",
});
return container;
}
function generate_result() {
const result_wrapper = document.createElement("div");
Object.assign(result_wrapper.style, {
marginBottom: "60px",
display: "flex",
justifyContent: "end",
});
const result = document.createElement("p");
Object.assign(result.style, {
color: "black",
padding: "1em",
textAlign: "end",
width: "max-content",
});
let timeout_id;
function set_result(text, backgroundColor) {
Object.assign(result.style, {
backgroundColor,
});
result.textContent = text;
clearTimeout(timeout_id);
result.style.display = "block";
timeout_id = setTimeout(() => {
result.style.display = "none";
}, 5000);
}
result_wrapper.append(result);
return [result_wrapper, set_result];
}
function generate_input_wrapper() {
const input_wrapper = document.createElement("div");
function hide() {
input_wrapper.style.opacity = 0;
}
function show() {
input_wrapper.style.opacity = 1;
}
hide();
input_wrapper.addEventListener("mouseover", show);
input_wrapper.addEventListener("dragover", show);
input_wrapper.addEventListener("mouseleave", hide);
input_wrapper.addEventListener("dragleave", hide);
return input_wrapper;
}
function generate_paste_button() {
const paste_button = document.createElement("input");
Object.assign(paste_button, {
type: "button",
value: "paste",
});
Object.assign(paste_button.style, {
marginRight: "0.5em",
});
return paste_button;
}
function generate_box() {
const box = document.createElement("input");
box.type = "text";
Object.assign(box.style, {
width: "5em",
});
return box;
}
})();