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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * 噗浪表符整理器 v0.9 (20201217)
 * ==============
 * 利用噗浪本身的JS/HTML砌成,以便批量管理表符
 * 僅限於桌面版使用
 *
 * 功能:
 * - 利用噗下留言中的表符去新增/移動/刪除表符
 * - 顯示表符總數
 * - 刪除群組內的所有表符(清空群組,群組保留)
 *
 * 備註:
 * 1. 程式沒有加入60/800的上限檢查(也沒進行測試),表符庫爆掉沒有提示,請自行檢查
 * 2. 成功與錯誤都沒有訊息提示
 * 3. 此script重度依賴噗浪桌面版的JS/HTML,日後噗浪改版可能會無法使用(例如沒有反應)
 */
javascript:(function(){
	
	window.custPlugin = window.custPlugin || {};
	window.custPlugin.emo = {
		removeEmo: async function(hashId){
			var postResult = await $.post("/Emoticons/remove",{token:top.SiteState.getToken(), hash_id:hashId}).promise();
			return postResult;
		},
		
		batchDeleteEmoByIds: async function(emos){
			if (emos.length > 0) {
				for (const e of emos) {
					let resp = await this.removeEmo(this.getHashFromURL(e.src));
					console.info(this.getEmoByUrl(e.src).keyword + ' is deleted.');
				}
				custPlugin.emo.refreshEmoticon();
			}
		},

		batchDeleteEmoByGrpId: function(emoGroupId){
			var emoGroupId = this.getSelectedEmoGrpId();
			var _self=this;
			if (emoGroupId !== undefined && emoGroupId > 1){
				$.post("/EmoticonManager/getUserEmoticons",{token:top.SiteState.getToken()}).done(async function(emos){
					for (const e of (emos[emoGroupId])) {
						await _self.removeEmo(e.hash_id);
						console.info(e.keyword + ' is removed.');
					}
					custPlugin.emo.refreshEmoticon();
				});
			}
		},

		getHashFromURL: function(url){
			if (url !== undefined && url.length > 0){
				return url.substring(url.lastIndexOf('/')+1, url.indexOf('_'));
			}
		},

		addEmo: async function(hashId, groupId, keyword){
			var postResult = await $.post("/Emoticons/add",{form_token:top.SiteState.getToken(), hash_id: hashId, keyword: keyword, group_id: groupId}).promise();
			return postResult;
		},

		batchAddEmo: async function(emos, groupId){
			if (emos.length > 0) {
				for (const e of emos) {					
					let resp = await this.addEmo(this.getHashFromURL(e.src), groupId, '');
					console.info(resp.keyword + ' is added.');
				}
				custPlugin.emo.refreshEmoticon();
			}
		},
		
		setEmoGroup: async function(hashId, groupId){
			var postResult = await $.post("/EmoticonManager2/setGroup",{token:top.SiteState.getToken(), hash_id:hashId, group_id: groupId}).promise();
			return postResult;
		},
		
		batchSetEmoGroup: async function(emos, groupId){
			if (emos.length > 0) {
				for (const e of emos) {
					let resp = await this.setEmoGroup(this.getHashFromURL(e.src), groupId);
					console.info(this.getEmoByUrl(e.src).keyword + ' is moved.' + ' to group='+groupId);
				}
				custPlugin.emo.refreshEmoticon();
			}
		},
		
		getEmoTotalCount: function(){
			var total = 0;
			for (let grp of EmoticonCustom.getGroups()){
				total += EmoticonCustom.getEmoticons(grp.id).length;
			}
			return total;
		},

		getSelectedEmoGrpId: function(){
			return $('#emoticon-tabs li.current[emo-group-type="custom"]').attr('emo-group-id');
		},
		
		isCustomEmoGrpSelected: function(){
			var emoGroupId = this.getSelectedCustomEmoGrpId();
			return emoGroupId !== undefined && emoGroupId > 0;
		},
		
		getCounterNode: function(){
			var counterNode = document.querySelector('#emoCounter');
			if (counterNode == undefined){
				var newNode = document.createElement("DIV");
				newNode.id = "emoCounter";
				var postNode = document.querySelector('#emoticon-selector-holder .manager-button');
				postNode.parentNode.insertBefore(newNode, postNode);
				$(newNode).css({
					position : "absolute",
					right: "130px",
					top: "12px",
					lineHeight: "26px"
				});
				counterNode = newNode;
			}
			return counterNode;
		},

		updateCounterNode: function(groupType, groupId){
			var counterNode = this.getCounterNode();
			var html = '';
			if (groupType == 'custom'){
				html = '群組' + groupId + ':' + EmoticonCustom.getEmoticons(groupId).length + '/' + this.getEmoTotalCount();
			}
			else{
				html = this.getEmoTotalCount();
			}
			$(counterNode).html(html);
		},
		
		getBatchDeleteNode: function(){
			var deleteBtnNode = document.querySelector('#emoGrpBatchDelete');
			if (deleteBtnNode == undefined){
				var newNode = document.createElement("DIV");
				newNode.id = "emoGrpBatchDelete";
				var postNode = document.querySelector('#emoticon-selector-holder .manager-button');
				postNode.parentNode.insertBefore(newNode, postNode);
				$(newNode).css({
					position : "absolute",
					right: "50px",
					top: "11px",
					lineHeight: "26px",
					padding: "0 5px",
					background: "#434343",
					color: "#FFF",
					borderRadius: "4px",
					textAlign: "center",
					fontSize: "13px",
					width: "65px",
				});
				$(newNode).html("刪除群組");
				deleteBtnNode = newNode;
			}
			return deleteBtnNode;
		},
		
		updateBatchDeleteNode: function(groupType, groupId){
			var deleteEmoBtn = this.getBatchDeleteNode();
			if (groupType == 'custom' && groupId > 1){
				html = '群組' + groupId + ':' + EmoticonCustom.getEmoticons(groupId).length + '/' + this.getEmoTotalCount();
				$(deleteEmoBtn).off("click");
				$(deleteEmoBtn).on("click", function() {
					if (confirm('確定刪除選擇群組的所有表符?')){
						custPlugin.emo.batchDeleteEmoByGrpId();
					}
				});
				$(deleteEmoBtn).css({
					opacity: '1',
					cursor: "pointer",
				});
			}
			else{
				$(deleteEmoBtn).off("click");
				$(deleteEmoBtn).css({
					opacity:'0.3',
					cursor: "default",
				});
			}
		},
		
		updateEmoPanel: function(grpType, grpId){
			if (grpType == undefined && grpId == undefined){
				grpType = $('#emoticon-tabs li.current').attr('emo-group-type');
				grpId = $('#emoticon-tabs li.current').attr('emo-group-id');
			}
			this.updateCounterNode(grpType, grpId);
			this.updateBatchDeleteNode(grpType, grpId);
		},
		
		initEmoPanel: function(){
			if (document.querySelector('#emoticon-tabs li.current') != undefined){
				$('#emoticon-tabs li[emo-group-type]').on( "click", function() {
					let grpType = $(this).attr('emo-group-type');
					let grpId = $(this).attr('emo-group-id');
					custPlugin.emo.updateEmoPanel(grpType, grpId);
				});
				
				custPlugin.emo.updateEmoPanel();
			}
		},
		
		addEmoPanelToComment: function(){
			var node = document.querySelector('#emoPanelInComment');
			if (node == undefined){
				let respManagerEle = document.querySelector('.show-response-manager');
				if (respManagerEle != null){
					var containerNode = $( "<div id='emoPanelInComment'></div>" );
					var addEmoNode = $( "<span class='emoBtn addEmo' title='新增或移動表符'>⭐</span>" );
					var removeEmoNode = $( "<span class='emoBtn removeEmo' title='刪除表符'>❌</span>" );
					
					addEmoNode.on("click", function() {
						var emos = Object.values(document.querySelectorAll('.show-response-manager .text_holder img.emoticon_my'));
						custPlugin.emo.showEmoGroupPopup(this, emos, 'add');
					});
					removeEmoNode.on("click", function() {
						var emos = Object.values(document.querySelectorAll('.show-response-manager .text_holder img.emoticon_my'));
						custPlugin.emo.showEmoGroupPopup(this, emos, 'delete');
					});
					
					containerNode.append(addEmoNode);
					containerNode.append(removeEmoNode);
					
					$('.show-response-manager .time').after(containerNode);
					
					$('#emoPanelInComment .emoBtn').css({
						padding: "0 2px",
						margin: "0 2px",
						background: "transparent",
						color: "#FFF",
						borderRadius: "4px",
						cursor: "pointer",
						textAlign: "center",
						fontSize: "13px",
						fontWeight: "600",
						display: "inline-block",
						width: "24px"
					});
					
					$('#emoPanelInComment .emoBtn').hover(function() {
						$(this).css("background-color","#b6e2dc");
					}, function(){
						$(this).css("background-color","transparent");
					});
				}
			}
		},
		
		getGroupIdByEmo: function(url){
			var hash = this.getHashFromURL(url);
			var allEmos = EmoticonCustom.getEmoticons();
			for (var grpId in allEmos){
				for (var emo of allEmos[grpId]){
					if (hash == emo['hash_id']){
						return grpId;
					}
				}
			}
			return -1;
		},
		
		getEmoByUrl: function(url){
			var hash = this.getHashFromURL(url);
			var allEmos = EmoticonCustom.getEmoticons();
			for (var grpId in allEmos){
				for (var emo of allEmos[grpId]){
				  if (hash == emo['hash_id']){
					  return emo;
				  }
				}
			}
			return null;
		},
		
		isEmoExists: function(url){
			return this.getGroupIdByEmo(url) > 0;
		},
		
		isEmoInGroup: function(url, grpId){
			var resultGroupId = this.getGroupIdByEmo(url);
			return resultGroupId > 0 && resultGroupId == grpId;
		},
		
		refreshEmoticon: function(){
			console.info('Refresh EmoticonCustom...');
			window.EmoticonCustom.load(true);
			window.setTimeout(function () {
				custPlugin.emo.initEmoPanel();
			}, 2000);
		},
		
		showEmoGroupPopup: function(fromEle, emos, usage){
			var ele = $('<div id="custEmoGrpPanel" class="emo-adder"><div class="emo-adder-panel"><div class="emo-groups"><div class="emo-message"></div></div></div></div>');

			if (emos.length == 0) {
				$('div.emo-message', ele).append('未選擇留言/留言沒有表符');
			}
			else{
				if (usage == 'add'){
					var ul = $('<ul></ul>');
					for (var grp of EmoticonCustom.getGroups()){
						var li = $('<li emo-group-id="' + grp['id'] + '"><img src="' + grp['thumbnail'] + '"/></li>');
						li.on('click', function(){
							var groupId = $(this).attr('emo-group-id');
							var toAddEmos = emos.filter(e => !custPlugin.emo.isEmoExists(e.src));
							var toMoveEmos = emos.filter(e => custPlugin.emo.isEmoExists(e.src) && !custPlugin.emo.isEmoInGroup(e.src, groupId));
							if (toAddEmos.length > 0){
								custPlugin.emo.batchAddEmo(toAddEmos, groupId);
							}
							if (toMoveEmos.length > 0){
								custPlugin.emo.batchSetEmoGroup(toMoveEmos, groupId);
							}
							$('#custEmoGrpPanel').remove();
						});
						ul.append(li);
					}
					$('div .emo-groups', ele).append(ul);
				}
				else if (usage == 'delete'){
					$('div.emo-message', ele).append('確定刪除留言內包含的所有表符?');
					
					var toDelEmos = emos.filter(e => custPlugin.emo.isEmoExists(e.src));
					
					var buttonPanel = $('<div class="buttons" />');
					var delBtn = $('<div class="button small-button pif-delete">刪除</div>');
					delBtn.on('click', function(){
						if (toDelEmos.length > 0){
							custPlugin.emo.batchDeleteEmoByIds(toDelEmos);
						}
						$('#custEmoGrpPanel').remove();
					});
					buttonPanel.append(delBtn);
					
					var cancelBtn = $('<div class="add-button button small-button pif-cancel">取消</div>');
					cancelBtn.on('click', function(){
						$('#custEmoGrpPanel').remove();
					});
					buttonPanel.append(cancelBtn);
					
					$('div.emo-groups', ele).append(buttonPanel);
				}
			}

			var popView = new PopView({
			  content: ele,
			  exClass: 'pop-tooltip',
			  direction: 'bottom',
			  keep: false,
			  hoverArea: true,
			});
			popView.showFrom(fromEle);
		}
	};

	EmoticonSelector.toggleFrom();
	for( let i = 0; i < 5; i++ ) {
		window.setTimeout(function () {
			custPlugin.emo.initEmoPanel();
		}, 1000 * i);
	}
	
	function loadCommentEmoPanel(){
		custPlugin.emo.addEmoPanelToComment();
		window.setTimeout(loadCommentEmoPanel, 500);
	}
	loadCommentEmoPanel();

})()