Skip to content

Commit 77ad9a8

Browse files
authored
v2.0.3: 提升速度,优化表情菜单识别
1 parent 9761f86 commit 77ad9a8

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

NGAEmojiManager.user.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name NGA表情管理器
33
// @namespace https://github.com/jc3213
4-
// @version 2.0.2
4+
// @version 2.0.3
55
// @description 为NGA论坛添加/删除新表情
66
// @author jc3213
77
// @match *://bbs.nga.cn/thread.php?*
@@ -37,15 +37,15 @@ let loader = document.createElement('input');
3737
loader.type = 'file';
3838
loader.accept = '.json';
3939
loader.multiple = true;
40-
loader.addEventListener('change', (event) => {
41-
[...event.target.files].forEach(async (file) => {
40+
loader.addEventListener('change', async (event) => {
41+
for (let file of event.target.files) {
4242
let text = await file.text();
4343
let { name, emoji, author } = JSON.parse(text);
4444
if (!name || !emoji) return;
4545
emojis.set(name, { author, emoji });
4646
GM_setValue('emoji', [...emojis]);
4747
printEmojiUI(name, author, emoji);
48-
});
48+
}
4949
event.target.value = '';
5050
});
5151

@@ -72,7 +72,9 @@ function createEmojiUI(name, author, emoji) {
7272
delete emojiUI[name];
7373
}
7474
else {
75-
emojiPanel.childNodes.forEach((pane) => { pane.style.display = 'none'; });
75+
for (let pane of emojiPanel.childNodes) {
76+
pane.style.display = 'none';
77+
}
7678
panel.style.display = '';
7779
appendEmojiToUI(name, emoji, panel);
7880
}
@@ -89,23 +91,27 @@ function createEmojiUI(name, author, emoji) {
8991
function appendEmojiToUI(name, emoji, panel) {
9092
if (panel.runOnce) return;
9193
panel.runOnce = true;
92-
emoji.forEach((em) => {
94+
for (let em of emoji) {
9395
let img = document.createElement('img');
9496
img.src = 'https://img.nga.178.com/attachments/' + em;
9597
img.alt = em;
9698
panel.appendChild(img);
97-
});
99+
}
98100
}
99101

100102
let observer = new MutationObserver((mutationList) => {
101103
for (let mutation of mutationList) {
102104
for (let node of mutation.addedNodes) {
103-
if (node?.id && node.id.startsWith('commonwindow')) {
104-
let emojiMain = node.children[0].children[1].children[0].children;
105-
emojiTab = emojiMain[0];
106-
emojiPanel = emojiMain[1];
105+
if (node.id.startsWith('commonwindow')) {
106+
let [menu, body] = node.children[0].children;
107+
if (menu.textContent !== '​插入表情') continue;
108+
let pane = body.children[0].children;
109+
emojiTab = pane[0];
110+
emojiPanel = pane[1];
107111
emojiTab.appendChild(manager);
108-
emojis.forEach(({ author, emoji }, name) => printEmojiUI(name, author, emoji));
112+
for (let [name, { author, emoji }] of emojis) {
113+
printEmojiUI(name, author, emoji);
114+
}
109115
return observer.disconnect();
110116
}
111117
}

0 commit comments

Comments
 (0)