-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
118 lines (91 loc) · 4.02 KB
/
content_script.js
File metadata and controls
118 lines (91 loc) · 4.02 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
(async () => {
if (window.hasRun) {
return;
}
window.hasRun = true;
const itemToInsertContent = document.getElementsByClassName("profile_header_centered_persona").item(0);
const divToInsertContent = itemToInsertContent.parentNode;
const container = document.createElement("div");
container.innerHTML = (await getHtmlContent()).innerHTML;
divToInsertContent.appendChild(container);
async function getHtmlContent() {
async function getErrorHtmlContent(){
const dbDirContent = browser.runtime.getURL("page-content/error.html");
const content = (await (await fetch(dbDirContent)).text());
const contentDoc = new DOMParser().parseFromString(content, 'text/html');
return contentDoc.body;
}
async function getEnabledSites(){
const settingsObj = await browser.storage.sync.get();
return settingsObj.sites;
}
const steamId = await getSteamId(window.location + "/");
if(!steamId){
await onError("Failed to get steamId. Probably no API key set.", "getHtmlContent");
return await getErrorHtmlContent();
}
const sitesContent = browser.runtime.getURL("page-content/sites.html");
const contentHtml = (await (await fetch(sitesContent)).text());
const contentDoc = new DOMParser().parseFromString(contentHtml, 'text/html');
const siteManager = new SiteManager(steamId, contentDoc);
await siteManager.AddAllEnabled(await getEnabledSites());
const content = await siteManager.Build();
return content.body;
}
async function getSteamId(url) {
async function getVanityUrlFromUrl(url) {
const idSegRegExp = /(\/id\/[A-z0-9\-]*\/)/g;
const idVanity = idSegRegExp.exec(url)[0];
const vanity = idVanity.split('/')[2];
await onError("Vanity: " + vanity, "getSteamId:getVanityUrlFromUrl");
return vanity;
}
async function getSteamIdFromVanity(url) {
const apiKeyObj = await browser.storage.sync.get("steamApiKey");
const apiKey = apiKeyObj.steamApiKey;
const vanityUrl = await getVanityUrlFromUrl(url);
const getIdUrl = `http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${apiKey}&vanityurl=${vanityUrl}`
const response = (await (await fetch(getIdUrl)).json());
await onError("Api response: " + JSON.stringify(response.response), "getSteamId:getSteamIdFromVanity");
return response.response.steamid;
}
async function isVanityUrl(url) {
const idSegRegExp = /(\/id\/[A-z0-9\-]*\/)/g;
return idSegRegExp.test(url);
}
async function getSteamIdFromUrl(url) {
const idSegRegExp = /(\/profiles\/\d*\/)/;
const profileSteamId = idSegRegExp.exec(url)[0];
const steamId = profileSteamId.split('/')[2];
await onError("Steam id from url: " + steamId, "getSteamId:getSteamIdFromUrl");
return steamId;
}
try {
let steamId;
if(await isVanityUrl(url)) {
await onError("Url contained vanity string", "getSteamId");
steamId = await getSteamIdFromVanity(url);
} else {
await onError("Url contained steamid", "getSteamId");
steamId = await getSteamIdFromUrl(url);
}
return steamId;
} catch (ex) {
await onError(ex, "getSteamId");
return undefined;
}
}
async function onError(error, functionName)
{
const settingsObj = await browser.storage.sync.get();
if(settingsObj.debugMode) {
let message;
if (typeof functionName === 'undefined') {
message = `[cswhois] ${error}`
} else {
message = `[cswhois (${functionName})] ${error}`
}
console.log(message);
}
}
})();