-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwScriptHelper.user.js
182 lines (158 loc) · 5.24 KB
/
gwScriptHelper.user.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
// ==UserScript==
// @name GlyphWiki script helper
// @version 2023.01.01
// @namespace szc
// @description -
// @match *://glyphwiki.org/wiki/*
// @match *://*.glyphwiki.org/wiki/*
// @grant none
// @inject-into content
// ==/UserScript==
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];
}
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;
}
}