-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
127 lines (112 loc) · 4.16 KB
/
script.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
126
127
//https://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists
function waitForElm(selector) {
return new Promise((resolve) => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver((mutations) => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
});
}
function updateBlockButtonIcon(blockButton) {
let firstDivChild = blockButton.children("div:first-child");
firstDivChild.attr("data-testid", "block");
firstDivChild.attr("aria-label", "Block");
blockButton.find("svg").parent().html(
// Bootstrap Dash circle
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-dash-circle" viewBox="0 0 16 16"> <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/> <path d="M4 8a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7A.5.5 0 0 1 4 8z"/> </svg>'
);
}
//https://stackoverflow.com/questions/43613261/javascript-create-and-destroy-a-toast-message
function CreateToast(toastMessage) {
let toastDivContainer = document.createElement("div");
toastDivContainer.className = "one-click-block-report-toast-container";
let toastDiv = document.createElement("div");
toastDiv.className = "one-click-block-report-toast";
toastDiv.innerHTML = toastMessage;
toastDivContainer.appendChild(toastDiv);
document.body.appendChild(toastDivContainer);
}
function addBlockAndReportButton(article) {
let shareButton = article.find('div[role="group"] div:nth-child(4)');
if (!shareButton.length) {
// blocked
return;
}
shareButton.after(shareButton.clone());
let blockButton = shareButton.next();
updateBlockButtonIcon(blockButton);
blockButton.click(function (event) {
var hideMenuTemporarily = true;
var hideDialogTemporarily = true;
CreateToast("Reporting tweet and blocking user...");
let more = $(event.target)
.closest("article")
.find("div[aria-label='More']");
more.click();
waitForElm("div[role='menu']").then((menu) => {
if (hideMenuTemporarily) {
$(menu).hide();
hideMenuTemporarily = false;
}
});
waitForElm("div[data-testid='report']").then((report) => {
report.click();
waitForElm("div[role='dialog']").then((dialog) => {
if (hideDialogTemporarily) {
$(dialog).hide();
hideDialogTemporarily = false;
}
});
waitForElm("div[role='dialog'] iframe").then((ifr) => {
$("div[role='dialog'] iframe").on("load", function () {
// This block is run each time the frame changes
// however, not all elements are always present
let ifrm = $("div[role='dialog'] iframe");
let spamButton = ifrm.contents().find("button[id='spam-btn']");
let spamCatchAllButton = ifrm
.contents()
.find("button[id='spam-catchall-btn']");
let blockButton = ifrm.contents().find("button[id='block-btn']");
let closeButton = $("div[aria-label='Close']");
if (spamButton.length) {
spamButton.click();
} else if (spamCatchAllButton.length) {
spamCatchAllButton.click();
} else if (blockButton.length) {
blockButton.click();
} else {
closeButton.click();
//https://stackoverflow.com/questions/43613261/javascript-create-and-destroy-a-toast-message
$(document)
.find(".one-click-block-report-toast-container")
.fadeOut(200, function () {
$(this).remove();
});
}
});
});
});
});
}
$(function () {
$(document).on("DOMNodeInserted", (event) => {
let articles = $(event.target).find("article");
articles.each((k, value) => {
if ($(value).find("span:contains('You reported this Tweet')").length) {
//article with blocked tweet
//continue
} else {
addBlockAndReportButton($(value));
}
});
});
});