-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontent-script.js.in
604 lines (557 loc) · 19.7 KB
/
content-script.js.in
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
Copyright 2023 Jonathan Kamens.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
const cardClasses = ["jobs-search-results__list-item",
"jobs-job-board-list__item",
"discovery-templates-entity-item"];
const companyClasses = ["job-card-container__primary-description",
"job-card-container__company-name"];
const titleClasses = ["job-card-list__title"];
const locationClasses = ["job-card-container__metadata-item"];
const workplaceClasses = ["job-card-container__metadata-item--workplace-type"]
const privateButtonClass = "lijfhidebutton"
var titleRegexps, companyRegexps, locationRegexps, jobFilters, hideJobs;
function getButtonElement() {
utils.debug("getButtonElement");
var button = document.createElement("button");
button.setAttribute("class", privateButtonClass);
button.setAttribute("style", "background-color: #777;");
var image = document.createElement("img");
image.setAttribute("src", buttonIconURL);
image.setAttribute("title", "Hide without telling LinkedIn");
button.appendChild(image);
return button;
}
function findPrivateButton(elt) {
utils.debug(`findPrivateButton(${utils.id(elt)})`);
var elts = elt.querySelectorAll(`.${privateButtonClass}`);
if (elts.length) {
return elts[0];
}
return undefined;
}
function findDismissButton(elt) {
utils.debug(`findDismissButton(${utils.id(elt)})`);
var elts = elt.getElementsByTagName("button");
for (var button of elts) {
var label = button.getAttribute("aria-label");
if (label && (label.includes("Hide") || label.includes("Dismiss") ||
/^Mark .* with Not for me/.test(label))) {
return button;
}
}
return null;
}
function findUnhideButton(elt) {
utils.debug(`findUnhideButton(${utils.id(elt)})`);
var elts = elt.getElementsByTagName("button");
for (elt of elts)
if (elt.innerText == "Undo")
return elt;
return null;
}
function getLocation(elt) {
utils.debug(`getLocation(${utils.id(elt)})`);
var location = getClassValue(locationClasses, elt)
if (! location)
return false;
var workplace = getClassValue(workplaceClasses, elt)
if (workplace)
location = `${location} (${workplace})`;
return location;
}
function getJobSpec(elt) {
utils.debug(`getJobspec(${utils.id(elt)})`);
var spec = {
title: getClassValue(titleClasses, elt),
company: getClassValue(companyClasses, elt),
location: getLocation(elt)
};
if (! (spec.title && spec.company && spec.location))
return null;
return spec;
}
/*
State configurations:
* initial
* undetermined card visibility
* undetermined job visibility
* undetermined private hide button with listener
* undetermined listener on LinkedIn hide button
* undetermined undo button listener
* filtering:
* undetermined card visibility
* undetermined job visibility
* undetermined private hide button with listener
* undetermined listener on LinkedIn hide button
* undetermined undo button listener
* visible-pending:
* undetermined card visibility
* undetermined job visibility
* undetermined private hide button with listener
* undetermined listener on LinkedIn hide button
* undetermined undo button listener
* visible
* card is visible
* job is visible
* private hide button with listener
* listener on LinkedIn hide button
* no undo button listener
* private-hidden
* card is hidden
* job is visible on hidden card
* no private hide button / listener
* no listener on LinkedIn hide button
* no listener on undo button
* linkedin-hidden-pending
* undetermined card visibility
* undetermined job visibility
* undetermined hide button / listener
* undetermined on LinkedIn hide button
* undetermined listener on undo button
* linkedin-hidden
* card is visible
* job is not visible
* no private hide button / listener
* no listener on LinkedIn hide button
* listener on undo button
* linkedin-hidden-hidejobs
* card is hidden
* job is not visible on hidden card
* no private hide button / listener
* no listener on LinkedIn hide button
* no listener on undo button
State transitions:
initial:
* to filtering if jobSpec and dismiss button available
filtering:
* to visible-pending if no match and jobSpec and dismiss button available
* to private-hidden if private hide match
* to linkedin-hidden-pending if non-private hide match and not hideJobs
* first click LinkedIn dismiss button
* to linkedin-hidden-hidejobs if non-private hide match and hideJobs
* first click LinkedIn dismiss button
visible-pending:
* to visible when dismiss button and job spec become available
* remove job from job hide list if it's there
visible:
* to private-hidden if user clicks private hide button
* add private hide to options
* to linkedin-hidden-pending if user clicks LinkedIn hide button and not
hideJobs
* add non-private hide to options
* to linkedin-hidden-hidejobs if user clicks LinkedIn hide button and hideJobs
* add non-private hide to options
private-hidden:
None.
linkedin-hidden-pending:
* to linkedin-hidden when undo button becomes available
linkedin-hidden:
* to visible-pending if user clicks undo button
* remove hide from options
linkedin-hidden-hidejobs:
None.
*/
/*
Annoyingly, LinkedIn sometimes replaces a node when switching from
dismissed to not dismissed or vice versa. Wen need to keep an
internal cache to try to overcome these node changes.
*/
var cardCache = [];
function cardCacheMark() {
utils.debug("cardCacheMark()");
for (var i in cardCache)
cardCache[i].mark = 1;
}
function cardCacheCollect() {
utils.debug("cardCacheCollect()");
for (var i = cardCache.length - 1; i >= 0; i--)
if (cardCache[i].mark)
cardCache.splice(i, 1);
}
function cardCacheSet(elt, state, jobSpec) {
utils.debug(`cardCacheSet(${utils.id(elt)}, ${state}, ` +
`${JSON.stringify(jobSpec)})`);
var obj;
for (obj of cardCache)
if (obj.elt == elt) {
delete obj.mark;
obj.state = state;
if (! obj.jobSpec)
obj.jobSpec = jobSpec;
return;
}
if (jobSpec)
for (obj of cardCache)
if (utils.valuesAreEqual(obj.jobSpec, jobSpec)) {
delete obj.mark;
obj.elt = elt;
obj.state = state;
return;
}
obj = {elt: elt, jobSpec: jobSpec, state: state};
cardCache.push(obj);
}
function cardCacheGet(elt, jobSpec) {
utils.debug(`cardCacheGet(${utils.id(elt)}, ${JSON.stringify(jobSpec)})`);
var obj;
for (obj of cardCache)
if (obj.elt == elt) {
delete obj.mark;
return obj.state;
}
if (jobSpec) {
for (obj of cardCache)
if (obj.jobSpec && utils.valuesAreEqual(obj.jobSpec, jobSpec)) {
delete obj.mark;
obj.elt = elt;
return obj.state;
}
}
return undefined;
}
function getState(elt, jobSpec) {
utils.debug(`getState(${utils.id(elt)}, ${JSON.stringify(jobSpec)})`);
var attr = elt.getAttribute("lijfState");
var cache = cardCacheGet(elt, jobSpec);
var retval = attr || cache;
return retval;
}
function setState(elt, jobSpec, state) {
utils.debug(`setState(${utils.id(elt)}, ${JSON.stringify(jobSpec)}, ` +
`${state})`);
cardCacheSet(elt, jobSpec, state);
elt.setAttribute("lijfState", state);
}
function configureState(elt, jobSpec, state) {
utils.debug(`configureState(${utils.id(elt)}, ` +
`${JSON.stringify(jobSpec)}, ${state})`);
var jobVisible;
var cardVisible, privateButton, linkedinListener, undoListener;
if (state == "initial") {
cardVisible = undefined;
jobVisible = undefined;
privateButton = undefined;
linkedinListener = undefined;
undoListener = undefined;
}
else if (state == "filtering") {
cardVisible = undefined;
jobVisible = undefined;
privateButton = undefined;
linkedinListener = undefined;
undoListener = undefined;
}
else if (state == "visible-pending") {
cardVisible = undefined;
jobVisible = undefined;
privateButton = undefined;
linkedinListener = undefined;
undoListener = undefined;
}
else if (state == "visible") {
cardVisible = true;
jobVisible = true;
privateButton = true;
linkedinListener = true;
undoListener = false;
}
else if (state == "private-hidden") {
cardVisible = false;
jobVisible = true;
privateButton = false;
linkedinListener = false;
undoListener = false;
}
else if (state == "linkedin-hidden-pending") {
cardVisible = undefined;
jobVisible = undefined;
privateButton = undefined;
linkedinListener = undefined;
undoListener = undefined;
}
else if (state == "linkedin-hidden") {
cardVisible = true;
jobVisible = false;
privateButton = false;
linkedinListener = false;
undoListener = true;
}
else if (state == "linkedin-hidden-hidejobs") {
cardVisible = false;
// eslint-disable-next-line no-unused-vars
jobVisible = false;
privateButton = false;
linkedinListener = false;
undoListener = false;
}
else {
throw `Unknown state ${state}`;
}
setCardVisible(elt, jobSpec, cardVisible);
// LinkedIn handles job visibility, we don't touch it.
setPrivateButton(elt, jobSpec, privateButton);
setLinkedinListener(elt, jobSpec, linkedinListener);
setUndoListener(elt, jobSpec, undoListener);
setState(elt, jobSpec, state);
return state;
}
function setCardVisible(elt, jobSpec, visible) {
utils.debug(`setCardVisible(${utils.id(elt)}, ` +
`${JSON.stringify(jobSpec)}, ${visible})`);
if (visible == undefined)
return;
elt.hidden = !visible;
}
function setPrivateButton(elt, jobSpec, wanted) {
utils.debug(`setPrivateButton(${utils.id(elt)}, ` +
`${JSON.stringify(jobSpec)}, ${wanted})`);
if (wanted == undefined)
return;
var oldButton, button, listener;
oldButton = elt.lijfPrivateButton || undefined;
if (oldButton) {
listener = oldButton[1]
oldButton = oldButton[0]
}
button = findPrivateButton(elt) || undefined;
if (! wanted || (oldButton && oldButton != button)) {
if (oldButton) {
if (oldButton != button)
console.log(`setPrivateButton: mismatch ` +
`oldButton=${oldButton} vs button=${button}`);
oldButton.removeEventListener("click", listener);
delete elt.lijfPrivateButton;
oldButton = undefined;
}
if (button)
button.remove();
}
if (! wanted)
return;
if (oldButton)
return;
button = getButtonElement();
var reference = findDismissButton(elt);
var parent = reference.parentNode;
parent.insertBefore(button, reference.nextSibling);
listener = (event) => {
hideJob(jobSpec, true);
event.stopPropagation();
configureState(elt, jobSpec, "private-hidden");
};
button.addEventListener("click", listener);
elt.lijfPrivateButton = [button, listener];
}
function setLinkedinListener(elt, jobSpec, wanted) {
utils.debug(`setLinkedinListener(${utils.id(elt)}, ` +
`${JSON.stringify(jobSpec)}, ${wanted})`);
setListener(elt, jobSpec, wanted, "lijfDismissButton", findDismissButton,
() => {
hideJob(jobSpec);
configureState(elt, jobSpec, hideJobs ?
"linkedin-hidden-hidejobs" :
"linkedin-hidden-pending");
});
}
function setUndoListener(elt, jobSpec, wanted) {
utils.debug(`setUndoListener(${utils.id(elt)}, ` +
`${JSON.stringify(jobSpec)}, ${wanted})`);
setListener(elt, jobSpec, wanted, "lijfUndoButton", findUnhideButton,
() => configureState(elt, jobSpec, "visible-pending"));
}
function setListener(elt, jobSpec, wanted, property, finder, listener) {
utils.debug(`setListener(${utils.id(elt)}, ${JSON.stringify(jobSpec)}, `+
`${wanted}, ${property}, [finder], [listener])`);
if (wanted == undefined)
return;
var oldButton, oldListener, button;
oldButton = elt[property] || undefined;
if (oldButton) {
oldListener = oldButton[1]
oldButton = oldButton[0]
}
button = finder(elt) || undefined;
if (! wanted || (oldButton && oldButton != button)) {
if (oldButton) {
if (oldButton != button)
console.log(`setListener: mismatch ` +
`oldButton=${oldButton} vs button=${button}`);
oldButton.removeEventListener("click", oldListener);
delete elt[property];
oldButton = undefined;
}
}
if (! wanted)
return;
if (oldButton)
return;
button.addEventListener("click", listener);
elt[property] = [button, listener];
}
function filterOneJob(elt) {
utils.debug(`filterOneJob(${utils.id(elt)})`);
var jobSpec = getJobSpec(elt);
var state = getState(elt, jobSpec);
var button;
if (! state) {
/*
We don't call configureState here because we don't want to put an
"initial" state into the cache in case when we finally get the
jobSpec it turns out this is a card LinkedIn replaced and we
actually know what its state is.
*/
button = findDismissButton(elt);
if (! (jobSpec && button))
return;
state = configureState(elt, jobSpec, "filtering");
// fall through
}
if (state == "filtering") {
var result = (matches(jobSpec.title, titleRegexps) ||
matches(jobSpec.company, companyRegexps) ||
matches(jobSpec.location, locationRegexps) ||
jobMatches(jobSpec));
if (! result) {
state = configureState(elt, jobSpec, "visible-pending");
// fall through
}
else {
if (result == "private") {
state = configureState(elt, jobSpec, "private-hidden")
return;
}
findDismissButton(elt).click();
state = configureState(elt, jobSpec, hideJobs ?
"linkedin-hidden-hidejobs" :
"linkedin-hidden-pending");
return;
}
}
if (state == "visible-pending") {
button = findDismissButton(elt);
if (! (jobSpec && button)) // not ready yet
return;
unhideJob(elt, jobSpec);
state = configureState(elt, jobSpec, "visible");
return;
}
if (state == "linkedin-hidden-pending") {
button = findUnhideButton(elt)
if (! button)
return;
state = configureState(elt, jobSpec, "linkedin-hidden");
return;
}
}
function matches(fieldValue, regexps) {
utils.debug(`matches(${fieldValue}, ${regexps})`);
if (! fieldValue) return false;
return regexps.some(r => r.test(fieldValue));
}
function getClassValue(classes, elt) {
utils.debug(`getClassValue(${classes}, ${utils.id(elt)})`);
var selector = classes.map(c => `.${c}`).join(", ");
var elts = elt.querySelectorAll(selector);
if (elts.length == 0) return false;
return elts[0].innerText;
}
function jobMatches(jobSpec) {
utils.debug(`jobMatches(${JSON.stringify(jobSpec)})`);
for (var filter of jobFilters) {
if (filter.title != jobSpec.title ||
filter.company != jobSpec.company ||
filter.location != jobSpec.location)
continue;
return filter.private ? "private" : "linkedin";
}
return false;
}
function hideJob(jobSpec, isPrivate) {
utils.debug(`hideJob(${JSON.stringify(jobSpec)}, ${isPrivate})`);
// Don't list a job explicitly if it's already filtered by the regular
// expressions, presumably because the user just edited them to include it,
// or if it's already listed. This could happen if user hides a job and then
// unhides it and then we detect the DOM change and scan the job again,
// generating an artificial click event which causes this function to be
// called. (This shouldn't happen anymore with recent code improvements but
// I'm leaving this in as a precautionary measure.)
if (matches(jobSpec.title, titleRegexps) ||
matches(jobSpec.company, companyRegexps) ||
matches(jobSpec.location, locationRegexps) ||
jobMatches(jobSpec))
return;
if (isPrivate) {
jobSpec = structuredClone(jobSpec);
// 1 instead of true because shorter stringification
jobSpec.private = 1;
}
jobFilters.unshift(jobSpec);
utils.saveListToStorage("jobFilters", jobFilters).then();
}
function unhideJob(elt, jobSpec) {
utils.debug(`unhideJob(${utils.id(elt)}, ${JSON.stringify(jobSpec)})`);
// Find the job details, check if they're in jobFilters, and remove them
// if so.
var changed = false;
for (var i = jobFilters.length - 1; i >= 0; i--)
if (utils.valuesAreEqual(jobFilters[i], jobSpec)) {
changed = true;
jobFilters.splice(i, 1);
}
if (changed)
utils.saveListToStorage("jobFilters", jobFilters).then();
return changed;
}
var filterTimeout;
function filterEverything() {
utils.debug("filterEverything()");
cardCacheMark();
if (filterTimeout)
clearTimeout(filterTimeout);
filterTimeout = setTimeout(cardCacheCollect, 5000);
var elts = document.querySelectorAll(
cardClasses.map(c => `.${c}`).join(", "));
for (var elt of elts) {
filterOneJob(elt);
}
}
async function loadOptions() {
utils.debug("loadOptions()");
titleRegexps = utils.compileRegexps(
await utils.readListFromStorage("titleRegexps"));
companyRegexps = utils.compileRegexps(
await utils.readListFromStorage("companyRegexps"));
locationRegexps = utils.compileRegexps(
await utils.readListFromStorage("locationRegexps"));
jobFilters = await utils.readListFromStorage("jobFilters");
var options = await chrome.storage.sync.get(["hideJobs"]);
hideJobs = options["hideJobs"];
}
var topObserver = null;
async function createTopObserver() {
utils.debug("createTopObserver()");
var config = {childList: true, subtree: true};
// eslint-disable-next-line no-unused-vars
var callback = (mutationList, observer) => {
filterEverything();
};
topObserver = new MutationObserver(callback);
topObserver.observe(document.body, config);
filterEverything();
}
loadOptions()
.then(() => createTopObserver());
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace != "sync") return;
loadOptions();
});