blob: f25fe4b6f2ca3e7ac3513f57aabe034095ea0643 [file] [log] [blame]
avm99963cbea3142019-03-28 00:48:15 +01001var mutationObserver, intersectionObserver, options;
2
avm99963af7860e2019-06-04 03:33:26 +02003function parseUrl(url) {
4 var forum_a = url.match(/forum\/([0-9]+)/i);
5 var thread_a = url.match(/thread\/([0-9]+)/i);
6
7 if (forum_a === null || thread_a === null) {
8 return false;
9 }
10
11 return {
avm99963b69eb3d2020-08-20 02:03:44 +020012 'forum': forum_a[1],
13 'thread': thread_a[1],
avm99963af7860e2019-06-04 03:33:26 +020014 };
15}
16
avm99963943b8492020-08-31 23:40:43 +020017function addProfileHistoryLink(node, type, query) {
18 var urlpart = encodeURIComponent('query=' + query);
19 var container = document.createElement('div');
20 container.style.margin = '3px 0';
21
22 var link = document.createElement('a');
23 link.setAttribute(
24 'href', 'https://support.google.com/s/community/search/' + urlpart);
25 link.innerText = chrome.i18n.getMessage('inject_previousposts_' + type);
26
27 container.appendChild(link);
28 node.querySelector('.main-card-content').appendChild(container);
29}
30
avm99963847ee632019-03-27 00:57:44 +010031function mutationCallback(mutationList, observer) {
32 mutationList.forEach((mutation) => {
avm99963b69eb3d2020-08-20 02:03:44 +020033 if (mutation.type == 'childList') {
34 mutation.addedNodes.forEach(function(node) {
35 if (typeof node.classList !== 'undefined') {
36 if (options.thread && node.classList.contains('load-more-bar')) {
37 intersectionObserver.observe(
38 node.querySelector('.load-more-button'));
avm99963d0757252019-03-30 20:13:00 +010039 }
40
avm99963b69eb3d2020-08-20 02:03:44 +020041 if (options.threadall && node.classList.contains('load-more-bar')) {
42 intersectionObserver.observe(
43 node.querySelector('.load-all-button'));
avm999636d9c5fe2019-06-04 00:35:53 +020044 }
45
avm99963b69eb3d2020-08-20 02:03:44 +020046 if (options.history && ('parentNode' in node) &&
47 node.parentNode !== null && ('tagName' in node.parentNode) &&
48 node.parentNode.tagName == 'EC-USER') {
49 var nameElement = node.querySelector('.name span');
avm99963d0757252019-03-30 20:13:00 +010050 if (nameElement !== null) {
avm99963943b8492020-08-31 23:40:43 +020051 var forumId =
52 location.href.split('/forum/')[1].split('/')[0] || '0';
53
54 var name = escapeUsername(nameElement.innerHTML);
55 var query1 = encodeURIComponent(
avm99963ad65e752020-09-01 00:13:59 +020056 '(creator:"' + name + '" | replier:"' + name +
57 '") forum:' + forumId);
avm99963943b8492020-08-31 23:40:43 +020058 var query2 = encodeURIComponent(
avm99963ad65e752020-09-01 00:13:59 +020059 '(creator:"' + name + '" | replier:"' + name +
60 '") forum:any');
avm99963943b8492020-08-31 23:40:43 +020061 addProfileHistoryLink(node, 'forum', query1);
62 addProfileHistoryLink(node, 'all', query2);
avm99963d0757252019-03-30 20:13:00 +010063 }
64 }
65 }
avm99963847ee632019-03-27 00:57:44 +010066 });
67 }
68 });
69}
70
avm99963adf90862020-04-12 13:27:45 +020071function intersectionCallback(entries, observer) {
avm99963847ee632019-03-27 00:57:44 +010072 entries.forEach(entry => {
73 if (entry.isIntersecting) {
74 entry.target.click();
75 }
76 });
77};
78
79var observerOptions = {
80 childList: true,
81 attributes: true,
avm99963b69eb3d2020-08-20 02:03:44 +020082 subtree: true,
avm99963847ee632019-03-27 00:57:44 +010083}
84
avm99963129fb502020-08-28 05:18:53 +020085var intersectionOptions = {
86 root: document.querySelector('.scrollable-content'),
87 rootMargin: '0px',
88 threshold: 1.0,
89};
avm99963847ee632019-03-27 00:57:44 +010090
avm99963129fb502020-08-28 05:18:53 +020091chrome.storage.sync.get(null, function(items) {
92 options = items;
avm99963cbea3142019-03-28 00:48:15 +010093
avm99963129fb502020-08-28 05:18:53 +020094 mutationObserver = new MutationObserver(mutationCallback);
95 mutationObserver.observe(
96 document.querySelector('.scrollable-content'), observerOptions);
avm99963cbea3142019-03-28 00:48:15 +010097
avm99963129fb502020-08-28 05:18:53 +020098 intersectionObserver =
99 new IntersectionObserver(intersectionCallback, intersectionOptions);
avm99963122dc9b2019-03-30 18:44:18 +0100100
avm99963129fb502020-08-28 05:18:53 +0200101 if (options.fixedtoolbar) {
102 injectStyles(
avm999630bc113a2020-09-07 13:02:11 +0200103 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
avm99963129fb502020-08-28 05:18:53 +0200104 }
avm99963ae6a26d2020-04-12 14:03:51 +0200105
avm99963129fb502020-08-28 05:18:53 +0200106 if (options.increasecontrast) {
avm999630bc113a2020-09-07 13:02:11 +0200107 injectStyles(
108 '.thread-summary.read{background: var(--TWPT-thread-read-background, #ecedee)!important;}');
avm99963129fb502020-08-28 05:18:53 +0200109 }
avm999630f9503f2020-07-27 13:56:52 +0200110
avm99963129fb502020-08-28 05:18:53 +0200111 if (options.stickysidebarheaders) {
112 injectStyles(
avm999630bc113a2020-09-07 13:02:11 +0200113 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
114 }
115
116 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
117 injectStylesheet(
118 chrome.runtime.getURL('injections/ccdarktheme_switch.css'));
119
120 var darkThemeSwitch = document.createElement('material-button');
121 darkThemeSwitch.classList.add('TWPT-dark-theme');
122 darkThemeSwitch.setAttribute('button', '');
123 darkThemeSwitch.setAttribute(
124 'title', chrome.i18n.getMessage('inject_ccdarktheme_helper'));
125
126 darkThemeSwitch.addEventListener('click', e => {
127 chrome.storage.sync.get(null, currentOptions => {
128 currentOptions.ccdarktheme_switch_status =
129 !options.ccdarktheme_switch_status;
130 chrome.storage.sync.set(currentOptions, _ => {
131 location.reload();
132 });
133 });
134 });
135
136 var switchContent = document.createElement('div');
137 switchContent.classList.add('content');
138
139 var icon = document.createElement('material-icon');
140
141 var i = document.createElement('i');
142 i.classList.add('material-icon-i', 'material-icons-extended');
143 i.textContent = 'brightness_4';
144
145 icon.appendChild(i);
146 switchContent.appendChild(icon);
147 darkThemeSwitch.appendChild(switchContent);
148
149 var rightControl = document.querySelector('header .right-control');
150 rightControl.style.width =
151 (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px';
152 rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch);
avm99963129fb502020-08-28 05:18:53 +0200153 }
avm99963129fb502020-08-28 05:18:53 +0200154});