-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
178 lines (157 loc) · 5.11 KB
/
content-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
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
// Constants for styling
const BUTTON_STYLES = {
backgroundColor: "#53fc18",
color: "black",
border: "none",
padding: ".300rem .25rem",
textAlign: "center",
textDecoration: "none",
fontSize: ".750rem",
lineHeight: "1.25",
margin: "2px 1px",
cursor: "pointer",
borderRadius: "5px",
display: "flex",
alignItems: "center",
fontWeight: "600",
fontFamily: "inherit",
transition: "background-color 0.3s, transform 0.3s",
};
const ADD_ALL_BUTTON_STYLES = {
...BUTTON_STYLES,
padding: ".600rem .25rem",
fontSize: ".850rem",
};
// Helper function to apply styles
function applyStyles(element, styles) {
Object.assign(element.style, styles);
}
// Helper function to create a button
function createButton(text, className, styles) {
const button = document.createElement("button");
button.textContent = text;
button.className = className;
applyStyles(button, styles);
return button;
}
// Function to add button to streamer usernames
function addButtonToStreamerUsernames() {
const actionButtons = document.querySelectorAll(
".stream-username > span:nth-child(1)"
);
actionButtons.forEach((actionButton) => {
if (
!actionButton.nextElementSibling ||
actionButton.nextElementSibling.className !== "streamer-button"
) {
const url = window.location.href;
const streamerName = url.split("/").pop();
const button = createButton("", "streamer-button", BUTTON_STYLES);
browser.storage.local.get(streamerName).then((result) => {
button.textContent = result[streamerName]
? "Remove Streamer"
: "Add Streamer";
});
button.addEventListener("click", () =>
handleStreamerButtonClick(button, streamerName)
);
actionButton.parentNode.insertBefore(button, actionButton.nextSibling);
}
});
}
// Function to handle streamer button click
function handleStreamerButtonClick(button, streamerName) {
const isAdding = button.textContent === "Add Streamer";
button.textContent = isAdding ? "Remove Streamer" : "Add Streamer";
browser.runtime.sendMessage({
type: isAdding ? "addStreamer" : "removeStreamer",
content: streamerName,
});
}
// Function to create or remove "Add All Streamers" button
function updateAddAllStreamersButton() {
const channelsElement = document.querySelector(
'div[data-v-adccd6b9][class*="border-primary/100"]'
);
const streamerElements = document.querySelectorAll(
"div[data-v-3e774179] > a"
);
const existingButton = document.querySelector(".add-all-streamers-button");
// Check if we're on the Channels tab
const isChannelsTab = channelsElement && streamerElements.length > 0;
if (isChannelsTab && !existingButton) {
// Create and add the button if we're on the Channels tab and the button doesn't exist
const addAllStreamersButton = createButton(
"Add All Streamers",
"add-all-streamers-button",
ADD_ALL_BUTTON_STYLES
);
addAllStreamersButton.addEventListener("click", handleAddAllStreamersClick);
channelsElement.parentNode.insertBefore(
addAllStreamersButton,
channelsElement.nextSibling
);
} else if (!isChannelsTab && existingButton) {
// Remove the button if we're not on the Channels tab and the button exists
existingButton.remove();
}
}
// Function to handle "Add All Streamers" button click
function handleAddAllStreamersClick() {
const streamerElements = document.querySelectorAll(
"div[data-v-3e774179] > a"
);
if (streamerElements.length === 0) {
showNotification(
"No streamers found. Please make sure you're on the Channels tab."
);
return;
}
const streamerNames = Array.from(streamerElements).map((element) =>
element.getAttribute("href").slice(1)
);
Promise.all(
streamerNames.map((streamerName) =>
browser.runtime.sendMessage({
type: "addStreamer",
content: streamerName,
})
)
)
.then((responses) => {
const addedCount = responses.filter(
(response) => response.status === "success"
).length;
const alreadyAddedCount = responses.filter(
(response) => response.status === "alreadyAdded"
).length;
if (addedCount > 0) {
showNotification(`${addedCount} streamer(s) added!`);
} else if (alreadyAddedCount === streamerNames.length) {
showNotification("All streamers were already added.");
} else {
showNotification(
"No new streamers added. Please refresh the page and ensure you're on the Channels tab."
);
}
})
.catch((error) => {
console.error("Error adding streamers:", error);
showNotification("An error occurred while adding streamers.");
});
}
// Function to show notification
function showNotification(message) {
browser.runtime.sendMessage({ type: "showNotification", content: message });
}
// Initialize the script
function init() {
updateAddAllStreamersButton();
addButtonToStreamerUsernames();
const observer = new MutationObserver(() => {
updateAddAllStreamersButton();
addButtonToStreamerUsernames();
});
observer.observe(document.body, { childList: true, subtree: true });
}
init();