-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
105 lines (92 loc) · 3.04 KB
/
content.js
File metadata and controls
105 lines (92 loc) · 3.04 KB
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
let blockedUsers = [];
let blockedTokens = [];
// Function to request blocked items from the background script
function requestBlockedItems() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ action: 'getBlockedItems' }, (response) => {
resolve(response);
});
});
}
function getTokenFromAltText(altText) {
// Extract token name from alt text like "Donatello token avatar"
const tokenMatch = altText.match(/^(\w+)/);
if (tokenMatch) {
console.log('Token extracted from alt text:', tokenMatch[1]);
return tokenMatch[1];
}
console.log('No token found in alt text:', altText);
return null;
}
function hideBlockedContent() {
// Handle user comments (keeping existing functionality)
const userLinks = document.querySelectorAll('a.font-weight-semibold.font-size-2');
userLinks.forEach(link => {
const username = link.textContent.trim();
console.log('Username:', username);
if (blockedUsers.includes(username)) {
const comment = link.closest('.comment');
if (comment) {
console.log('Hiding comment for user:', username);
comment.style.display = 'none';
}
}
});
// Handle token posts using the alt text of the avatar image
const posts = document.querySelectorAll('.post.card');
console.log('Found posts:', posts.length);
posts.forEach(post => {
const avatarImg = post.querySelector('img.avatar-img[alt*="token avatar"]');
if (avatarImg) {
const altText = avatarImg.getAttribute('alt');
const token = getTokenFromAltText(altText);
// Debugging step: Log token and alt text to verify the extraction
console.log('Token:', token, 'Alt Text:', altText);
if (token && blockedTokens.includes(token)) {
console.log('Hiding post for token:', token);
post.style.display = 'none';
}
} else {
console.log('No avatar image found in post:', post);
}
});
}
// Function to set up the MutationObserver
function setupMutationObserver() {
// Set up the observer on the feed container or body
const targetNode = document.body;
if (targetNode) {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.addedNodes.length) {
hideBlockedContent();
}
}
});
observer.observe(targetNode, {
childList: true,
subtree: true
});
} else {
console.error('Failed to find document body');
}
}
// Function to initialize the content script
async function initializeContentScript() {
const response = await requestBlockedItems();
blockedUsers = response.blockedUsers;
blockedTokens = response.blockedTokens;
console.log('Blocked Users:', blockedUsers);
console.log('Blocked Tokens:', blockedTokens);
hideBlockedContent();
setupMutationObserver();
}
// Wait for the DOM to be fully loaded before setting up the observer and hiding content
function waitForDocumentBody() {
if (document.body) {
initializeContentScript();
} else {
requestAnimationFrame(waitForDocumentBody);
}
}
waitForDocumentBody();