forked from Tanza3D/bluesky-quick-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockbutton.user.js
208 lines (191 loc) · 9.99 KB
/
blockbutton.user.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// ==UserScript==
// @name Block button in dropdown
// @version 2024-10-19
// @description because going to the profile takes too long.
// @author Tanza3D
// @updateUrl https://raw.githubusercontent.com/Tanza3D/bluesky-quick-block/refs/heads/main/blockbutton.user.js
// @match https://bsky.app/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=bsky.app
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Put language codes here if there are multiple locales under the same language code. (Currently only for "zh")
const HAS_REGION = ["zh"];
// The message matching the language setting on Bluesky is used. If it's not one of these, English is used as a fallback.
const TL = {
// by Tanza3D
en: "Are you sure you want to block {handle} ({did})? You will have to refresh the page to reflect this change.",
// Machine-translated
//ca: "Estàs cert que vols bloquejar {handle} ({did})? Haureu d'actualitzar la pàgina per reflectir aquest canvi.",
// official translation uses du/deine
// Machine-translated
//de: "Bist du sicher, dass du {handle} ({did}) blockieren willst? Du musst die Seite aktualisieren, um diese Änderung zu berücksichtigen.",
// by HaleyHalcyon
es: "¿Estás cierto de que quieres bloquear a {handle} ({did})? Tendrás que actualizar la página para reflejar este cambio.",
// Machine-translated
//fi: "Halautko varmasti estää {handle} ({did})? Sinun on päivitettävä sivu, jotta tämä muutos näkyy.",
// official translation uses vouvoyer
// Machine-translated
//fr: "Êtes-vous sûr de vouloir bloquer {handle} ({did})\u202F? Vous devrez actualiser la page pour refléter ce changement.",
// Machine-translated
//ga: "An bhfuil tú cinnte gur mhaith leat {handle} ({did}) a bhlocáil? Beidh ort an leathanach a athnuachan chun an t-athrú seo a léiriú.",
// Machine-translated
//hi: "क्या आप वाकई {handle} ({did}) को ब्लॉक करना चाहते हैं? इस परिवर्तन को दर्शाने के लिए आपको पृष्ठ को ताज़ा करना होगा।",
// Machine-translated
//id: "Apakah Anda yakin ingin memblokir {handle} ({did})? Anda perlu menyegarkan halaman untuk mencerminkan perubahan ini.",
// Machine-translated
//it: "Sei sicuro di voler bloccare {handle} ({did})? Sarà necessario aggiornare la pagina per riflettere questa modifica.",
// by HaleyHalcyon
ja: "本当に{handle}({did})をブロックしますか? 反映させるにはページを再読み込みする必要があります。",
// Machine-translated
//ko: "정말 {handle} ({did})을 차단하시겠습니까? 반영하려면 페이지를 다시 로드해야 합니다.",
// by Tanza3D
// Dutch is NOT supported by Bluesky as of this addition
nl: "Weet je zeker dat je {handle} ({did}) wilt blokkeren? Je zult de pagina moeten herladen om de verandering te zien.",
// Machine-translated
//'pt-BR': "Tem certeza de deseja bloquear {handle} ({did})? Você precisará atualizar a página para refletir esta alteração.",
// by Tanza3D
ru: "Вы уверены, что хотите заблокировать {handle} ({did})? Вам нужно будет обновить страницу, чтобы увидеть изменения.",
// Machine-translated
//tr: "{handle} ({did})'ı engellemek istediğinizden emin misiniz? Bu değişikliğin yansıtılması için sayfayı yenilemeniz gerekecektir.",
// Machine-translated
//uk: "Ви впевнені, що бажаєте заблокувати {handle} ({did})? Вам потрібно буде оновити сторінку, щоб відобразити цю зміну.",
// Machine-translated
//"zh-CN": "您确定要阻止 {handle} ({did}) 吗?您将需要刷新页面以反映此更改。",
// Machine-translated
//"zh-TW": "您確定要封鎖 {handle} ({did}) 嗎?您將需要刷新頁面以反映此更改。",
// Machine-translated
//"zh-HK": "你確定要阻止{handle}({did})嗎? 您需要重新加載頁面才能使其生效。",
};
const TLButton = {
// by Tanza3D
en: "Block user",
//ca: "",
//de: "",
// by HaleyHalcyon
es: "Bloquear a usuario",
//fi: "",
//fr: "",
//ga: "",
//hi: "",
//id: "",
//it: "",
// by HaleyHalcyon
ja: "ユーザーをブロックする",
// Machine-translated
//ko: "",
// by HaleyHalcyon
nl: "Gebruiker blokkeren",
//'pt-BR': "",
//ru: "",
//tr: "",
//uk: "",
//"zh-CN": "",
//"zh-TW": "",
//"zh-HK": "",
};
function getLocale() {
const langCheck = (lang) => {
const tokens = lang.split("-");
if (!(tokens[0] in HAS_REGION)) {
if (TL.hasOwnProperty(tokens[0])) {
return tokens[0];
}
} else {
if (TL.hasOwnProperty(lang)) {
return lang;
}
}
return "";
}
const browserLangList = navigator.languages;
for (let lang of browserLangList) {
const check = langCheck(lang);
if (check !== "") {
return check;
}
}
const blueskyLang = document.getElementsByTagName("HTML")[0].lang;
const check = langCheck(blueskyLang);
if (check !== "") {
return check;
}
return "en";
}
(function() {
'use strict';
function addButtonToDropdown(menu) {
if (!menu.classList.contains('new-dropdown-button')) {
menu.classList.add("new-dropdown-button");
menu.addEventListener('click', () => {
var account = JSON.parse(localStorage.getItem('BSKY_STORAGE')).session.currentAccount;
setTimeout(() => {
var list = document.querySelector('[role="menu"][data-state="open"]').querySelector(".css-175oi2r");
var buttonToClone = list.querySelector('[data-testid="postDropdownMuteWordsBtn"');
var button = buttonToClone.cloneNode(true)
list.appendChild(button);
let locale = getLocale();
button.querySelector(".css-146c3p1").innerText = TLButton[locale];
list.insertBefore(button, buttonToClone);
var postItem = menu.closest('[data-testid^="feedItem-by-"]');
if(postItem == null) postItem = menu.closest('[data-testid^="postThreadItem-by-"]');
console.log(postItem);
var handle = "";
var tmp = postItem.querySelectorAll('a');
console.log(tmp);
for(var i of tmp) {
if(i.querySelector("span") == null) continue;
console.log(i.querySelector("span").innerText)
if(i.querySelector("span").innerText.trim().startsWith("@") && i.href.includes("/profile")) handle = i.querySelector("span").innerText.trim();
}
var pfp = postItem.querySelector('[data-testid="userAvatarImage"]').querySelector("img").src;
var did = pfp.split("/");
for(var v of did) {
if(v.startsWith("did:")) {
did = v.split("@")[0];
}
}
button.style.background = "#ff000022";
button.addEventListener("click", async () => {
let locale = getLocale();
if (window.confirm(TL[locale].replace("{handle}", handle).replace("{did}", did))) {
await fetch(account.pdsUrl+"xrpc/com.atproto.repo.createRecord", {
method: "POST",
headers: {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0",
"Accept": "*/*",
"Content-Type": "application/json",
"Authorization": "Bearer " + account.accessJwt,
"Origin": "https://bsky.app",
},
body: JSON.stringify({
collection: "app.bsky.graph.block",
repo: account.did,
record: {
subject: did,
createdAt: new Date().toISOString(),
$type: "app.bsky.graph.block"
}
})
});
}
});
}, 30)
});
}
}
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
const dropdowns = document.querySelectorAll('[data-testid="postDropdownBtn"]');
for (var dropdown of dropdowns) {
addButtonToDropdown(dropdown);
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();
})();