-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_worker.js
93 lines (87 loc) · 3.35 KB
/
service_worker.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
const BADGE_TEXT_COLOR = "#ffffff",
BADGE_TEXT_ENABLED = "on",
BADGE_TEXT_DISABLED = "off",
BADGE_BG_ENABLED = "#518c60",
BADGE_BG_DISABLED = "#ff0000";
let forcePasterSettings = {
isPasteEnabled: false,
clickCount: 0,
pasteCount: 0,
};
let saveAndApplyExtensionDetails = newData => {
forcePasterSettings = {
...forcePasterSettings,
...newData,
};
chrome.storage.local.set({ 'forcepaster': forcePasterSettings });
setExtensionUninstallURL(forcePasterSettings);
chrome.action.setBadgeText({ text: forcePasterSettings.isPasteEnabled ? BADGE_TEXT_ENABLED : BADGE_TEXT_DISABLED });
chrome.action.setBadgeBackgroundColor({ color: forcePasterSettings.isPasteEnabled ? BADGE_BG_ENABLED : BADGE_BG_DISABLED });
}
let setExtensionUninstallURL = debugData => {
const encodedDebugData = encodeURIComponent(
Object.keys(debugData).sort()
.map(key => `${key}: ${debugData[key]}`)
.join("\n")
);
chrome.runtime.setUninstallURL(`https://pratyushvashisht.com/forcepaster/uninstall?utm_source=chrome&utm_medium=extension&utm_campaign=uninstall&debugData=${encodedDebugData}`);
};
chrome.action.onClicked.addListener(() => {
saveAndApplyExtensionDetails({
isPasteEnabled: !forcePasterSettings.isPasteEnabled,
clickCount: forcePasterSettings.clickCount + 1,
});
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
const { type, on } = request;
if (type === "themechange") {
const icon_paths = {
"16": `icons/${request.mode}/icon16.png`,
"32": `icons/${request.mode}/icon32.png`,
"48": `icons/${request.mode}/icon48.png`,
"128": `icons/${request.mode}/icon128.png`
};
chrome.action.setIcon({path: icon_paths});
} else if (type === "onpastestart") {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
saveAndApplyExtensionDetails({
pasteStart: forcePasterSettings.pasteCount + 1,
lastDomain: (new URL(tabs[0].url)).origin,
lastTag: on,
});
})
} else if (type === "onpastecomplete") {
saveAndApplyExtensionDetails({
pasteCount: forcePasterSettings.pasteCount + 1,
});
sendResponse({ totalPastes: forcePasterSettings.pasteCount });
}
return true;
});
chrome.runtime.onInstalled.addListener(async installInfo => {
let installDate, updateDate;
if (installInfo.reason === "install") {
installDate = new Date().toISOString();
} else {
updateDate = new Date().toISOString();
}
const platformInfo = await chrome.runtime.getPlatformInfo();
// let isExtensionPinned = await chrome.action.getUserSettings().isOnToolbar;
let debugData = {
...platformInfo,
agent: navigator.userAgent,
locale: navigator.language,
platform: navigator.platform,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
version: chrome.runtime.getManifest().version,
}
if (installDate) debugData.installDate = installDate;
if (updateDate) debugData.updateDate = updateDate;
saveAndApplyExtensionDetails({
isPasteEnabled: false,
clickCount: 0,
pasteCount: 0,
...debugData
});
chrome.action.setBadgeTextColor({ color: BADGE_TEXT_COLOR });
});