-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
206 lines (172 loc) · 7.72 KB
/
popup.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
// popup.js
document.addEventListener('DOMContentLoaded', function () {
loadDarkModeState(); // Load dark mode state
loadSubredditUI();
document.getElementById('addEnabledSubredditBtn').addEventListener('click', function () {
addEnabledSubreddits(); // Updated function to handle multiple subreddits
});
document.getElementById('editSubredditsBtn').addEventListener('click', function () {
toggleEditMode(); // Handle edit mode for the subreddits
});
document.getElementById('searchSubreddits').addEventListener('input', function () {
searchSubreddits(this.value); // Handle subreddit search
});
document.getElementById('toggleDarkModeBtn').addEventListener('click', function () {
toggleDarkMode(); // Handle dark mode toggle
});
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var currentUrl = tabs[0].url;
transformRedditPage(tabs[0].id, currentUrl);
});
});
// Add this function to save the dark mode state
function saveDarkModeState(isDarkMode) {
chrome.storage.sync.set({ darkMode: isDarkMode });
}
// Add this function to load the dark mode state
function loadDarkModeState() {
chrome.storage.sync.get({ darkMode: false }, function (data) {
if (data.darkMode) {
document.body.classList.remove('light-mode');
document.body.classList.add('dark-mode');
document.getElementById('toggleDarkModeBtn').src = 'icons/lightbulb48.png'; // Adjust this if you have a different icon for dark mode
} else {
document.body.classList.remove('dark-mode');
document.body.classList.add('light-mode');
document.getElementById('toggleDarkModeBtn').src = 'icons/lightbulb48.png'; // Adjust this if you have a different icon for light mode
}
});
}
function toggleDarkMode() {
const body = document.body;
const darkModeIcon = 'icons/lightbulb48.png'; // Assuming this is the dark mode icon
const lightModeIcon = 'icons/lightbulb48.png'; // Assuming this is the light mode icon
if (body.classList.contains('light-mode')) {
body.classList.remove('light-mode');
body.classList.add('dark-mode');
document.getElementById('toggleDarkModeBtn').src = darkModeIcon; // Change to dark mode icon
saveDarkModeState(true); // Save dark mode state
} else {
body.classList.remove('dark-mode');
body.classList.add('light-mode');
document.getElementById('toggleDarkModeBtn').src = lightModeIcon; // Change to light mode icon
saveDarkModeState(false); // Save light mode state
}
}
function loadSubredditUI() {
chrome.storage.sync.get({ enabledSubreddits: [] }, function (data) {
var enabledSubreddits = data.enabledSubreddits;
displaySubredditUI(enabledSubreddits);
});
}
function displaySubredditUI(enabledSubreddits) {
var enabledSubredditsTbody = document.getElementById('enabledSubreddits');
if (enabledSubredditsTbody) {
enabledSubredditsTbody.innerHTML = ""; // Clear the existing content
enabledSubreddits.forEach(function (subreddit) {
addSubredditRow(enabledSubredditsTbody, subreddit);
});
}
}
function addSubredditRow(container, subreddit) {
var row = document.createElement('tr');
var subredditCell = document.createElement('td');
subredditCell.textContent = subreddit;
subredditCell.className = 'px-4 py-2 border-main border-b';
var actionsCell = document.createElement('td');
actionsCell.className = 'px-4 py-2 border-main border-b';
var removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.className = 'remove-button';
removeButton.addEventListener('click', function () {
removeFromSubredditList(subreddit);
container.removeChild(row);
});
actionsCell.appendChild(removeButton);
row.appendChild(subredditCell);
row.appendChild(actionsCell);
container.appendChild(row);
}
function addEnabledSubreddits() {
var subredditInput = document.getElementById('addEnabledSubreddit');
if (subredditInput) {
var subreddits = subredditInput.value.trim().split(',').map(s => s.trim()).filter(Boolean);
if (subreddits.length > 0) {
chrome.storage.sync.get({ enabledSubreddits: [] }, function (data) {
var enabledSubreddits = data.enabledSubreddits;
subreddits.forEach(function (subreddit) {
if (!enabledSubreddits.includes(subreddit)) {
enabledSubreddits.push(subreddit);
}
});
chrome.storage.sync.set({ enabledSubreddits: enabledSubreddits }, function () {
subredditInput.value = ""; // Clear the input field
loadSubredditUI(); // Reload UI to display the updated list
});
});
}
}
}
function removeFromSubredditList(subreddit) {
chrome.storage.sync.get({ enabledSubreddits: [] }, function (data) {
var enabledSubreddits = data.enabledSubreddits.filter(function (item) {
return item !== subreddit;
});
chrome.storage.sync.set({ enabledSubreddits: enabledSubreddits });
});
}
function toggleEditMode() {
var editButton = document.getElementById('editSubredditsBtn');
var tableRows = document.querySelectorAll('#enabledSubreddits tr');
if (editButton.textContent === 'Edit') {
editButton.textContent = 'Save Changes';
tableRows.forEach(function (row) {
var subredditCell = row.children[0];
var subredditName = subredditCell.textContent;
subredditCell.innerHTML = `<input type="text" value="${subredditName}" class="w-full border-main border rounded-md py-1 px-2">`;
});
} else {
editButton.textContent = 'Edit';
var updatedSubreddits = [];
tableRows.forEach(function (row) {
var subredditInput = row.children[0].querySelector('input');
var subredditName = subredditInput.value.trim();
if (subredditName) {
updatedSubreddits.push(subredditName);
}
});
chrome.storage.sync.set({ enabledSubreddits: updatedSubreddits }, function () {
loadSubredditUI(); // Reload UI to display the updated list
});
}
}
function searchSubreddits(query) {
var tableRows = document.querySelectorAll('#enabledSubreddits tr');
tableRows.forEach(function (row) {
var subredditName = row.children[0].textContent.toLowerCase();
if (subredditName.includes(query.toLowerCase())) {
row.style.display = '';
} else {
row.style.display = 'none';
}
});
}
function transformRedditPage(tabId, url) {
if (url.includes('www.reddit.com')) {
var subredditName = getSubredditName(url);
chrome.storage.sync.get({ enabledSubreddits: [] }, function(data) {
var enabledSubreddits = data.enabledSubreddits || [];
if (isSubredditEnabled(subredditName, enabledSubreddits) && isRedditPostURL(url)) {
var modifiedUrl = 'read://https_www.reddit.com/?url=' + encodeURIComponent(url);
chrome.tabs.update(tabId, { url: modifiedUrl });
}
});
}
}
function getSubredditName(url) {
var match = url.match(/reddit\.com\/r\/([^/]+)/);
return match ? match[1] : null;
}
function isSubredditEnabled(subredditName, enabledSubreddits) {
return enabledSubreddits.includes(subredditName);
}