-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoptions.js
233 lines (201 loc) · 9.99 KB
/
options.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
document.addEventListener('DOMContentLoaded', function() {
// Load stored properties
loadProperties();
// Toggle expand/collapse for social sections
document.querySelectorAll('.social-title').forEach(title => {
title.addEventListener('click', function() {
let platform = this.getAttribute('data-toggle');
display_name = platform.charAt(0).toUpperCase() + platform.slice(1);
let list = document.getElementById(`${platform}-list`);
let content = document.getElementById(`${platform}-content`);
if (list.style.display === "none") {
list.style.display = "block";
content.style.display = "block";
title.textContent = `${display_name} ▼`;
} else {
list.style.display = "none";
content.style.display = "none";
title.textContent = `${display_name} ►`;
}
});
});
// Add new user property
document.querySelectorAll('.add-button').forEach(button => {
button.addEventListener('click', function() {
let input = this.previousElementSibling;
let property = input.value.trim();
let platform = this.closest('.social-section').querySelector('.social-title').getAttribute('data-toggle');
let list = document.getElementById(`${platform}-list`);
if (property) {
addPropertyToList(list, property, true);
input.value = '';
saveProperties(platform, list);
}
});
});
});
function addPropertyToList(list, property, isUserItem) {
let li = document.createElement('li');
if (isUserItem) {
li.className = "user-item";
li.innerHTML = `
<button class="delete-button">\u2715</button>
<span class="filter-description">${property}</span>
<button class="toggle-button" data-state="On"></button>
`;
li.querySelector('.delete-button').addEventListener('click', function() {
this.closest('li').remove();
let platform = list.closest('.social-section').querySelector('.social-title').getAttribute('data-toggle');
saveProperties(platform, list);
});
} else {
li.innerHTML = `
<span class="filter-description">${property}</span>
<button class="toggle-button" data-state="On"></button>
`;
}
li.querySelector('.toggle-button').addEventListener('click', function() {
if (this.getAttribute('data-state') === "On") {
this.setAttribute('data-state', 'Off');
this.style.backgroundColor = "#676869"; // Gray color for "Off" state
} else {
this.setAttribute('data-state', 'On');
this.style.backgroundColor = "#0077b6"; // Blue color for "On" state
}
let platform = list.closest('.social-section').querySelector('.social-title').getAttribute('data-toggle');
saveProperties(platform, list);
});
list.appendChild(li);
}
function saveProperties(platform, list) {
let properties = Array.from(list.children).map(li => {
return {
value: li.querySelector('span').textContent,
isUserItem: li.classList.contains('user-item'),
state: li.querySelector('.toggle-button').getAttribute('data-state')
};
});
chrome.storage.sync.set({ [platform]: properties }, function() {
console.log(`Saved properties for ${platform}`);
});
}
function loadProperties() {
chrome.storage.sync.get(null, function(items) {
console.log(items);
// if items is empty, call resetToDefaults()
if (Object.keys(items).length === 0 && items.constructor === Object) {
resetToDefaults();
}
for (let platform in items) {
let list = document.getElementById(`${platform}-list`);
if (list == null){
continue;
}
console.log(list);
items[platform].forEach(propertyObj => {
addPropertyToList(list, propertyObj.value, propertyObj.isUserItem);
let li = list.lastElementChild;
let toggleBtn = li.querySelector('.toggle-button');
if (propertyObj.state === 'Off') {
toggleBtn.textContent = "";
toggleBtn.style.backgroundColor = "#676869";
} else {
toggleBtn.textContent = "";
toggleBtn.style.backgroundColor = "#0077b6";
}
toggleBtn.setAttribute('data-state', propertyObj.state);
});
}
});
}
function resetToDefaults() {
// Define the default properties for each platform
const defaultProperties = {
'linkedin': [
{ value: 'It\'s an announcement ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a self-improvement post (time-management, productivity hacks, how to socialize, etc.) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a career update (new job or promotion, job anniversary, resignation or retirement, etc.) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a news article ', isUserItem: false, state: 'Off' },
{ value: 'It\'s header mentions that it\'s a promoted post', isUserItem: false, state: 'Off' },
{ value: 'It\'s a job posting ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a flyer for networking or an events (hackathon, mixer, dinner, meetup, etc. ) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a scientific or research update (new paper, research findings, tech breakthrough, etc. ) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a book recommendation or review ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a flyer for a professional development program (webinar, online course, degree program) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a celebration post (winning an award or achieving something) ', isUserItem: false, state: 'Off' },
// ... Add other LinkedIn defaults here
],
'twitter': [
{ value: 'It\'s Joke or funny tweet', isUserItem: false, state: 'Off' },
{ value: 'It\'s about Joe Biden or Donald Trump', isUserItem: false, state: 'Off' },
{ value: 'It\'s about hollywood stars', isUserItem: false, state: 'Off' },
{ value: 'It\'s an announcement ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a self-improvement post (time-management, productivity hacks, how to socialize, etc.) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a career update (new job or promotion, job anniversary, resignation or retirement, etc.) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a news article ', isUserItem: false, state: 'Off' },
{ value: 'It\'s header mentions that it\'s an Advertisement', isUserItem: false, state: 'Off' },
{ value: 'It\'s a job posting ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a flyer for networking or an events (hackathon, mixer, dinner, meetup, etc. ) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a scientific or research update (new paper, research findings, tech breakthrough, etc. ) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a book recommendation or review ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a flyer for a professional development program (webinar, online course, degree program) ', isUserItem: false, state: 'Off' },
{ value: 'It\'s a celebration post (winning an award or achieving something) ', isUserItem: false, state: 'Off' },
// ... Add other Twitter defaults here
]
// ... Add defaults for other platforms
};
// Reset options in the UI
for (let platform in defaultProperties) {
let list = document.getElementById(`${platform}-list`);
// Clear current items
while (list.firstChild) {
list.removeChild(list.lastChild);
}
// Add default properties
defaultProperties[platform].forEach(propertyObj => {
addPropertyToList(list, propertyObj.value, propertyObj.isUserItem);
let li = list.lastElementChild;
if (propertyObj.state === 'Off') {
let toggleBtn = li.querySelector('.toggle-button');
toggleBtn.textContent = "";
toggleBtn.style.backgroundColor = "#676869";
toggleBtn.setAttribute('data-state', 'Off');
}
});
}
// Save defaults to storage
chrome.storage.sync.set(defaultProperties, function() {
console.log('Restored default properties.');
});
}
document.getElementById('reset-btn').addEventListener('click', resetToDefaults);
document.addEventListener("DOMContentLoaded", function() {
// Load the saved API key when the page is loaded
loadAPIKey();
// Save the API key when the 'Save' button is clicked
document.getElementById("api-key-save-btn").addEventListener("click", function() {
saveAPIKey();
});
// The rest of your JS event listeners and functions go here...
});
function loadAPIKey() {
chrome.storage.sync.get("openai_api_key", function(data) {
if (data.openai_api_key) {
document.getElementById("api-key-input").value = data.openai_api_key;
}
});
}
function saveAPIKey() {
let apiKey = document.getElementById("api-key-input").value;
if (apiKey) {
chrome.storage.sync.set({ "openai_api_key": apiKey }, function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else {
alert("API Key saved successfully!");
}
});
} else {
alert("Please enter an API Key before saving.");
}
}