-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastodon-clear-column.user.js
125 lines (109 loc) · 4.83 KB
/
mastodon-clear-column.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
// ==UserScript==
// @name Hide Articles in Mastodon Column on button click
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Adds an icon to each column header to hide articles in the respective column
// @author @phpmacher@sueden.social
// @match https://sueden.social/*
// @match https://chaos.social/*
// @match https://mastodon.social/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Constants
const ICON_HTML = '💧';
const COLUMN_SELECTOR = '.column';
const COLUMN_HEADER_SELECTOR = '.column-header';
const ARTICLE_SELECTOR = '.scrollable article';
const ICON_ADDED_CLASS = 'icon-added';
// Column Utilities
const columnUtils = {
// Function to create an icon and append it to the column header
addIconToColumnHeader: function (column) {
try {
// Check if the icon was already added to prevent multiple additions
if (column.classList.contains(ICON_ADDED_CLASS)) {
return; // Icon already added, no need to add again
}
const header = column.querySelector(COLUMN_HEADER_SELECTOR);
const button = header?.querySelector('button');
if (header && button) {
// Create the icon element
const icon = document.createElement('div');
icon.textContent = ICON_HTML;
icon.style.cursor = 'pointer';
icon.style.margin = '15px 5px auto';
icon.style.display = 'inline-block';
icon.style.fontSize = '20px';
icon.classList.add('hide-articles-icon');
// Add click event listener to the icon
const clickListener = () => {
// Toggle article visibility in the current column
const articles = column.querySelectorAll(ARTICLE_SELECTOR);
articles.forEach(article => {
if (article.style.display !== 'none') {
// Hide article
article.style.display = 'none';
}
});
};
icon.addEventListener('click', clickListener);
// Insert the icon after the button in the header
button.insertAdjacentElement('afterend', icon);
// Mark the column as processed by adding a class
column.classList.add(ICON_ADDED_CLASS);
// Store the click listener for later removal
column.clickListener = clickListener;
}
} catch (error) {
console.error('Error adding icon:', error);
}
},
// Remove the icon and the event listener from a column
removeIconFromColumn: function (column) {
try {
// Remove the icon
const icon = column.querySelector('.hide-articles-icon');
if (icon) {
icon.removeEventListener('click', column.clickListener);
icon.remove();
}
// Remove the ICON_ADDED_CLASS
column.classList.remove(ICON_ADDED_CLASS);
} catch (error) {
console.error('Error removing icon:', error);
}
},
// Find all columns and add the icon to each column header
addIconsToAllColumns: function () {
const columns = document.querySelectorAll(COLUMN_SELECTOR);
columns.forEach(column => {
this.addIconToColumnHeader(column);
});
}
};
// MutationObserver to dynamically handle new articles or columns
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE && node.matches(COLUMN_SELECTOR)) {
columnUtils.addIconToColumnHeader(node);
}
});
}
if (mutation.removedNodes.length > 0) {
mutation.removedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE && node.matches(COLUMN_SELECTOR)) {
columnUtils.removeIconFromColumn(node);
}
});
}
});
});
// Start observing the document body for changes
observer.observe(document.body, {childList: true, subtree: true});
// Initial call to add icons to all existing columns
columnUtils.addIconsToAllColumns();
})();