-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
151 lines (135 loc) · 4.95 KB
/
content.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
// @ts-check
/** @type {MutationObserver[]} */
const observers = [];
/** @type {Options} */
let options;
/**
*
* @param {HTMLElement} $item
* @param {[ModeType, Work | Tag][]} wts
*/
function applyStyles($item, wts) {
let border = "";
for (const [modeType, wt] of wts) {
const borderProp = joinTagMode(modeType, "border");
if (wt && wt.modes[borderProp]) {
const borderColorProp = joinTagColorProperty(modeType, "borderColor");
border = `2px solid ${wt[borderColorProp]}`;
break;
}
}
if ($item.style.border !== border) $item.style.border = border;
let background = "";
for (const [modeType, wt] of wts) {
const backgroundProp = joinTagMode(modeType, "background");
if (wt && wt.modes[backgroundProp]) {
const backgroundColorProp = joinTagColorProperty(modeType, "backgroundColor");
background = wt[backgroundColorProp];
break;
}
}
if ($item.style.background !== background) $item.style.background = background;
let opacity;
for (const [modeType, wt] of wts) {
const opacityProp = joinTagMode(modeType, "opacity");
if (wt && wt.modes[opacityProp]) {
opacity = "0.4";
break;
}
}
if ($item.style.opacity !== opacity) $item.style.opacity = opacity;
let displayNone;
for (const [modeType, wt] of wts) {
const hideProp = joinTagMode(modeType, "hide");
if (wt && wt.modes[hideProp]) {
displayNone = true;
break;
}
}
if (displayNone) {
if ($item.style.display !== "none") {
$item.dataset.dlsiteMakerFilterDisplay = $item.style.display;
$item.style.display = "none";
}
} else if ($item.style.display === "none") {
$item.style.display = $item.dataset.dlsiteMakerFilterDisplay;
}
}
function filterItems() {
const $items = /** @type {NodeListOf<HTMLElement>} */(document.querySelectorAll(".n_worklist_item, .search_result_img_box_inner, .n_worklist tr, .push_list > li"));
for (const $item of $items) {
const $makerName = $item.querySelector(".maker_name a");
const $searchTags = /** @type {NodeListOf<HTMLElement>} */($item.querySelectorAll(".search_tag a"));
let work;
let noWorkTag;
let workTag;
if ($makerName) {
const makerName = $makerName.textContent;
work = options.works.find(work => work.makerNamesMap[makerName]);
}
if ($searchTags) {
const tagNames = [];
for (const $searchTag of $searchTags) {
const tagName = $searchTag.textContent.trim();
tagNames.push(tagName);
const tag = options.tags.find(tag => tag.tagsMap[tagName] && tag.modeTypes[""]);
applyStyles($searchTag, [["", tag]]);
}
noWorkTag = options.tags.find(tag => tag.modeTypes["noWork"] && tagNames.every(tagName => !tag.tagsMap[tagName]));
workTag = options.tags.find(tag => tag.modeTypes["work"] && tagNames.some(tagName => tag.tagsMap[tagName]));
}
applyStyles($item, [["", work], ["work", workTag], ["noWork", noWorkTag]]);
}
}
async function loadSettings() {
options = await loadOptions();
options.works.forEach(work => {
work.makerNamesMap = {};
for (const makerName of work.makerNames.split("\n").filter(Boolean)) work.makerNamesMap[makerName] = true;
});
options.tags.forEach(tag => {
tag.tagsMap = {};
for (const t of tag.tags.split("\n").filter(Boolean)) tag.tagsMap[t] = true;
tag.modeTypes = {
"": modes.some(mode => tag.modes[mode]),
"work": tagWorkModes.some(mode => tag.modes[mode]),
"noWork": tagNoWorkModes.some(mode => tag.modes[mode]),
};
});
}
/**
*
* @param {number} ms
*/
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/** @type {MutationObserverInit} */
const mutationObserverInit = {
childList: true,
subtree: true,
};
async function main() {
await loadSettings();
/** @type {HTMLDivElement} */
let element;
let index = 0;
while (index < 60) {
await wait(1000);
element = /** @type {HTMLDivElement} */(document.querySelector("#new_worklist, #search_result_list, #__workbox, #ana_work_wrapper"));
if (element) break;
index++;
}
if (!element) return; // elementが1分経っても見つからなければexit
const observer = new MutationObserver(filterItems);
observer.observe(element, mutationObserverInit);
observers.push(observer);
const elements = /** @type {NodeListOf<HTMLDivElement>} */(document.querySelectorAll(".work_push"));
for (const element of elements) {
const observer = new MutationObserver(filterItems);
observer.observe(element, mutationObserverInit);
observers.push(observer);
}
filterItems();
}
main();