-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbackground.ts
114 lines (99 loc) · 3.07 KB
/
background.ts
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
import { DocumentData, findSvg } from 'svg-gobbler-scripts'
import Chrome from './src/utils/chrome-utils'
/**
* Functions related to initializing the extension. This includes setting the
* extension icons and launching the onboarding experience.
*/
class Background {
/**
* Sets the uninstall URL for the extension.
*/
static handleUninstall() {
chrome.runtime.setUninstallURL('https://svggobbler.com/uninstall')
}
/**
* Initializes the extension.
*/
static init() {
Background.setExtensionIcons()
Background.launchOnboardingExperience()
Background.launchExtensionFromOnboarding()
Background.launchSvgGobbler()
Background.handleUninstall()
}
/**
* Launches the extension from the onboarding page.
*/
static launchExtensionFromOnboarding() {
chrome.runtime.onMessage.addListener(async function onboardingListener(req) {
const { data, type } = req
if (type === 'launch-svg-gobbler-from-onboarding') {
chrome.runtime.onMessage.addListener(function listener(req, __, sendResponse) {
if (req === 'gobble') {
sendResponse({ data })
chrome.runtime.onMessage.removeListener(listener)
}
})
await Chrome.createNewTab()
}
})
}
/**
* If the extension is installed for the first time, open the onboarding page.
*/
static launchOnboardingExperience() {
chrome.runtime.onInstalled.addListener(async (details) => {
if (details.reason === 'install') {
await Chrome.createNewTab('onboarding.html')
}
})
}
/**
* Initializes SVG Gobbler conditionally based on the type of page the user is
* currently on. Responsible for getting data from the active tab and sending
* it to the content script.
*/
static launchSvgGobbler() {
const onClickHandler = async () => {
let data = {
data: [],
host: 'Collection',
href: '',
origin: '',
} as DocumentData
const activeTab = await Chrome.getActiveTab()
// Check if the active tab is a system page
if (!activeTab.url?.includes('chrome://')) {
data = await Chrome.executeScript(activeTab.id!, findSvg)
}
// Add a listener
chrome.runtime.onMessage.addListener(function listener(req, __, sendResponse) {
if (req === 'gobble') {
sendResponse({ data })
chrome.runtime.onMessage.removeListener(listener)
}
})
// Open the SVG Gobbler page
await Chrome.createNewTab()
}
chrome.action.onClicked.addListener(onClickHandler)
}
/**
* Load the development icon if the extension is running in development mode.
*/
static setExtensionIcons() {
if (!('update_url' in chrome.runtime.getManifest())) {
// The extension is running as an unpacked extension
chrome.action.setIcon({
path: {
'16': 'assets/dev/16.png',
'24': 'assets/dev/24.png',
'32': 'assets/dev/32.png',
'48': 'assets/dev/48.png',
'64': 'assets/dev/64.png',
},
})
}
}
}
Background.init()