-
Notifications
You must be signed in to change notification settings - Fork 21
/
service_worker.js
86 lines (84 loc) · 2.42 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
// Context menus
chrome.runtime.onInstalled.addListener(() => {
const context_defs = [
{
id: 'community-friends',
title: 'Steam Friends'
},
{
id: 'community-groups',
title: 'Steam Groups'
},
{
id: 'community-recently',
title: 'Recently Played With'
},
{
id: 'cs-premier-matches',
title: 'Counter-Strike Premier Matches'
},
{
id: 'cs-competitive-matches',
title: 'Counter-Strike Ranked Competitive Matches'
},
{
id: 'github',
title: 'Github page'
}
];
for (const def of context_defs) {
chrome.contextMenus.create({ ...def, contexts: ['action'] });
}
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
const actions = {
'cs-competitive-matches':
'https://steamcommunity.com/my/gcpd/730?tab=matchhistorycompetitivepermap',
'cs-premier-matches':
'https://steamcommunity.com/my/gcpd/730?tab=matchhistorypremier',
'community-friends': 'https://steamcommunity.com/my/friends/',
'community-groups': 'https://steamcommunity.com/my/groups/',
github: 'https://github.com/ge-ku/Ban-Checker-for-Steam',
'community-recently': 'https://steamcommunity.com/my/friends/coplay/'
};
if (info.menuItemId in actions) {
chrome.tabs.create({ url: actions[info.menuItemId] });
}
});
// Fetch bans using Steam API
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'showOptions') {
chrome.runtime.openOptionsPage();
}
if (request.action === 'fetchBans') {
chrome.permissions.contains(
{
origins: ['*://steamcommunity.com/*', 'https://api.steampowered.com/*']
},
hasPermissions => {
if (hasPermissions) {
fetch(
`https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=${
request.apikey
}&steamids=${request.batch.join(',')}`
)
.then(res => {
if (res.ok) {
return res.json();
} else {
throw Error(`Code ${res.status}. ${res.statusText}`);
}
})
.then(json => sendResponse({ json }))
.catch(error => sendResponse({ json: undefined, error }));
} else {
sendResponse({
json: undefined,
error: 'No permissions to access Steam Web API'
});
}
}
);
}
return true;
});