-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
371 lines (293 loc) · 11.4 KB
/
Copy pathcontent.js
File metadata and controls
371 lines (293 loc) · 11.4 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Detect Simplify Internship Page and inform background script
const elements = document.querySelectorAll('.Box-sc-g0xbh4-0.bJMeLZ.js-snippet-clipboard-copy-unpositioned');
// Iterate over the found elements and override padding
elements.forEach(element => {
element.style.padding = '0px';
});
chrome.runtime.sendMessage({ action: 'detectSimplifyInternshipPage', detected: true });
// Function to find the job table in the page
function findJobTable() {
const tables = document.querySelectorAll('table');
for (const table of tables) {
const headers = table.querySelectorAll('thead th');
if (headers.length >= 5) {
const headerTexts = Array.from(headers).map(header => header.innerText.trim().toLowerCase());
if (
headerTexts.includes('company') &&
headerTexts.includes('role') &&
headerTexts.includes('location') &&
headerTexts.includes('application/link') &&
headerTexts.includes('date posted')
) {
// Add two more columns
const appliedHeader = document.createElement('th');
appliedHeader.innerText = 'Date Applied';
headers[headers.length - 1].insertAdjacentElement('afterend', appliedHeader);
const dateAppliedHeader = document.createElement('th');
dateAppliedHeader.innerText = 'Applied';
headers[headers.length - 1].insertAdjacentElement('afterend', dateAppliedHeader);
// Apply styles to the table
return table;
}
}
}
return null;
}
// Function to apply styles to the specified layout
function applyLayoutStyles() {
const layoutElement = document.querySelector('.Layout--flowRow-until-md.react-repos-overview-margin.Layout--sidebarPosition-end.Layout--sidebarPosition-flowRow-end');
if (layoutElement) {
const styleElement = document.createElement('style');
styleElement.innerHTML = `
@media (min-width: 1012px) {
.Layout {
--Layout-sidebar-width: 240px;
}
}
`;
document.head.appendChild(styleElement);
}
}
// Function to retrieve tracked jobs from localStorage
function getTrackedJobs() {
return JSON.parse(localStorage.getItem('trackedJobs')) || [];
}
// Function to inform the background script about a tracked job
function trackJob(job) {
try {
chrome.runtime.sendMessage({ action: 'jobTracked', job });
} catch (error) {
console.error('Error in trackJob:', error);
}
}
// Function to inform the background script about an untracked job
function untrackJob(job) {
try {
chrome.runtime.sendMessage({ action: 'jobUntracked', job });
} catch (error) {
console.error('Error in untrackJob:', error);
}
}
// Function to create tracking icons for jobs
// Function to create tracking icons for jobs
// Function to create tracking icons for jobs
// Function to create tracking icons for jobs
function createTrackingIcon(jobTitle, location, datePosted, trackedDate, isJobTracked, jobRow) {
// Icon container
const iconContainer = document.createElement('td');
iconContainer.classList.add('icon-container');
// Image container
const imageContainer = document.createElement('div');
imageContainer.classList.add('image-container');
const icon = document.createElement('img');
icon.alt = 'Track Job';
icon.classList.add('centered-icon');
icon.style.cursor = 'pointer';
// Set a fixed width and height for consistency
icon.style.width = '20px';
icon.style.height = '20px';
// Set color for tracked or untracked state
icon.style.filter = isJobTracked ? '' : 'grayscale(100%)';
// Change the image source based on the tracking state
icon.src = 'https://miro.medium.com/v2/resize:fit:512/1*nZ9VwHTLxAfNCuCjYAkajg.png';
// Date string container
const dateStringContainer = document.createElement('td');
dateStringContainer.classList.add('date-string-container');
icon.addEventListener('click', () => {
// Toggle the tracking state
isJobTracked = !isJobTracked;
// Change the image source dynamically after the click event
icon.style.filter = isJobTracked ? '' : 'grayscale(100%)';
// Update the tracking state for the next click
if (isJobTracked) {
const currentDate = new Date();
const formattedDate = `${currentDate.toLocaleDateString()} ${currentDate.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})}`;
trackJob({ title: jobTitle, location, datePosted, trackedDate: formattedDate });
console.log('Icon clicked! Job tracked.');
updateTrackingInfo(jobTitle, location, datePosted, formattedDate, isJobTracked, jobRow, dateStringContainer);
// Make the date string container visible when the job is tracked
dateStringContainer.style.display = '';
} else {
untrackJob({ title: jobTitle, location, datePosted });
console.log('Icon clicked! Job untracked.');
updateTrackingInfo(jobTitle, location, datePosted, '', isJobTracked, jobRow, dateStringContainer);
// Hide the date string container when the job is untracked
dateStringContainer.style.display = 'none';
}
});
imageContainer.appendChild(icon);
// Append image container to the icon container
iconContainer.appendChild(imageContainer);
// Append trackedDate information to the date string container
const trackedDateElement = document.createElement('div');
trackedDateElement.innerText = trackedDate ? formatDate(trackedDate) : '';
trackedDateElement.classList.add('tracked-date');
dateStringContainer.appendChild(trackedDateElement);
// Append both containers to the job row
jobRow.appendChild(iconContainer);
jobRow.appendChild(dateStringContainer);
}
// Function to apply tracking icons to job rows
function applyTrackingIcons() {
const jobTable = findJobTable();
if (!jobTable) {
console.log('Job table not found on the page.');
return;
}
const jobRows = jobTable.querySelectorAll('tbody > tr');
// Intersection Observer options
const options = {
threshold: 0.5, // Adjust as needed
};
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const jobRow = entry.target;
const jobTitleElement = jobRow.querySelector('td:nth-child(2)');
const locationElement = jobRow.querySelector('td:nth-child(3)');
const datePostedElement = jobRow.querySelector('td:nth-child(5)');
const jobTitle = jobTitleElement?.innerText || 'N/A';
const location = locationElement?.innerText || 'N/A';
const datePosted = datePostedElement?.innerText || 'N/A';
const trackedJobs = getTrackedJobs();
const trackedJob = trackedJobs.find(job => job.title === jobTitle && job.location === location);
const isJobTracked = !!trackedJob;
const trackedDate = trackedJob ? trackedJob.trackedDate : null;
createTrackingIcon(jobTitle, location, datePosted, trackedDate, isJobTracked, jobRow);
// Unobserve the row once the icon is added
observer.unobserve(jobRow);
}
});
}, options);
// Observe each job row
jobRows.forEach(jobRow => {
observer.observe(jobRow);
});
}
function updateTrackingInfo(jobTitle, location, datePosted, trackedDate, isJobTracked, jobRow, dateStringContainer) {
const iconContainer = jobRow.querySelector('.icon-container');
if (iconContainer && dateStringContainer) {
const icon = iconContainer.querySelector('img');
const trackedDateElement = dateStringContainer.querySelector('.tracked-date');
if (icon && trackedDateElement) {
// Set color for tracked or untracked state
icon.style.filter = isJobTracked ? '' : 'grayscale(100%)';
// Change the image source based on the tracking state
icon.src = isJobTracked
? 'https://miro.medium.com/v2/resize:fit:512/1*nZ9VwHTLxAfNCuCjYAkajg.png'
: 'https://miro.medium.com/v2/resize:fit:512/1*nZ9VwHTLxAfNCuCjYAkajg.png'; // Adjust the source URLs as needed
// Update trackedDate information
trackedDateElement.innerText = trackedDate ? formatDate(trackedDate) : '';
// Update "Date Applied" column
const dateAppliedElement = dateStringContainer.querySelector('.date-applied');
if (dateAppliedElement) {
const currentDate = new Date();
dateAppliedElement.innerText = isJobTracked ? formatDate(currentDate) : '';
}
// Update local storage
const updatedTrackedJobs = getTrackedJobs();
const existingJobIndex = updatedTrackedJobs.findIndex(
job => job.title === jobTitle && job.location === location
);
if (isJobTracked) {
const formattedDate = formatDate(new Date());
if (existingJobIndex !== -1) {
updatedTrackedJobs[existingJobIndex].trackedDate = formattedDate;
} else {
updatedTrackedJobs.push({ title: jobTitle, location, datePosted, trackedDate: formattedDate });
}
// Remove the "date-string-container" class
dateStringContainer.classList.remove('date-string-container');
} else {
if (existingJobIndex !== -1) {
updatedTrackedJobs.splice(existingJobIndex, 1);
}
// Add the "date-string-container" class back
dateStringContainer.classList.add('date-string-container');
}
localStorage.setItem('trackedJobs', JSON.stringify(updatedTrackedJobs));
}
}
}
// Function to format date without the year and time
function formatDate(dateTimeString) {
const date = new Date(dateTimeString);
// Format date
const formattedDate = date.toLocaleDateString(undefined, {
month: 'short',
day: 'numeric',
});
// Format time
const formattedTime = date.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
});
// Combine formatted date and time on the same line
const result = `${formattedDate} ${formattedTime}`.replace(/\r?\n|\r/g, ' ');
return result;
}
// Apply tracking icons when the content script is loaded
applyTrackingIcons();
// Apply styles to the specified layout
applyLayoutStyles();
// Listen for messages from the background script
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.action === 'clearTrackedJobs') {
clearTrackedJobs();
console.log('Message received: clearTrackedJobs');
sendResponse({ success: true }); // Send a response indicating success
}
});
function clearTrackedJobs() {
console.log('Clear tracked jobs function called.');
// Clear tracked jobs data from localStorage
localStorage.removeItem('trackedJobs');
console.log('Tracked jobs cleared.');
// Reload the page
location.reload();
// Trigger a message indicating that tracked jobs have been cleared
chrome.runtime.sendMessage({ action: 'trackedJobsCleared' });
}
// Add the following styles at the end of your content.js file
const styles = `
/* Add this style for the hover effect */
.image-container:hover img {
transform: scale(1.1); /* Adjust the scale factor as needed */
transition: transform 0.3s ease; /* Add a smooth transition effect */
}
/* Existing styles */
.centered-icon {
width: 20px;
height: 20px;
margin: auto;
display: block;
}
.icon-container {
height: 100px;
width: 100%;
border: none;
display: flex;
align-items: center;
}
.image-container img {
border: 0px;
height: 100%;
width: 60px;
margin-left: 10px;
}
.text-container {
margin-left: 100px;
}
.applied {
font-size: 12px;
}
.tracked-date {
font-size: 14px;
}
`;
const styleElement = document.createElement('style');
styleElement.innerHTML = styles;
document.head.appendChild(styleElement);