forked from rpeng220/kaleidoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
171 lines (131 loc) · 4.12 KB
/
background.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
function genericOnClick(info, tab) {
chrome.storage.local.get(['resume'], function(result) {
if (typeof result === "undefined" || typeof result.resume === "undefined")
{
createTab();
}
else
{
}
});
}
function createTab() {
chrome.windows.getCurrent(function call(window) {
var url = chrome.extension.getURL("index.html");
var args = {
'index': 0,
'windowId': window.id,
'url': url,
'selected': true
}
try {
chrome.tabs.create(args);
} catch (e) {
alert(e);
}});
}
function sendResume() {
var applyUserId = "";
chrome.storage.local.get(['applyUserId'], function(result) {
applyUserId = result.applyUserId;
});
chrome.storage.local.get(['resume'], function(result) {
var resumeObj = {"userId" : applyUserId, "resume" : result.resume};
sendData(JSON.stringify(resumeObj));
});
}
function sendApplication() {
}
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({"title": "Kumquat", "id": "parent"});
chrome.contextMenus.create(
{"title": "Profile", "parentId": "parent", "id": "Profile", });
// chrome.contextMenus.create({"title": "Autofill", "parentId": "parent", "id": "Autofill"});
// chrome.contextMenus.onClicked.addListener(function(info, tab) {
// if (info.menuItemID == "Autofill") {
// chrome.tabs.sendMessage({message: 'autofill'});
// }
// });
generateUserId();
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.MESSEGE_TYPE === RESUME_MES) {
sendResume();
}
else if (request.MESSEGE_TYPE === JOB_URL_MES) {
sendData(sender.tab.url);
}
else if (request.MESSEGE_TYPE === APPLICATION_MES) {
sendApplication();
}
});
});
function randomstring(L) {
var s = '';
var randomchar = function() {
var n = Math.floor(Math.random() * 62);
if (n < 10) return n; //1-10
if (n < 36) return String.fromCharCode(n + 55); //A-Z
return String.fromCharCode(n + 61); //a-z
}
while (s.length < L) s += randomchar();
return s;
}
function generateUserId() {
var userId = randomstring(16);
var obj= {};
obj[USER_ID] = userId;
chrome.storage.sync.set(obj, function() {
});
var data = {[USER_ID] : userId};
sendData(USER_ID, data);
}
function sendData(method, data) {
var x = new XMLHttpRequest();
var url = WEBURL+ "/" + method;
x.open('POST', url, true);
x.onload = function() {
// alert(x.responseText);
};
x.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
var jsonStr = JSON.stringify(data);
x.send(jsonStr);
}
function sendUrl(url) {
var applyUserId = "";
chrome.storage.sync.get(USER_ID, function(result) {
applyUserId = result[USER_ID];
var data = {[USER_ID] : applyUserId, [JOB_URL_MES] : url};
sendData(JOB_URL_MES, data);
});
}
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.onClicked.addListener(genericOnClick);
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status === 'complete' && tab.url !== "undefined" &&
tab.url !== "chrome://newtab/")
{
var flag = tab.url.includes("lever.co") || tab.url.includes("taleo") || tab.url.includes("myworkdayjobs") || tab.url.includes("greenhouse");
if (flag)
{
sendUrl(tab.url);
}
}
});
generateUserId();
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.MESSEGE_TYPE === RESUME_MES) {
sendResume();
}
else if (request.MESSEGE_TYPE === JOB_URL_MES) {
sendData(sender.tab.url);
}
else if (request.MESSEGE_TYPE === APPLICATION_MES) {
sendApplication();
}
});
});
chrome.runtime.onMessage.addListener(data => {
if (data.type === 'notification') {
chrome.notifications.create('', data.options);
}
});