-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbackground.js
More file actions
148 lines (129 loc) · 3.77 KB
/
background.js
File metadata and controls
148 lines (129 loc) · 3.77 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// PS Plus game name lookup map (normalized name -> game data)
let psplusGamesMap = null;
let psplusLoadPromise = null;
// Load PS Plus games data
const loadPSPlusGames = async () => {
if (psplusGamesMap) return psplusGamesMap;
if (psplusLoadPromise) return psplusLoadPromise;
psplusLoadPromise = (async () => {
try {
const url = 'https://www.playstation.com/bin/imagic/gameslist?locale=en-us&categoryList=plus-games-list';
const response = await fetch(url);
const data = await response.json();
// Build normalized name lookup map
psplusGamesMap = new Map();
data.forEach(category => {
if (category.games) {
category.games.forEach(game => {
const normalizedName = normalizeName(game.nameEn || game.name);
psplusGamesMap.set(normalizedName, {
name: game.nameEn || game.name,
conceptUrl: game.conceptUrl,
device: game.device
});
});
}
});
return psplusGamesMap;
} catch (error) {
console.error('Failed to load PS Plus games:', error);
psplusGamesMap = new Map();
return psplusGamesMap;
}
})();
return psplusLoadPromise;
};
// Normalize game name for matching
const normalizeName = (name) => {
if (!name) return '';
return name
.toLowerCase()
.replace(/[™®©]/g, '')
.replace(/[:''"\-–—]/g, ' ')
.replace(/\s+/g, ' ')
.trim();
};
// Check if game matches PS Plus by name
const checkPSPlus = async (gameName) => {
const map = await loadPSPlusGames();
const normalized = normalizeName(gameName);
return map.get(normalized) || null;
};
// Add PS Plus info to game response
const addPSPlusToGames = async (games) => {
if (!games || !Array.isArray(games)) return games;
await loadPSPlusGames();
for (const game of games) {
if (game.name && game.status === 'found') {
const psplusMatch = await checkPSPlus(game.name);
if (psplusMatch) {
if (!game.subs) game.subs = {};
game.subs.psplus = {
status: 'active',
date: {
since: null,
until: null
},
link: psplusMatch.conceptUrl,
target: '_blank'
};
}
}
}
return games;
};
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.type) {
case 'fetch-game':
fetchData(request.data, 'game')
.then(r => addPSPlusToGames(r))
.then(r => sendResponse(r));
break;
case 'fetch-pass':
fetchData(request.data, 'pass').then(r => sendResponse(r));
break;
case 'fetch-menu':
getMenu(request.data, 'menu').then(r => sendResponse(r));
break;
}
return true;
});
const getMenu = async (data, url) => {
// todo: clear cache if settings change
const menu = await chrome.storage.session.get("menu");
// Check if the menu is in the cache and if it's not older than 15 minutes
if (menu?.menu?.timestamp > Date.now() - 1000 * 60 * 15) {
return menu?.menu?.data;
}
return await fetchData(data, url);
}
const fetchData = async (data, url) => {
const save = await chrome.storage.sync.get("aSub_options");
const enabled = save?.aSub_options?.options?.enabled ?? {};
// Add the extension options to the data
const keys = Object.keys(enabled).filter(key => enabled[key] === false);
if (keys.length > 0) {
data.ec = keys;
}
// Add the extension version to the data
const version = chrome.runtime.getManifest().version;
data.v = version.replaceAll('.', '-');
return fetch(`https://aligueler.com/SubscriptionInfo/ajax/${url}data.php`, {
method: 'POST',
headers: {
// 'Autorization': 'Basic ' + btoa(''),
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(async response => {
try {
const jsonData = await response.json();
if (url === 'menu') {
chrome.storage.session.set({ menu: { data: jsonData, timestamp: Date.now() } });
}
return jsonData;
} catch (error) {
console.error(error, response);
}
});
}