SHOW:
|
|
- or go back to the newest paste.
1 | // ==UserScript== | |
2 | // @name Hide thread iichan | |
3 | // @namespace http://iichan.hk/ | |
4 | // @license WTFPL | |
5 | // @version 0.1 | |
6 | // @description adds hide thread feature to iichan | |
7 | // @author Cirno | |
8 | // @match http://iichan.hk/* | |
9 | // @grant none | |
10 | // ==/UserScript== | |
11 | ||
12 | (function() { | |
13 | 'use strict'; | |
14 | ||
15 | var THREAD_TITLE_LENGTH = 50; // Сколько первых символов из поста показывать в заголовке скрытого треда | |
16 | ||
17 | function removeClass(element, className) { | |
18 | var reg = new RegExp('(\\s|^)' + className + '(\\s|$)'); | |
19 | element.className = element.className.replace(reg, ' '); | |
20 | } | |
21 | ||
22 | function addHideBtns() { | |
23 | var threads = document.querySelectorAll('[id^=thread]'); | |
24 | for (var i = threads.length - 1; i >= 0; i--) { | |
25 | var thread = threads[i]; | |
26 | var label = thread.querySelector('label'); | |
27 | if (!label) continue; | |
28 | var btn = document.createElement('a'); | |
29 | btn.innerHTML = '[-]'; | |
30 | btn.className = 'iichan-hide-thread-btn'; | |
31 | btn.title = 'Скрыть тред'; | |
32 | - | btn.addEventListener('click', function() {hideThread(thread.id);}); |
32 | + | btn.threadId = thread.id; |
33 | btn.addEventListener('click', function() {hideThread(this.threadId);}); | |
34 | thread.insertBefore(btn, label.nextSibling); | |
35 | } | |
36 | } | |
37 | ||
38 | function unHideThread(threadId) { | |
39 | var hiddenThreads = JSON.parse(window.localStorage.getItem('iichan_hidden_threads') || "[]"); | |
40 | var index = hiddenThreads.indexOf(threadId); | |
41 | if (index === -1) { | |
42 | return; | |
43 | } | |
44 | hiddenThreads.splice(index, 1); | |
45 | window.localStorage.setItem('iichan_hidden_threads', JSON.stringify(hiddenThreads)); | |
46 | ||
47 | var thread = document.getElementById(threadId); | |
48 | if(!thread) { | |
49 | return; | |
50 | } | |
51 | removeClass(thread, 'iichan-thread-hidden'); | |
52 | var placeholder = document.getElementById('iichan-hidden-' + threadId); | |
53 | if (placeholder) { | |
54 | placeholder.parentElement.removeChild(placeholder); | |
55 | } | |
56 | } | |
57 | ||
58 | function hideThread(threadId) { | |
59 | var thread = document.getElementById(threadId); | |
60 | if(!thread) { | |
61 | return; | |
62 | } | |
63 | var parent = thread.parentNode; | |
64 | var threadHiddenDiv = document.createElement('div'); | |
65 | var threadNo = threadId.split('-')[1]; | |
66 | var threadTitle = thread.querySelector('.filetitle').innerText || thread.querySelector('blockquote').innerText || 'картинка'; | |
67 | threadTitle = threadTitle.substr(0, THREAD_TITLE_LENGTH); | |
68 | threadHiddenDiv.innerHTML = 'Тред №' + threadNo + ' скрыт (' + threadTitle + ')'; | |
69 | threadHiddenDiv.id = 'iichan-hidden-' + threadId; | |
70 | threadHiddenDiv.className = 'reply iichan-hidden-thread-placeholder'; | |
71 | threadHiddenDiv.addEventListener('click', function() {unHideThread(threadId);}); | |
72 | thread.className += ' iichan-thread-hidden'; | |
73 | parent.insertBefore(threadHiddenDiv, thread); | |
74 | ||
75 | // save result | |
76 | var hiddenThreads = JSON.parse(window.localStorage.getItem('iichan_hidden_threads') || "[]"); | |
77 | if (hiddenThreads.indexOf(threadId) === -1) { | |
78 | hiddenThreads.push(threadId); | |
79 | window.localStorage.setItem('iichan_hidden_threads', JSON.stringify(hiddenThreads)); | |
80 | } | |
81 | } | |
82 | ||
83 | function hideAllHiddenThreads() { | |
84 | var hiddenThreads = JSON.parse(window.localStorage.getItem('iichan_hidden_threads') || "[]"); | |
85 | for (var i = hiddenThreads.length - 1; i >= 0; i--) { | |
86 | hideThread(hiddenThreads[i]); | |
87 | } | |
88 | } | |
89 | ||
90 | function appendCSS() { | |
91 | var css = document.createElement('style'); | |
92 | css.type = 'text/css'; | |
93 | css.innerHTML = '\ | |
94 | .iichan-hidden-thread-placeholder {\ | |
95 | cursor: pointer;\ | |
96 | }\ | |
97 | .iichan-hidden-thread-placeholder:hover + div, .iichan-hidden-thread-placeholder:hover + div + br {\ | |
98 | display: block !important;\ | |
99 | }\ | |
100 | .iichan-thread-hidden {\ | |
101 | display: none;\ | |
102 | }\ | |
103 | .iichan-thread-hidden + br {\ | |
104 | display: none;\ | |
105 | }\ | |
106 | .iichan-hide-thread-btn {\ | |
107 | margin-left: 0.2em;\ | |
108 | cursor: pointer;\ | |
109 | }\ | |
110 | '; | |
111 | document.body.appendChild(css); | |
112 | } | |
113 | ||
114 | appendCSS(); | |
115 | addHideBtns(); | |
116 | hideAllHiddenThreads(); | |
117 | })(); |