-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
291 lines (214 loc) · 7.79 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict';
var query = { active: true, currentWindow: true};
var btn;
var go_to_url;
var cpt = [];
var general_opt = [];
var context_opt = [];
// get current domain
chrome.tabs.query(query, function (tabs) {
if (tabs.length > 0) {
var url = tabs[0].url;
var patt = '^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)';
var domain = url.match(patt);
go_to_url = domain[0];
}
});
// Set buttons
chrome.storage.sync.get('wp_buttons', function(data) {
if(data.wp_buttons) {
btn = data.wp_buttons;
} else {
btn = {};
}
const buttons = Object.entries(btn);
if(Object.keys(btn).length == 0) {
chrome.storage.sync.set({ wp_buttons: btn });
document.getElementById("load_btns").innerHTML = "<p>No buttons found - add new button below.</p>";
} else {
document.getElementById("load_btns").innerHTML = '';
}
for (const [title, url] of buttons) {
document.getElementById("load_btns").innerHTML += `<button class="open-link" data-url="${url}">${title}</button><button class="remove-link" data-remove="${title}">X</button>`;
}
});
// Set custom post type list
chrome.storage.sync.get('wp_cpts', function(data) {
if(data.wp_cpts) {
cpt = data.wp_cpts;
} else {
cpt = [];
}
if(cpt.length == 0) {
document.getElementById("load_cpts").innerHTML = "<p>No custom post types found - add new CPTs below.</p>";
} else {
document.getElementById("load_cpts").innerHTML = '<br>';
}
var i;
for (i = 0; i < cpt.length; i++) {
document.getElementById("load_cpts").innerHTML += '<button disabled>'+cpt[i]+'</button><button class="remove-cpt" data-remove="'+cpt[i]+'">X</button>';
}
});
// Function to save the checkbox state to Chrome Storage
function saveCheckboxState() {
const checkbox = document.getElementById('hide_front_bar');
const isChecked = checkbox.checked;
chrome.storage.sync.set({ 'hide_front_bar': isChecked }, function () {
//console.log('Checkbox state saved');
});
}
// Function to load the checkbox state from Chrome Storage
function loadCheckboxState() {
const checkbox = document.getElementById('hide_front_bar');
chrome.storage.sync.get('hide_front_bar', function (data) {
const isChecked = data['hide_front_bar'];
checkbox.checked = isChecked;
//console.log('Checkbox state loaded');
});
}
function testChanges() {
//console.log(chrome.tabs);
//const body = document.body;
//body.style.backgroundColor = checkbox.checked ? 'green' : 'red';
// Send a message to content.js
const isChecked = checkbox.checked;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, { toggleContent: isChecked });
});
}
// Function to update CSS based on checkbox state
async function updateCSS() {
let [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
if (checkbox.checked) {
//console.log("checked");
try {
await chrome.scripting.insertCSS({
target: {
tabId: tab.id,
},
files: ["style.css"],
});
} catch (err) {
console.error(`failed to insert option 1 CSS: ${err}`);
}
} else {
//console.log("NOT CHECKED");
try {
await chrome.scripting.removeCSS({
target: {
tabId: tab.id,
},
files: ["style.css"],
});
} catch (err) {
console.error(`failed to remove option 1 CSS: ${err}`);
}
}
}
// Event listener for when the checkbox is changed
const checkbox = document.getElementById('hide_front_bar');
checkbox.addEventListener('change', saveCheckboxState);
checkbox.addEventListener('change', testChanges); // Add an event listener to the checkbox for click events
checkbox.addEventListener("click", updateCSS);
window.addEventListener("load", updateCSS); // Call the updateCSS function when the page loads
loadCheckboxState(); // Load the initial state of the checkbox when the popup is opened
// Set context options checkbox list
chrome.storage.sync.get('wp_context_opts', function(data) {
if(data.wp_context_opts) {
context_opt = data.wp_context_opts;
} else {
context_opt = [];
}
var i;
for (i = 0; i < context_opt.length; i++) {
document.getElementById(context_opt[i]).checked = true;
}
});
document.addEventListener('DOMContentLoaded', function (event) {
// Save button trigger
document.getElementById("saveButton").addEventListener("click", function() {
var key = document.getElementById("new_button_title").value;
var value = document.getElementById("new_button_url").value;
if(key != '' && value != '') {
btn[key] = value;
chrome.storage.sync.set({ wp_buttons: btn });
window.location.reload();
}
});
// Save CPT trigger
document.getElementById("saveCPT").addEventListener("click", function() {
var new_type = document.getElementById("new_cpt").value;
if(new_type != '') {
cpt.push(new_type);
chrome.storage.sync.set({ wp_cpts: cpt });
window.location.reload();
}
});
// Remove button trigger
document.addEventListener('click', function (event) {
if (!event.target.matches('.remove-link')) return; // If clicked element doesn't have the right selector, bail
var msg = confirm("Remove Button?");
if (msg == true) {
chrome.storage.sync.get('wp_buttons', function(data) {
var key = event.target.dataset.remove;
var btn_array = data.wp_buttons;
delete btn_array[key]
chrome.storage.sync.set({ wp_buttons: btn_array });
});
window.location.reload();
} else {
window.location.reload();
}
}, false);
// Remove CPT trigger
document.addEventListener('click', function (event) {
if (!event.target.matches('.remove-cpt')) return; // If clicked element doesn't have the right selector, bail
var msg = confirm("Remove Custom Post Type?");
if (msg == true) {
chrome.storage.sync.get('wp_cpts', function(data) {
var type = event.target.dataset.remove;
var cpt_array = data.wp_cpts.filter(function(e) { return e !== type })
chrome.storage.sync.set({ wp_cpts: cpt_array });
});
window.location.reload();
} else {
window.location.reload();
}
}, false);
// Open link trigger
document.addEventListener('click', function (event) {
if (!event.target.matches('.open-link')) return; // If clicked element doesn't have the right selector, bail
var path = event.srcElement.dataset.url;
var link = go_to_url+path;
window.open(link, '_blank');
}, false);
// Save options trigger
document.addEventListener('click', function (event) {
// General Options
if(event.target.className == 'general-checkboxes' && event.target.checked == true) { // Add to opt
general_opt.push(event.target.value);
chrome.storage.sync.set({ wp_gen_opts: general_opt });
} else if(event.target.className == 'general-checkboxes' && event.target.checked == false) { // Remove from opt
chrome.storage.sync.get('wp_gen_opts', function(data) {
var unchecked = event.target.value;
var gen_opt_array = data.wp_gen_opts.filter(function(e) { return e !== unchecked })
chrome.storage.sync.set({ wp_gen_opts: gen_opt_array });
});
}
// Context Menu Options
if(event.target.className == 'context-checkboxes' && event.target.checked == true) { // Add to opt
context_opt.push(event.target.value);
chrome.storage.sync.set({ wp_context_opts: context_opt });
} else if(event.target.className == 'context-checkboxes' && event.target.checked == false) { // Remove from opt
chrome.storage.sync.get('wp_context_opts', function(data) {
var unchecked = event.target.value;
var opt_array = data.wp_context_opts.filter(function(e) { return e !== unchecked })
chrome.storage.sync.set({ wp_context_opts: opt_array });
});
}
});
});