blob: 82e949e978b06110eff6e3b32f74e2af95071ae5 [file] [log] [blame]
avm9996337601bc2022-02-21 10:36:45 +01001import {getOptions} from '../common/optionsUtils.js';
2
3import PerForumStatsSection from './communityConsole/utils/PerForumStatsSection.js';
4import {correctArrayKeys} from './utilsCommon/protojs.js';
5import {injectPreviousPostsLinksUnifiedProfile} from './utilsCommon/unifiedProfiles.js';
6
7const profileViewRegex = /var view ?= ?(.+\]);/;
8
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]) {
28 profileView = JSON.parse(matches[1]);
29 break;
30 }
31 }
32 const profileViewC = {'1': correctArrayKeys(profileView)};
33 console.log(profileViewC);
34 if (!profileView) throw new Error('Could not find user view data.');
35 new PerForumStatsSection(
36 chart?.parentNode, profileViewC,
37 document.documentElement?.lang ?? 'en',
38 /* isCommunityConsole = */ false);
39 } catch (err) {
40 console.error('Error while injecting extra info: ', err);
41 }
42 }
43});