blob: 6c3da8d899d08176c8c27e1cc54f77a00927b67c [file] [log] [blame]
avm9996337601bc2022-02-21 10:36:45 +01001import {getOptions} from '../common/optionsUtils.js';
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +02002import {correctArrayKeys} from '../common/protojs.js';
avm9996337601bc2022-02-21 10:36:45 +01003
4import PerForumStatsSection from './communityConsole/utils/PerForumStatsSection.js';
avm9996337601bc2022-02-21 10:36:45 +01005import {injectPreviousPostsLinksUnifiedProfile} from './utilsCommon/unifiedProfiles.js';
6
Adrià Vilanova Martínez217e5f02023-07-22 20:55:50 +02007const profileViewRegex = /var view ?= ?'([^']+)';/;
avm9996337601bc2022-02-21 10:36:45 +01008
Adrià Vilanova Martínez89c54812022-06-06 13:32:29 +02009getOptions(['history', 'perforumstats']).then(options => {
avm9996337601bc2022-02-21 10:36:45 +010010 if (options?.history)
11 injectPreviousPostsLinksUnifiedProfile(/* isCommunityConsole = */ false);
12
Adrià Vilanova Martínez89c54812022-06-06 13:32:29 +020013 if (options?.perforumstats) {
avm9996337601bc2022-02-21 10:36:45 +010014 try {
15 // Find chart
16 const chart = document.querySelector(
17 'sc-tailwind-user_profile-user-profile ' +
18 '.scTailwindUser_profileUserprofilesection ' +
19 'sc-tailwind-shared-activity-chart');
20 if (!chart) throw new Error('Couldn\'t find existing chart.');
21
22 // Extract profile JSON information
23 const scripts = document.querySelectorAll('script');
24 let profileView = null;
25 for (let i = 0; i < scripts.length; ++i) {
26 const matches = scripts[i].textContent.match(profileViewRegex);
27 if (matches?.[1]) {
Adrià Vilanova Martínez217e5f02023-07-22 20:55:50 +020028 let rawJsonStringContents =
29 matches[1]
30 .replace(
31 /\\x([0-9a-f]{2})/ig,
32 (_, pair) => {
33 return String.fromCharCode(parseInt(pair, 16));
34 })
35 .replace(/\\'/g, `'`)
36 .replace(/"/g, `\\"`);
37 let rawJsonString = `"${rawJsonStringContents}"`;
38 let rawJson = JSON.parse(rawJsonString);
39 profileView = JSON.parse(rawJson);
avm9996337601bc2022-02-21 10:36:45 +010040 break;
41 }
42 }
43 const profileViewC = {'1': correctArrayKeys(profileView)};
avm9996337601bc2022-02-21 10:36:45 +010044 if (!profileView) throw new Error('Could not find user view data.');
45 new PerForumStatsSection(
46 chart?.parentNode, profileViewC,
47 document.documentElement?.lang ?? 'en',
48 /* isCommunityConsole = */ false);
49 } catch (err) {
50 console.error('Error while injecting extra info: ', err);
51 }
52 }
53});