-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNGAEmojiManager.user.js
127 lines (119 loc) · 4.29 KB
/
NGAEmojiManager.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
// ==UserScript==
// @name NGA Emoji Manager
// @name:zh NGA表情管理器
// @namespace https://github.com/jc3213
// @version 1.9.1
// @description Add/Remove New Emoji to NGA Forum
// @description:zh 为NGA论坛添加/删除新表情
// @author jc3213
// @match *://bbs.nga.cn/thread.php?*
// @match *://bbs.nga.cn/read.php?*
// @match *://bbs.nga.cn/post.php?*
// @match *://ngabbs.com/thread.php?*
// @match *://ngabbs.com/read.php?*
// @match *://ngabbs.com/post.php?*
// @match *://nga.178.com/thread.php?*
// @match *://nga.178.com/read.php?*
// @match *://nga.178.com/post.php?*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
var emojiPackage = GM_getValue('emoji', []);
var emojiRunOnce = {};
var emojiTab;
var emojiPanel;
var emojiOrigin;
var css = document.createElement('style');
css.innerText = `.single_ttip2 img {max-height: 64px;}`
document.head.append(css);
var manager = document.createElement('button');
manager.innerText = '添加新表情';
manager.className = 'block_txt_big';
manager.addEventListener('click', event => subscribe.click());
var subscribe = document.createElement('input');
subscribe.type = 'file';
subscribe.accept = 'application/json';
subscribe.multiple = 'true';
subscribe.addEventListener('change', async event => {
var files = [...subscribe.files];
var result = files.map(async file => {
var json = await fileReader(file);
var find = emojiPackage.findIndex(package => package.name === json.name);
find = find === -1 ? emojiPackage.length : find;
emojiPackage[find] = json;
createEmojiUI(json, find);
});
await Promise.all(result);
GM_setValue('emoji', emojiPackage);
subscribe.value = '';
});
function createEmojiUI({name, author, emoji}, index) {
var panel = emojiPanel.childNodes[index + emojiOrigin];
if (panel) {
panel.innerHTML = '';
panel.style.display = 'none';
delete emojiRunOnce[name];
}
else {
var tab = document.createElement('button');
tab.className = 'block_txt_big';
tab.innerText = name;
tab.title = '制作者: ' + author;
manager.before(tab);
panel = document.createElement('div');
emojiPanel.append(panel);
tab.addEventListener('click', event => {
if (event.ctrlKey && confirm('确定要删除表情包【'+ name + '】吗?')) {
emojiPackage.splice(index, 1);
delete emojiRunOnce[name];
GM_setValue('emoji', emojiPackage);
tab.remove();
panel.remove();
}
else {
emojiPanel.childNodes.forEach(ePanel => {
ePanel.style.display = ePanel === panel ? 'block' : 'none';
});
appendEmojiToUI(name, panel);
}
});
}
}
function appendEmojiToUI(name, panel) {
if (emojiRunOnce[name]) {
return;
}
var {emoji} = emojiPackage.find(package => package.name === name);
emoji.forEach(em => {
var img = document.createElement('img');
img.src = 'https://img.nga.178.com/attachments/' + em;
img.addEventListener('click', event => {
postfunc.addText('[img]' + em + '[/img]');
postfunc.selectSmilesw._.hide();
});
panel.appendChild(img);
});
emojiRunOnce[name] = true;
}
function fileReader(file) {
return new Promise(resolve => {
var reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
var json = JSON.parse(reader.result);
resolve(json);
};
});
}
new MutationObserver(mutationList => {
mutationList.forEach(mutation => {
var newNode = mutation.addedNodes[0];
if (newNode && newNode.id.startsWith('commonwindow')) {
emojiTab = newNode.querySelector('.div1 > .div2 > .div3 > div');
emojiPanel = newNode.querySelector('.div1 > .div2 > .div3 > span');
emojiOrigin = emojiPanel.childNodes.length;
emojiTab.appendChild(manager);
emojiPackage.forEach(createEmojiUI);
}
});
}).observe(document.body, {childList: true});