Skip to content

Commit a7e23df

Browse files
Refactor the company id feature.
1 parent 28401f0 commit a7e23df

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

extension/changelog.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
{ "message": "Update the hardcoded items which we use as fallback.", "contributor": "DeKleineKobini" },
1717
{ "message": "Improve background performance.", "contributor": "DeKleineKobini" },
1818
{ "message": "Update all company specials to avoid outdated specials being shown.", "contributor": "DeKleineKobini" },
19-
{ "message": "Improve the detection which faction id you are currently visiting.", "contributor": "DeKleineKobini" }
19+
{ "message": "Improve the detection which faction id you are currently visiting.", "contributor": "DeKleineKobini" },
20+
{ "message": "Refactor the company id feature.", "contributor": "DeKleineKobini" }
2021
],
2122
"removed": []
2223
}

extension/scripts/content/company/ttCompany.js

+35
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,38 @@ if (!isOwnCompany) {
2626
else if (getHashParameters().get("option") === "stock") triggerCustomListener(EVENT_CHANNELS.COMPANY_STOCK_PAGE);
2727
});
2828
}
29+
30+
async function readCompanyDetails() {
31+
if (isOwnCompany && hasAPIData()) {
32+
if (userdata.job.company_id) return { id: userdata.job.company_id };
33+
34+
const userID = userdata.player_id;
35+
if (!userID) return null; // ID could not be found
36+
37+
return { id: await getCompanyIDFromUser(userID) };
38+
}
39+
40+
const params = getHashParameters();
41+
42+
if (isIntNumber(params.get("ID"))) {
43+
return { id: parseInt(params.get("ID")) };
44+
}
45+
46+
if (isIntNumber(params.get("userID")) && hasAPIData()) {
47+
return { id: await getCompanyIDFromUser(parseInt(params.get("userID"))) };
48+
}
49+
50+
return null; // ID could not be found
51+
52+
async function getCompanyIDFromUser(userID) {
53+
const cached = ttCache.get("company-id", userID);
54+
if (cached) return cached;
55+
56+
const data = await fetchData("torn", { section: "user", selections: ["profile"], id: userID });
57+
const companyID = data.job.company_id;
58+
59+
void ttCache.set({ [userID]: companyID }, 1 * TO_MILLIS.DAYS, "company-id");
60+
61+
return companyID;
62+
}
63+
}

extension/scripts/features/company-id/ttCompanyID.js

+10-30
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,29 @@
1717
);
1818

1919
function initialise() {
20-
if (!isOwnCompany)
20+
if (!isOwnCompany) {
2121
CUSTOM_LISTENERS[EVENT_CHANNELS.COMPANY_EMPLOYEES_PAGE].push(() => {
2222
if (!feature.enabled() || !settings.pages.companies.idBesideCompanyName) return;
2323

2424
addID();
2525
});
26+
}
2627
}
2728

2829
async function addID() {
2930
if (document.getElementById("tt-company-id")) return; // Element has already been added - second check in-case feature reinjects
30-
const selector = isOwnCompany ? "div.company-wrap > div.title-black" : "div.company-details-wrap > div.company-details > div.title-black";
31-
const [id, container] = await Promise.all([getCompanyID(), requireElement(selector)]);
31+
32+
const container = await requireElement(
33+
isOwnCompany ? "div.company-wrap > div.title-black" : "div.company-details-wrap > div.company-details > div.title-black"
34+
);
35+
36+
const id = await readCompanyDetails();
3237
if (!id) throw new Error("Company ID could not be found.");
33-
const span = document.newElement({ type: "span", text: ` [${id}]`, id: "tt-company-id" });
34-
container.appendChild(span);
38+
39+
container.appendChild(document.newElement({ type: "span", text: ` [${id}]`, id: "tt-company-id" }));
3540
}
3641

3742
function removeID() {
3843
document.findAll("#tt-company-id").forEach((element) => element.remove());
3944
}
40-
41-
async function getCompanyID() {
42-
if (isOwnCompany) {
43-
if (userdata.job.company_id) return userdata.job.company_id;
44-
const userID = userdata.player_id;
45-
if (!userID) return null; // ID could not be found
46-
return await getCompanyIDFromUser(userID);
47-
}
48-
49-
const hashparams = getHashParameters();
50-
51-
if (isIntNumber(hashparams.get("ID"))) return parseInt(hashparams.get("ID"));
52-
if (isIntNumber(hashparams.get("userID"))) return await getCompanyIDFromUser(parseInt(hashparams.get("userID")));
53-
54-
return null; // ID could not be found
55-
56-
async function getCompanyIDFromUser(userID) {
57-
const cached = ttCache.get("company-id", userID);
58-
if (cached) return cached;
59-
const data = await fetchData("torn", { section: "user", selections: ["profile"], id: userID });
60-
const companyID = data.job.company_id;
61-
ttCache.set({ [userID]: companyID }, 3.5 * TO_MILLIS.DAYS, "company-id");
62-
return companyID;
63-
}
64-
}
6545
})();

0 commit comments

Comments
 (0)