forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
42 lines (38 loc) · 1.48 KB
/
Copy pathindex.ts
File metadata and controls
42 lines (38 loc) · 1.48 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
import { locale } from '@/i18n'
import { onInstalled, setUninstallURL } from "@api/chrome/runtime"
import { executeScript } from "@api/chrome/script"
import { createTabAfterCurrent, listTabs } from "@api/chrome/tab"
import { updateInstallTime } from "@service/meta-service"
import { IS_E2E, IS_FROM_STORE } from "@util/constant/environment"
import { getGuidePageUrl, UNINSTALL_QUESTIONNAIRE } from "@util/constant/url"
import { isBrowserUrl } from "@util/pattern"
import versionManager from './version'
async function onFirstInstall() {
updateInstallTime(Date.now())
!IS_E2E && createTabAfterCurrent(getGuidePageUrl())
}
async function reloadContentScript() {
const files = chrome.runtime.getManifest().content_scripts?.[0]?.js
if (!files?.length) return
const tabs = await listTabs()
tabs.filter(({ url }) => url && !isBrowserUrl(url))
.forEach(({ id: tabId }) => tabId && executeScript(tabId, files))
}
function initQuestionnaire() {
try {
setUninstallURL(UNINSTALL_QUESTIONNAIRE[locale] ?? UNINSTALL_QUESTIONNAIRE['en'])
} catch (e) {
console.error("Failed to set uninstall URL", e)
}
}
export function initAfterInstalled() {
onInstalled(async reason => {
reason === "install" && await onFirstInstall()
// Questionnaire for uninstall
initQuestionnaire()
// Reload content-script
IS_FROM_STORE && await reloadContentScript()
// Initialize with version
versionManager.handle(reason)
})
}