-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
106 lines (74 loc) · 2.56 KB
/
main.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
// ==UserScript==
// @name New script
// @namespace Violentmonkey Scripts
// @match *://*twitter.com/home
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js
// @version 1.0
// @author -
// @description 4/27/2023, 10:12:11 AM
// ==/UserScript==
console.log("AAAA: Violentmonkey code running");
function Unhelpful(text) {
if(text.includes("the")){
return true;
}
else {
return false;
}
}
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
function processTwitterDivs() {
var twitter_divs = document.querySelectorAll('[data-testid="cellInnerDiv"]');
console.log(twitter_divs.length);
for (let i = 0; i < twitter_divs.length; ++i) {
try{
var child_divs = twitter_divs[i].getElementsByTagName("*");
var text_div = child_divs[i].querySelector('[data-testid="tweetText"]');
var text = text_div.firstChild.textContent;
if (Unhelpful(text)) {
console.log("TWITTER DIV REMOVE ATTEMPTED");
console.log(text);
// this works best
twitter_divs[i].innerHTML = "<div>REDACTED at timestamp " + String(Date.now()) + " </div>";
//text_div.innerHTML = "<div>REDACTED</div>";
//twitter_divs[i].remove();
}
}
catch(err) {
;
//console.log(err);
}
}
}
// Use Mutation Observer to decide when to trigger twitter div processing
function createAndLaunchMutationObserver(){
const timelineNode = document.querySelector('[data-testid="cellInnerDiv"]').parentElement;
const config = { attributes: false, childList: true, subtree: false };
const callback = (mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === "childList") {
console.log("mutation observed at timestamp " + String(Date.now()));
processTwitterDivs();
}
}
}
const observer = new MutationObserver(callback);
console.log("mutation observer launched");
observer.observe(timelineNode, config);
}
// Need better way to launch - this has to be after Timeline div has loaded in DOM, but not before load of all DOM and content (waiting for that takes too long)
// This code assumes first scroll is in this time interval.
launchedMutationObserver = false;
document.addEventListener("scroll", (event ) => {
if(launchedMutationObserver == false) {
createAndLaunchMutationObserver();
launchedMutationObserver = true;
}
})