-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwignore.js
50 lines (41 loc) · 1.54 KB
/
twignore.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
const storageAuthors = 'twignoreAuthors';
class TweetsBlocker {
constructor(authors) {
this.tweets = [...document.querySelectorAll('.tweet')];
this.blockedAuthors = (authors !== undefined) ? new Set(authors) : new Set();
}
getTweetAuthor(tweet) {
const authorTwitterLink = tweet.querySelector('.tweet__user > a').href;
return authorTwitterLink.split('twitter.com/')[1];
}
hideBlockedTweets() {
this.tweets.filter(
(tweet) => this.blockedAuthors.has(this.getTweetAuthor(tweet))
).forEach(
(tweet) => tweet.classList.add('tweetignore-block')
)
}
blockUser(username) {
this.blockedAuthors.add(username);
chrome.storage.sync.set(
{twignoreAuthors: [...this.blockedAuthors]},
this.hideBlockedTweets()
);
}
renderBlockButton(tweet) {
const tweetAuthor = this.getTweetAuthor(tweet);
const blockButton = document.createElement('div');
blockButton.className = 'tweetignore-block-button';
blockButton.innerHTML = `<a href="#">Заблокировать</>`;
blockButton.addEventListener('click', () => this.blockUser(tweetAuthor))
tweet.querySelector('.tweet__user').appendChild(blockButton);
}
init() {
this.tweets.forEach(tweet => this.renderBlockButton(tweet));
this.hideBlockedTweets();
}
}
chrome.storage.sync.get([storageAuthors], function(result) {
const Blocker = new TweetsBlocker(result[storageAuthors]);
Blocker.init();
})