blob: 71b4779167fa0acf8ab89c57d4c8348243e11c5a [file] [log] [blame]
Adrià Vilanova Martínezba3eeba2021-07-25 12:22:02 +02001import {injectScript, injectStylesheet} from '../../common/contentScriptsUtils.js';
Adrià Vilanova Martínezc14e44a2023-03-09 19:28:03 +01002import MWI18nServer from '../../common/mainWorldI18n/Server.js';
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +01003import MWOptionsWatcherServer from '../../common/mainWorldOptionsWatcher/Server.js';
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02004import {getOptions} from '../../common/optionsUtils.js';
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +01005import {kCSTarget, kMWTarget} from '../../xhrInterceptor/responseModifiers/index.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02006
avm99963b6f68b62021-08-12 23:13:06 +02007import AutoRefresh from './autoRefresh.js';
Adrià Vilanova Martínez0d92a0c2023-11-06 01:37:20 +01008import ExtraInfo from './extraInfo/index.js';
Adrià Vilanova Martínez72a4ea42024-02-09 22:35:32 +01009import FlattenThreadsReplyActionHandler from './flattenThreads/replyActionHandler.js';
Adrià Vilanova Martínezd2344c42022-09-30 13:14:29 +020010import ThreadPageDesignWarning from './threadPageDesignWarning.js';
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010011import WorkflowsImport from './workflows/import.js';
avm99963b6f68b62021-08-12 23:13:06 +020012
Adrià Vilanova Martínez849ede62022-06-20 19:10:28 +020013const SMEI_NESTED_REPLIES = 15;
14const SMEI_RCE_THREAD_INTEROP = 22;
15
Adrià Vilanova Martínez4b6ce462023-04-15 02:06:09 +020016// The servers should be available as soon as possible, since e.g. the XHRProxy
17// already sends a request to the optionsWatcher server as soon as it is
18// constructed.
19new MWOptionsWatcherServer(kCSTarget, kMWTarget);
20new MWI18nServer();
21
Adrià Vilanova Martínez102d54b2022-12-18 11:12:11 +010022injectScript(
23 chrome.runtime.getURL('xhrInterceptorInject.bundle.js'),
24 /* prepend = */ true);
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +020025injectScript(chrome.runtime.getURL('extraInfoInject.bundle.js'));
26
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020027getOptions(null).then(options => {
Adrià Vilanova Martínezd2ac4ce2021-06-11 16:41:43 +020028 /* IMPORTANT NOTE: Remember to change this when changing the "ifs" below!! */
Adrià Vilanova Martínez32fd6422022-06-27 11:48:18 +020029 if (options.loaddrafts || options.interopthreadpage ||
30 options.nestedreplies) {
avm99963b69eb3d2020-08-20 02:03:44 +020031 var startup =
32 JSON.parse(document.querySelector('html').getAttribute('data-startup'));
avm99963c19497f2019-08-23 00:15:11 +020033
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020034 if (options.loaddrafts) {
avm99963c19497f2019-08-23 00:15:11 +020035 startup[4][13] = true;
36 }
37
Adrià Vilanova Martínez849ede62022-06-20 19:10:28 +020038 if (options.interopthreadpage) {
39 var index = startup[1][6].indexOf(SMEI_RCE_THREAD_INTEROP);
40
41 switch (options.interopthreadpage_mode) {
42 case 'previous':
43 if (index > -1) startup[1][6].splice(index, 1);
44 break;
45
46 case 'next':
47 if (index == -1) startup[1][6].push(SMEI_RCE_THREAD_INTEROP);
48 break;
49 }
50 }
51
52 if (options.nestedreplies) {
53 if (!startup[1][6].includes(SMEI_NESTED_REPLIES))
54 startup[1][6].push(SMEI_NESTED_REPLIES);
55 }
56
avm99963b69eb3d2020-08-20 02:03:44 +020057 document.querySelector('html').setAttribute(
58 'data-startup', JSON.stringify(startup));
avm999636d9c5fe2019-06-04 00:35:53 +020059 }
avm999630bc113a2020-09-07 13:02:11 +020060
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010061 // Initialized here instead of in main.js so the first event is received if it
62 // happens when the page loads.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020063 window.TWPTAutoRefresh = new AutoRefresh();
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010064 window.TWPTExtraInfo = new ExtraInfo();
Adrià Vilanova Martínezd2344c42022-09-30 13:14:29 +020065 window.TWPTThreadPageDesignWarning = new ThreadPageDesignWarning();
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010066 window.TWPTWorkflowsImport = new WorkflowsImport();
avm99963b6f68b62021-08-12 23:13:06 +020067
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020068 if (options.ccdarktheme) {
69 switch (options.ccdarktheme_mode) {
avm999630bc113a2020-09-07 13:02:11 +020070 case 'switch':
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020071 if (options.ccdarktheme_switch_status == true)
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +010072 injectStylesheet(chrome.runtime.getURL('ccDarkTheme.bundle.css'));
avm999630bc113a2020-09-07 13:02:11 +020073 break;
74
75 case 'system':
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +010076 injectStylesheet(chrome.runtime.getURL('ccDarkTheme.bundle.css'), {
avm999630bc113a2020-09-07 13:02:11 +020077 'media': '(prefers-color-scheme: dark)',
78 });
79 break;
80 }
81 }
Adrià Vilanova Martíneza2f79512022-07-10 15:32:32 +020082
83 if (options.uispacing) {
84 injectStylesheet(chrome.runtime.getURL('css/ui_spacing/shared.css'));
85 injectStylesheet(chrome.runtime.getURL('css/ui_spacing/console.css'));
86 }
Adrià Vilanova Martínez72a4ea42024-02-09 22:35:32 +010087
88 const flattenThreadsReplyActionHandler = new FlattenThreadsReplyActionHandler(options);
89 flattenThreadsReplyActionHandler.handleIfApplicable();
avm999636d9c5fe2019-06-04 00:35:53 +020090});