-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgwScriptHelper.user.js
More file actions
262 lines (229 loc) · 7.57 KB
/
Copy pathgwScriptHelper.user.js
File metadata and controls
262 lines (229 loc) · 7.57 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// ==UserScript==
// @namespace szc
// @name GlyphWiki: base script
// @version 2026.02.03
// @author sz
// @description -
// @icon https://glyphwiki.org/glyph/u5b57.50px.png
// @match *://glyphwiki.org/wiki/*
// @match *://*.glyphwiki.org/wiki/*
// @grant unsafeWindow
// @inject-into content
// ==/UserScript==
// Install functions
unsafeWindow.SH = {};
unsafeWindow.SH.analyzeName = function(name) {
let data = {};
name = name.match(/^(?:(.+):)?(?:(.+)_)?(.+?)(?:@(\d+))?$/);
data["ns"] = (name[1] ? name[1].toLowerCase() : "glyph");
data["user"] = name[2];
data["name"] = name[3];
data["revision"] = name[4];
return data;
}
// https://glyphwiki.org/wiki/Group:CDP外字-ALL
var UNI_PUA_START = '\ue000';
var UNI_PUA_END = '\uf8ff';
var EUDC_TO_PUA = 0xEE1B;
unsafeWindow.SH.hexToUnicode = function(hex) {
if (hex.startsWith('cdp-')) {
// https://kanji-database.sourceforge.net/ids/ids.html
// XXX: `(YY>129)` seems to be a typo? is it? idk what's going on
let xx = parseInt(hex.slice(4, 6), 16);
let yy = parseInt(hex.slice(6, 8), 16);
return String.fromCodePoint(
((xx - 128) * 157) + ((yy<129)?(yy-64):(yy-98)) + EUDC_TO_PUA
);
}
return String.fromCodePoint(hex.replace('u', '0x'));
}
unsafeWindow.SH.unicodeToHex = function(char) {
if (char >= UNI_PUA_START && char <= UNI_PUA_END) {
// https://glyphwiki.org/wiki/Group:CDP外字
let a = char.codePointAt(0) - EUDC_TO_PUA;
let xx = Math.floor(a / 157) + 128;
let yy = (a % 157);
yy += (yy < 62) ? 64 : 98;
return 'cdp-' + xx.toString(16).padStart(2, 0) + yy.toString(16).padStart(2, 0);
}
return 'u' + char.codePointAt(0).toString(16).padStart(4, 0);
}
unsafeWindow.SH.nameToUnicode = function(name) {
return name
.replace(/-?(u[0-9a-f]{4,})/g, function(_, hex) {
return unsafeWindow.SH.hexToUnicode(hex);
})
.replace(/-?(cdp-[0-9a-f]{4,})/g, function(_, hex) {
return unsafeWindow.SH.hexToUnicode(hex);
})
;
}
unsafeWindow.SH.unicodeToName = function(name) {
if (name.codePointAt(0) < 128) {
// do not codepoint-ize ASCII names like `sandbox`
return;
}
return name
.replace(/(?<![@-].*)([^@-])/gu, function(_, char) {
return '-' + unsafeWindow.SH.unicodeToHex(char);
})
.replace(/^-/, '')
;
}
unsafeWindow.SH.removeNameSuffix = function(name) {
if (name.match(/^(cdp-|u[0-9a-f]{4,})/)) {
return name.replace(/(-(var|itaiji)-\d{3}|-([a-z]{1,2}|)(\d{2}|))+$/g, '');
}
return name;
}
// Modify the DOM
gwData = {};
let temp;
// #firstHeading, #siteSub
let h1 = document.getElementsByTagName("h1")[0];
let sub = h1.nextElementSibling;
h1.id = "firstHeading";
sub.id = "siteSub";
// Extract data from query string
temp = new URLSearchParams(window.location.search);
for (let pair of temp) {
gwData[pair[0]] = pair[1];
}
// XXX: it seems like the Preview button never sends us to `?action=preview`
gwData["action"] = document.querySelector('.warning2.notice') ? "preview" : gwData["action"];
gwData["action"] ??= "view";
// Extract data from page elements
gwData["lang"] = document.documentElement.lang;
gwData = Object.assign(gwData, unsafeWindow.SH.analyzeName(decodeURIComponent(window.location.pathname.match(/[^/]+$/)[0])));
temp = [document.querySelector("span.related"), document.querySelector("input[name=related]")];
temp = [temp[0] && temp[0].innerText, temp[1] && temp[1].value];
gwData["related"] = temp[0] || temp[1];
gwData["protected"] = !!document.querySelector("#firstHeading ~ .message + hr");
// ns-specific
if (gwData.ns == "glyph") {
if (gwData.action == "view") {
// #aliasSub
let aliasSub = sub.nextElementSibling.nextElementSibling; // #siteSub + br + *
if (aliasSub && aliasSub.tagName == "DIV" && aliasSub.classList.contains("texts")) {
aliasSub.id = "hAliasSub";
temp = aliasSub.querySelector("a");
gwData["aliasTarget"] = temp && temp.innerText;
}
temp = {};
// #glyphCaption, #revision
if (h1.children.length == 2) {
temp.glyphCaption = h1.children[0];
temp.revision = h1.children[1];
} else if (h1.children.length == 1) {
if (h1.children[0].innerText.startsWith('(@')) {
// example: existing glyph, and the GW software does not provide a caption
temp.revision = h1.children[0];
} else {
// example: uncreated glyph, and the GW software provides a caption
temp.glyphCaption = h1.children[0];
}
}
for (let k in temp) {
temp[k].id = k; // add ID
gwData[k] = temp[k].innerText.substring(1, temp[k].innerText.length - 1); // add to gwData
}
// Add IDs to h2s
let h2All = document.getElementsByTagName("h2");
for (let i = 0; i < h2All.length; i++) {
let next = h2All[i].nextElementSibling.firstChild; // <h2> + div.texts > *:first-child
switch (next.tagName) {
case "UL":
h2All[i].id = "hMojiCodeKanrenJoho";
break;
case "P":
if (next.nextElementSibling == null) {
h2All[i].id = "hAliasItiran";
} else {
h2All[i].id = "hMetaJoho";
}
break;
case "TABLE":
h2All[i].id = "hKanrenGlyph";
break;
}
}
// Add classes to images for this glyph
let thisGlyphImagesA = document.querySelectorAll('#firstHeading ~ a[href^="/glyph/"]');
for (let i = 0; i < thisGlyphImagesA.length; i++) {
thisGlyphImagesA[i].classList.add("glyphMain");
}
}
}
// Write data to <body>
let gwDataDoNotWriteToDataAttribute = {
lang: true,
}
for (let k in gwData) {
let v = gwData[k];
if (v == null) {
continue;
}
if (gwDataDoNotWriteToDataAttribute[k]) {
continue;
}
v = v.toString(); // for booleans
document.body.dataset[k] = v;
}
// Print gwData to console for debugging
console.log(gwData);
// Add data attributes to thumbnails
let glyphImages = document.querySelectorAll(".glyph, .thumb, .thumb100");
for (let i = 0; i < glyphImages.length; i++) {
temp = glyphImages[i].src.match(/\.(\d+)px\./);
let pxSize = glyphImages[i].height || glyphImages[i].width || (temp && temp[1]) || 200;
glyphImages[i].height = pxSize;
glyphImages[i].width = pxSize;
let name = glyphImages[i].src.match(/glyph\/([^@.]+)/);
let revision = glyphImages[i].src.match(/\@(\d+)/);
glyphImages[i].dataset["name"] = (name && name[1]);
glyphImages[i].dataset["revision"] = (revision && revision[1]);
}
// Add data attributes to links
let links = document.querySelectorAll('.right_body a[href*="/wiki/"]:not([data-name]):not([href*="?"])');
for (let i = 0; i < links.length; i++) {
let data = unsafeWindow.SH.analyzeName(links[i].href.match(/wiki\/([^@.]+)/)[1]);
for (let k in data) {
let v = data[k];
if (v == null) {
continue;
}
v = v.toString(); // for booleans
links[i].dataset[k] = v;
}
}
// Add IDs to tabs
temp = {
caEdit: document.querySelector('.tab a[href$="edit"]'),
caHistory: document.querySelector('.tab a[href$="history"]'),
caTalk: document.querySelector('.tab a[href^="/wiki/Talk:"]'),
caMain: document.querySelector('.tab:nth-of-type(1) a'),
}
for (let k in temp) {
if (temp[k] == null) {
continue;
}
temp[k].id = k;
}
// Add IDs to elements on the edit page
if (gwData.action == "edit" || gwData.action == "preview") {
temp = {
edGlyphEditor: document.querySelector(".edit button[onclick]"),
edRelated: document.querySelector("input[type=text][name=related]"),
edTextbox: document.querySelector("textarea[name=textbox]"),
edTextboxMetaJoho: document.querySelector("textarea[name=textbox2]"), /* existing ID: #metatext */
edSummary: document.querySelector(".toolbox input[name=summary]"),
edPreview: document.querySelector(".toolbox input[type=submit]:nth-of-type(2)"),
edSubmit: document.querySelector(".toolbox input[type=submit]:nth-of-type(3)"),
}
for (let k in temp) {
if (temp[k] == null) {
continue;
}
temp[k].id = k;
}
}