-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserve.js
57 lines (40 loc) · 2.13 KB
/
observe.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
window.posts = {}
const addObserver = () => {
window.intervalId = setInterval(() => {
let cont = document.querySelectorAll('div[role="main"]').length == 2
? document.querySelectorAll('div[role="main"]')[1].children[0]
: document.querySelector('div[role="feed"]') || document.querySelector('div[role="main"] > div:last-child > div:last-child > div > div:last-child')
if(!cont) return
window.clearInterval(window.intervalId)
new window.MutationObserver(postsAdded).observe(cont, { childList: true })
setTimeout(postsAdded, 800)
}, 500)
}
const postsAdded = async () => {
const addedPosts = [...document.querySelectorAll('div[data-ad-preview="message"]:not(.pure)')].map(posdDom => new PostClass(posdDom))
if (!addedPosts.length) return
for(let i = 0; i < addedPosts.length; i++){
await addedPosts[i].checkInDB()
}
showTags(addedPosts.filter(post => post.tags && post.tags.length))
let postsForPrediction = addedPosts.filter(post => !post.tags)
if (!postsForPrediction.length) return
let postsForPredictionKa = postsForPrediction.filter(post => post.isKa )
if(postsForPredictionKa.length){
let predictions = await predictKa(postsForPrediction.map(post => ({hash: post.hash, text: post.text})))
for(let i = 0; i < Object.keys(predictions).length; i++){
let hash = Object.keys(predictions)[i]
await window.posts[hash].setTags(predictions[hash])
}
showTags(Object.keys(predictions).map(hash => window.posts[hash]).filter(post => post.tags.length))
}
let postsForPredictionEn = postsForPrediction.filter(post => post.isEn )
if(postsForPredictionEn.length){
let predictions = await predictEn(postsForPredictionEn)
for(let i = 0; i < Object.keys(predictions).length; i++){
let hash = Object.keys(predictions)[i]
await window.posts[hash].setTags(predictions[hash])
}
showTags(Object.keys(predictions).map(hash => window.posts[hash]).filter(post => post.tags.length))
}
}