Skip to content

Commit 871f689

Browse files
author
hoang.tran12
committed
track
1 parent 14b9f8e commit 871f689

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

scripts/background-scripts/background-script.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ const CACHED = {
1111
activeScriptIds: [],
1212
path: chrome.runtime.getURL("/scripts/"),
1313
};
14+
const GLOBAL = {
15+
log: console.log,
16+
trackEvent,
17+
fetch: customFetch,
18+
};
1419

1520
function cacheActiveScriptIds() {
1621
getAllActiveScriptIds().then((ids) => {
@@ -30,10 +35,8 @@ function runScripts(tabId, event, world) {
3035
});
3136
}
3237

33-
const global = {
34-
log: console.log,
35-
trackEvent,
36-
async fetch(url, options) {
38+
async function customFetch(url, options) {
39+
try {
3740
const res = await fetch(url, options);
3841
let body;
3942

@@ -66,8 +69,11 @@ const global = {
6669
};
6770
console.log("Response from background script:", data);
6871
return data;
69-
},
70-
};
72+
} catch (e) {
73+
console.log("Fetch failed:", e);
74+
return null;
75+
}
76+
}
7177

7278
function main() {
7379
// listen change active scripts
@@ -118,7 +124,7 @@ function main() {
118124
try {
119125
if (request.action === "runInBackground") {
120126
const { params = [], fnPath = "" } = request.data || {};
121-
let fn = fnPath?.startsWith("chrome") ? chrome : global;
127+
let fn = fnPath?.startsWith("chrome") ? chrome : GLOBAL;
122128
fnPath.split(".").forEach((part) => {
123129
fn = fn?.[part] || fn;
124130
});
@@ -146,6 +152,7 @@ function main() {
146152
chrome.contextMenus.onClicked.addListener((info) => {
147153
console.log(info);
148154
if (info.menuItemId == "ufs-magnify-image") {
155+
trackEvent("magnify-image-context-menu");
149156
/*
150157
{
151158
"editable": false,
@@ -180,7 +187,7 @@ function main() {
180187
});
181188

182189
chrome.runtime.onInstalled.addListener(function () {
183-
global.trackEvent("ufs-installed");
190+
GLOBAL.trackEvent("ufs-installed");
184191

185192
chrome.contextMenus.create({
186193
title: "Magnify this image",

0 commit comments

Comments
 (0)