-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
73 lines (68 loc) · 2.73 KB
/
background.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
'use strict';
import { setBadgeAndShield } from "./utils.js";
async function getExtensionStatus(extensionId) {
extensionId += '?&' + Date.now()
try {
const statusCode = await new Promise((resolve, reject) => {
fetch("https://corsproxy.io/?https://chrome.google.com/webstore/detail/" + extensionId, {
redirect: 'manual'
}).then(response => {
resolve(response.type == 'opaqueredirect' ? 301 : response.status)
return {}
}).catch(err => {
reject(err)
})
});
return statusCode
} catch (error) {
}
try {
const statusCode = await new Promise((resolve, reject) => {
fetch("https://api.allorigins.win/get?url=https://chrome.google.com/webstore/detail/" + extensionId, {
redirect: 'manual'
}).then(resp => resp.json()).then(data => resolve(data.status.http_code == 200 ? 301 : data.status.http_code)).catch(err => reject(err))
});
return statusCode
} catch (error) {
throw error
}
}
async function checkExtensions() {
chrome.management.getAll(async extensions => {
var totalHighRiskExtensions = 0
var highRiskExtensionsReason = {}
for (let i = 0; i < extensions.length; i++) {
const extension = extensions[i]
try {
// check if extension is from the webstore
const statusCode = await getExtensionStatus(extension.id)
if (statusCode != 301 && statusCode != 404) {
throw "Unexpected status code: " + statusCode
}
if (statusCode != 301) {
totalHighRiskExtensions++
highRiskExtensionsReason[extension.id] = "Removed in webstore"
}
} catch (error) {
if (typeof error == "object" && 'message' in error) {
error = error.message
}
totalHighRiskExtensions++
highRiskExtensionsReason[extension.id] = "Error checking webstore: " + error
}
await new Promise((resolve, reject) => setTimeout(resolve, Math.random() * 5000 + 5000))
}
setBadgeAndShield(totalHighRiskExtensions, false)
chrome.storage.sync.set({ totalHighRiskExtensions: totalHighRiskExtensions, highRiskExtensionsReason: highRiskExtensionsReason })
})
}
chrome.runtime.onInstalled.addListener(async () => {
checkExtensions()
chrome.alarms.create('CheckExtensions', { periodInMinutes: 30 })
});
chrome.alarms.onAlarm.addListener((alarm) => {
switch (alarm.name) {
case 'CheckExtensions':
return checkExtensions()
}
});