-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
88 lines (76 loc) · 2.18 KB
/
Copy pathbackground.js
File metadata and controls
88 lines (76 loc) · 2.18 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
/* global chrome, fetch */
const LAMBDA_URL = 'https://4uw1u63j59.execute-api.eu-west-1.amazonaws.com/production'
const REWARDS_URL = 'https://shift.gearboxsoftware.com/rewards'
function getURL () {
return new Promise((resolve, reject) => {
chrome.storage.sync.get({ url: LAMBDA_URL }, (items) => resolve(items.url))
})
}
function copyToClipboard (text) {
const textarea = document.createElement('textarea')
document.body.appendChild(textarea)
textarea.value = text
textarea.focus()
textarea.select()
document.execCommand('copy')
document.body.removeChild(textarea)
}
function checkLastCode (code) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get({ code: null }, (storage) => {
if (code === storage.code) {
reject(new Error('No new code found!'))
} else {
chrome.storage.sync.set({ code }, () => resolve(code))
}
})
})
}
function setIcon (icon) {
chrome.browserAction.setIcon({ path: iconPaths(icon) })
// Reset the icon after 2,5 seconds
setTimeout(() => chrome.browserAction.setIcon({ path: iconPaths('icon') }), 2500)
}
function iconPaths (icon) {
return {
16: `/assets/${icon}_16.png`,
32: `/assets/${icon}_32.png`,
48: `/assets/${icon}_48.png`,
128: `/assets/${icon}_128.png`
}
}
function createNotification (code) {
const notification = {
type: 'basic',
iconUrl: '/assets/icon_128.png',
title: 'Borderlands SHiFT Code',
message: 'A new SHiFT code is available!'
}
chrome.notifications.create(notification)
chrome.notifications.onClicked.addListener(() => {
chrome.tabs.create({ url: REWARDS_URL }, () => {
copyToClipboard(code)
setIcon('yes')
})
})
}
chrome.browserAction.onClicked.addListener(() => {
getURL()
.then(fetch)
.then((response) => response.json())
.then(copyToClipboard)
.then(() => setIcon('yes'))
.catch(() => setIcon('nope'))
})
chrome.alarms.create('checkForNewCode', {
when: Date.now() + 1000,
periodInMinutes: 60
})
chrome.alarms.onAlarm.addListener((alarm) => {
getURL()
.then(fetch)
.then((response) => response.json())
.then(checkLastCode)
.then(createNotification)
.catch(console.warn)
})