-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground-script.js
120 lines (113 loc) · 4.54 KB
/
background-script.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
const scrapy_cloud_api_url = 'https://app.zyte.com/api/jobs/list.json?'
const refresh_timeout = 2000
var jobs_status = {}
var notification_log = {}
var settings = {
'notification_reminder_time': 5,
}
var save_jobs_status = false
// Updating Job Status for all the Job IDs available in local storage
function updateJobsStatus() {
checkSavedAPIKey = browser.storage.local.get('apikey').then(object => object.apikey)
checkJobIDs = browser.storage.local.get('job_ids').then(object => object.job_ids ? object.job_ids : [])
checkJobsStatus = browser.storage.local.get('jobs_status').then(object => object.jobs_status)
Promise.all([checkSavedAPIKey, checkJobIDs, checkJobsStatus])
.then(result => {
apikey = result[0]
job_ids = result[1]
stored_jobs_status = result[2]
if (!jobs_status && stored_jobs_status) {
jobs_status = stored_jobs_status
}
if (apikey && job_ids.length) {
for (i = 0; i < job_ids.length; i++) {
var job_id = job_ids[i]
var project_id_regex = /(\d+)\/\d+\/\d+/i;
var project_id = job_id.match(project_id_regex)[1]
var api_url = scrapy_cloud_api_url + 'apikey=' + apikey + '&project=' + project_id + '&job=' + job_id
fetch(api_url)
.then(response => {
response.json()
.then(json => {
if (response.status == 200 && json.jobs.length) {
if (jobs_status[json.jobs[0].id]) {
Object.assign(jobs_status[json.jobs[0].id], JSON.parse(JSON.stringify(json.jobs[0])))
}
else {
jobs_status[json.jobs[0].id] = json.jobs[0]
}
save_jobs_status = true
}
})
})
.catch(error => {console.log(error)})
}
}
})
}
setInterval(updateJobsStatus, refresh_timeout)
// Saving Job Status to local storage
function saveJobsStatus() {
if (!save_jobs_status) { return }
browser.storage.local.set({'jobs_status': jobs_status})
}
setInterval(saveJobsStatus, refresh_timeout)
// Handling Messages coming from Popup
browser.runtime.onMessage.addListener(handleMessage)
function handleMessage(message) {
if (message.run_function == 'updateJobsStatus') {
updateJobsStatus()
}
if (message.run_function == 'updateComment') {
Object.assign(jobs_status[message.job_id], {'comment': message.comment})
saveJobsStatus()
}
}
function updateNotifications() {
// Sending Desktop notifications
for (let [key, value] of Object.entries(jobs_status)) {
if (!notification_log[key] && value.state == 'finished') {
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("icons/icon.png"),
"title": "Spider Sense",
"message": value.spider + " " + value.id + " has finished execution."
}).then(response=> {
notification_log[key] = new Date()
})
}
if (notification_log[key] && value.state == 'finished') {
now = new Date()
time_difference = (now.getTime() - notification_log[key].getTime()) / (1000 * 60 * 60)
if (time_difference > settings.notification_reminder_time) {
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("icons/icon.png"),
"title": "Spider Sense",
"message": value.spider + " " + value.id + " has finished execution."
}).then(response=> {
notification_log[key] = new Date()
})
}
}
}
}
setInterval(updateNotifications, refresh_timeout)
// Cleaning job ids which has been removed from the Popup
function cleanUpObjects() {
browser.storage.local.get('job_ids')
.then(response => {
console.log(response.job_ids)
for (let [key, value] of Object.entries(jobs_status)) {
if (!response.job_ids.includes(key)) {
delete jobs_status[key]
}
}
for (let [key, value] of Object.entries(notification_log)) {
if (!response.job_ids.includes(key)) {
delete notification_log[key]
}
}
})
}
setInterval(cleanUpObjects, refresh_timeout)