Skip to content

Commit e52b6e7

Browse files
committedJan 15, 2024
Add npm standard package and handle errors in background.js
1 parent 95d0ce6 commit e52b6e7

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed
 

‎.devcontainer/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# syntax=docker/dockerfile:1
22
FROM mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye
33

4-
RUN npm install -g npm
4+
RUN npm install -g npm && npm install -g standard

‎gcp-project-tooltip-extension/src/background.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ chrome.action.onClicked.addListener(async (tab) => {
88
: { '16': '/images/icon16.png', '48': '/images/icon48.png', '128': '/images/icon128.png' };
99
chrome.action.setIcon({ path: iconPath });
1010

11-
await chrome.scripting.unregisterContentScripts({ ids: ['project-id-content-script'] }).catch(() => { });
11+
try {
12+
await chrome.scripting.unregisterContentScripts({ ids: ['project-id-content-script'] });
13+
} catch (error) {
14+
console.log(error);
15+
}
1216

1317
let projectIDMap = {};
1418

@@ -30,20 +34,23 @@ chrome.action.onClicked.addListener(async (tab) => {
3034
url.toString();
3135
auth_url += url;
3236

33-
let token = await new Promise((resolve, reject) => {
34-
chrome.identity.launchWebAuthFlow({ url: auth_url, interactive: true }, function (responseUrl) {
35-
if (chrome.runtime.lastError) {
36-
// If there's an error, reject the promise
37-
reject(chrome.runtime.lastError);
38-
} else {
39-
let url = new URL(responseUrl);
40-
let params = new URLSearchParams(url.hash.substring(1)); // remove the '#' at the start
41-
resolve(params.get('access_token'));
42-
}
37+
let token;
38+
try {
39+
token = await new Promise((resolve, reject) => {
40+
chrome.identity.launchWebAuthFlow({ url: auth_url, interactive: true }, function (responseUrl) {
41+
if (chrome.runtime.lastError) {
42+
// If there's an error, reject the promise
43+
reject(chrome.runtime.lastError);
44+
} else {
45+
let url = new URL(responseUrl);
46+
let params = new URLSearchParams(url.hash.substring(1)); // remove the '#' at the start
47+
resolve(params.get('access_token'));
48+
}
49+
});
4350
});
44-
}).catch(error => {
51+
} catch (error) {
4552
console.log(error);
46-
});
53+
}
4754

4855

4956
const gcp_url = new URL('https://cloudresourcemanager.googleapis.com/v1/projects')
@@ -75,7 +82,11 @@ chrome.action.onClicked.addListener(async (tab) => {
7582

7683
const execOpts = enabled ? { files: ['src/contentScript.js'] } : {};
7784

78-
chrome.scripting.executeScript({ target: { tabId: tab.id }, ...execOpts })
79-
.catch(() => { });
85+
86+
try {
87+
await chrome.scripting.executeScript({ target: { tabId: tab.id }, ...execOpts });
88+
} catch (error) {
89+
console.log(error);
90+
}
8091
}
8192
});

0 commit comments

Comments
 (0)