-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
109 lines (98 loc) · 3.38 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
$(document).ready(() => {
// check if popup in company or job page
chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
let url = tabs[0].url;
const companyPageRegex104 = new RegExp('https://www.104.com.tw/company/', 'i');
const jobPageRegex104 = new RegExp('https://www.104.com.tw/job/', 'i');
let cp = url.match(companyPageRegex104);
let jp = url.match(jobPageRegex104);
if (cp) {
chrome.tabs.sendMessage(tabs[0].id, { todo: 'getCompanyInfo', selector: '.h1:first' }, fillInPopup);
} else if (jp) {
chrome.tabs.sendMessage(tabs[0].id, { todo: 'getCompanyInfo', selector: 'a[data-gtm-head=公司名稱]' }, fillInPopup);
// in job page, but we need companyURL
chrome.tabs.sendMessage(tabs[0].id, { todo: 'getCompanyURL', selector: 'a[data-gtm-head=公司名稱]' }, (companyURL) => {
url = companyURL;
});
} else {
// not in company or job page, disabled UI
$('input, textarea, button').attr('disabled', true);
$('button[type="submit"]').text('請在公司或職缺頁面新增/修改Note');
$('button[type="submit"]').removeClass('btn-primary');
$('button[type="submit"]').addClass('btn-warning');
}
function fillInPopup(res) {
let companyName = Object.keys(res)[0];
let companyInfo = res[companyName];
$('#companyName').val(companyName);
$('#companyURL').val(url);
if (companyInfo != undefined) {
let rating = 6 - companyInfo.rating;
$('#note').text(companyInfo.note);
$('#star' + rating).prop('checked', true);
}
}
});
// add or update note
$('#noteForm').submit(() => {
let companyName = $('#companyName').val().trim();
let companyURL = $('#companyURL').val();
let note = $('#note').val();
let rating = $('input[name="star"]:checked').val();
if (!rating) {
$('#invalid-feedback-rating').show();
return false;
}
rating = -rating + 6; // because the rating bar is {flex-direction: row-reverse;}
let companyInfo = {
"companyURL": companyURL,
"note": note,
"rating": rating
};
chrome.storage.local.set({ [companyName]: companyInfo });
window.close();
});
// clear rating error msg
$('input[type=radio]').on('change', () => {
let rating = $('input[name="star"]:checked').val();
if (rating) {
$('#invalid-feedback-rating').css('display', 'none');
}
});
});
/**
* Temporary workaround for secondary monitors on MacOS where redraws don't happen
* @See https://bugs.chromium.org/p/chromium/issues/detail?id=971701
*/
if (
// From testing the following conditions seem to indicate that the popup was opened on a secondary monitor
window.screenLeft < 0 ||
window.screenTop < 0 ||
window.screenLeft > window.screen.width ||
window.screenTop > window.screen.height
) {
chrome.runtime.getPlatformInfo((info) => {
if (info.os === 'mac') {
const fontFaceSheet = new CSSStyleSheet()
fontFaceSheet.insertRule(`
@keyframes redraw {
0% {
opacity: 1;
}
100% {
opacity: .99;
}
}
`)
fontFaceSheet.insertRule(`
html {
animation: redraw 1s linear infinite;
}
`)
document.adoptedStyleSheets = [
...document.adoptedStyleSheets,
fontFaceSheet,
]
}
})
}