-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipcHandlers.js
167 lines (161 loc) · 5.23 KB
/
ipcHandlers.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
// ipcHandlers.js
const { ipcMain } = require("electron");
const path = require("path");
const fs = require("fs");
const fetch = require("node-fetch");
const { JSDOM } = require("jsdom");
function registerIpcHandlers() {
ipcMain.handle("get-tags", async () => {
try {
const filePath = path.join(__dirname, "tags.txt");
const data = fs.readFileSync(filePath, "utf8");
const lines = data.split("\n").filter((line) => line.trim() !== "");
return lines.map((line) => {
const [tag, number] = line.split(",");
return { tag: tag.trim(), id: number.trim() };
});
} catch (err) {
console.error("Error reading tags.txt:", err);
return { error: "Unable to read tags.txt" };
}
});
ipcMain.handle("fetch-wiki-page", async (event, tag) => {
const url =
"https://danbooru.donmai.us/wiki_pages/" + encodeURIComponent(tag);
try {
const response = await fetch(url);
return await response.text();
} catch (err) {
console.error("Error fetching wiki page for tag:", tag, err);
throw err;
}
});
ipcMain.handle("fetch-wiki-example", async (event, tag) => {
const url =
"https://danbooru.donmai.us/wiki_pages/" + encodeURIComponent(tag);
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const htmlText = await response.text();
const dom = new JSDOM(htmlText);
const document = dom.window.document;
const wikiBody = document.getElementById("wiki-page-body");
if (!wikiBody) {
console.error('The div with id "wiki-page-body" was not found.');
return null;
}
let exampleH4 = null;
const h4Elements = wikiBody.getElementsByTagName("h4");
for (const h4 of h4Elements) {
if (h4.textContent.trim() === "Example") {
exampleH4 = h4;
break;
}
}
if (!exampleH4) {
console.error('No <h4> with text "Example" was found.');
return null;
}
const walker = document.createTreeWalker(
wikiBody,
dom.window.NodeFilter.SHOW_ELEMENT,
null,
false
);
walker.currentNode = exampleH4;
let imgElement = null;
while (walker.nextNode()) {
const currentNode = walker.currentNode;
if (
currentNode.tagName &&
currentNode.tagName.toLowerCase() === "img"
) {
imgElement = currentNode;
break;
}
}
if (imgElement) {
return imgElement.src;
} else {
console.error('No image found following the <h4> with text "Example".');
return null;
}
} catch (error) {
console.error("Error in fetch-wiki-example for tag:", tag, error);
return null;
}
});
ipcMain.handle("fetch-wiki-image", async (event, imageUrl) => {
try {
const response = await fetch(imageUrl);
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const contentType = response.headers.get("content-type");
const buffer = await response.buffer();
const base64 = buffer.toString("base64");
return `data:${contentType};base64,${base64}`;
} catch (err) {
console.error("Error fetching wiki image:", err);
return null;
}
});
ipcMain.handle("fetch-wiki-data", async (event, tag) => {
const url =
"https://danbooru.donmai.us/wiki_pages/" + encodeURIComponent(tag);
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const htmlText = await response.text();
const dom = new JSDOM(htmlText);
const document = dom.window.document;
const wikiBody = document.getElementById("wiki-page-body");
if (!wikiBody) {
console.error('The div with id "wiki-page-body" was not found.');
return { imageUrl: null, description: null };
}
let paragraphs = [];
for (let child of wikiBody.children) {
if (child.tagName.toLowerCase() === "h4") break;
if (child.tagName.toLowerCase() === "p") {
paragraphs.push(child.textContent.trim());
}
}
const descriptionText = paragraphs.join("\n");
let exampleImageUrl = null;
let exampleH4 = null;
const h4s = wikiBody.getElementsByTagName("h4");
for (let h4 of h4s) {
if (h4.textContent.trim() === "Example") {
exampleH4 = h4;
break;
}
}
if (exampleH4) {
const walker = document.createTreeWalker(
wikiBody,
dom.window.NodeFilter.SHOW_ELEMENT,
null,
false
);
walker.currentNode = exampleH4;
while (walker.nextNode()) {
let node = walker.currentNode;
if (node.tagName && node.tagName.toLowerCase() === "img") {
exampleImageUrl = node.src;
break;
}
}
}
return { imageUrl: exampleImageUrl, description: descriptionText };
} catch (error) {
console.error("Error fetching wiki data for tag:", tag, error);
return { imageUrl: null, description: null };
}
});
}
module.exports = { registerIpcHandlers };