blob: ee3bffb464baad1de4a7ce9d9869cabb404cff39 [file] [log] [blame]
avm99963a2945b62020-11-27 00:32:02 +01001var mutationObserver, intersectionObserver, options, authuser;
avm99963cbea3142019-03-28 00:48:15 +01002
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);
avm99963a2945b62020-11-27 00:32:02 +010019 var authuserpart =
20 (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
avm99963943b8492020-08-31 23:40:43 +020021 var container = document.createElement('div');
22 container.style.margin = '3px 0';
23
24 var link = document.createElement('a');
25 link.setAttribute(
avm99963a2945b62020-11-27 00:32:02 +010026 'href',
27 'https://support.google.com/s/community/search/' + urlpart +
28 authuserpart);
avm99963943b8492020-08-31 23:40:43 +020029 link.innerText = chrome.i18n.getMessage('inject_previousposts_' + type);
30
31 container.appendChild(link);
avm9996306167752020-09-08 00:50:36 +020032 node.appendChild(container);
avm99963943b8492020-08-31 23:40:43 +020033}
34
avm99963847ee632019-03-27 00:57:44 +010035function mutationCallback(mutationList, observer) {
36 mutationList.forEach((mutation) => {
avm99963b69eb3d2020-08-20 02:03:44 +020037 if (mutation.type == 'childList') {
38 mutation.addedNodes.forEach(function(node) {
39 if (typeof node.classList !== 'undefined') {
40 if (options.thread && node.classList.contains('load-more-bar')) {
41 intersectionObserver.observe(
42 node.querySelector('.load-more-button'));
avm99963d0757252019-03-30 20:13:00 +010043 }
44
avm99963b69eb3d2020-08-20 02:03:44 +020045 if (options.threadall && node.classList.contains('load-more-bar')) {
46 intersectionObserver.observe(
47 node.querySelector('.load-all-button'));
avm999636d9c5fe2019-06-04 00:35:53 +020048 }
49
avm99963b69eb3d2020-08-20 02:03:44 +020050 if (options.history && ('parentNode' in node) &&
51 node.parentNode !== null && ('tagName' in node.parentNode) &&
52 node.parentNode.tagName == 'EC-USER') {
53 var nameElement = node.querySelector('.name span');
avm99963d0757252019-03-30 20:13:00 +010054 if (nameElement !== null) {
avm99963943b8492020-08-31 23:40:43 +020055 var forumId =
56 location.href.split('/forum/')[1].split('/')[0] || '0';
57
avm9996314116b02020-11-02 14:04:14 +010058 var name = escapeUsername(nameElement.textContent);
avm99963943b8492020-08-31 23:40:43 +020059 var query1 = encodeURIComponent(
avm99963ad65e752020-09-01 00:13:59 +020060 '(creator:"' + name + '" | replier:"' + name +
61 '") forum:' + forumId);
avm99963943b8492020-08-31 23:40:43 +020062 var query2 = encodeURIComponent(
avm99963ad65e752020-09-01 00:13:59 +020063 '(creator:"' + name + '" | replier:"' + name +
64 '") forum:any');
avm9996306167752020-09-08 00:50:36 +020065
66 var container = document.createElement('div');
67 container.classList.add('TWPT-previous-posts');
68
69 var badge = document.createElement('div');
70 badge.classList.add('TWPT-badge');
71 badge.setAttribute(
72 'title',
73 chrome.i18n.getMessage(
74 'inject_extension_badge_helper',
75 [chrome.i18n.getMessage('appName')]));
76
77 var badgeI = document.createElement('i');
78 badgeI.classList.add(
79 'material-icon-i', 'material-icons-extended');
80 badgeI.textContent = 'repeat';
81
82 badge.appendChild(badgeI);
83 container.appendChild(badge);
84
85 var linkContainer = document.createElement('div');
86 linkContainer.classList.add('TWPT-previous-posts--links');
87
88 addProfileHistoryLink(linkContainer, 'forum', query1);
89 addProfileHistoryLink(linkContainer, 'all', query2);
90
91 container.appendChild(linkContainer);
92
93 node.querySelector('.main-card-content').appendChild(container);
avm99963d0757252019-03-30 20:13:00 +010094 }
95 }
96 }
avm99963847ee632019-03-27 00:57:44 +010097 });
98 }
99 });
100}
101
avm99963adf90862020-04-12 13:27:45 +0200102function intersectionCallback(entries, observer) {
avm99963847ee632019-03-27 00:57:44 +0100103 entries.forEach(entry => {
104 if (entry.isIntersecting) {
105 entry.target.click();
106 }
107 });
108};
109
110var observerOptions = {
111 childList: true,
112 attributes: true,
avm99963b69eb3d2020-08-20 02:03:44 +0200113 subtree: true,
avm99963847ee632019-03-27 00:57:44 +0100114}
115
avm99963129fb502020-08-28 05:18:53 +0200116var intersectionOptions = {
117 root: document.querySelector('.scrollable-content'),
118 rootMargin: '0px',
119 threshold: 1.0,
120};
avm99963847ee632019-03-27 00:57:44 +0100121
avm99963129fb502020-08-28 05:18:53 +0200122chrome.storage.sync.get(null, function(items) {
123 options = items;
avm99963cbea3142019-03-28 00:48:15 +0100124
avm99963a2945b62020-11-27 00:32:02 +0100125 var startup =
126 JSON.parse(document.querySelector('html').getAttribute('data-startup'));
127 authuser = startup[2][1] || '0';
128
avm99963129fb502020-08-28 05:18:53 +0200129 mutationObserver = new MutationObserver(mutationCallback);
130 mutationObserver.observe(
131 document.querySelector('.scrollable-content'), observerOptions);
avm99963cbea3142019-03-28 00:48:15 +0100132
avm99963129fb502020-08-28 05:18:53 +0200133 intersectionObserver =
134 new IntersectionObserver(intersectionCallback, intersectionOptions);
avm99963122dc9b2019-03-30 18:44:18 +0100135
avm99963129fb502020-08-28 05:18:53 +0200136 if (options.fixedtoolbar) {
137 injectStyles(
avm999630bc113a2020-09-07 13:02:11 +0200138 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
avm99963129fb502020-08-28 05:18:53 +0200139 }
avm99963ae6a26d2020-04-12 14:03:51 +0200140
avm99963129fb502020-08-28 05:18:53 +0200141 if (options.increasecontrast) {
avm999630bc113a2020-09-07 13:02:11 +0200142 injectStyles(
avm99963a2a06442020-11-25 21:11:10 +0100143 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
avm99963129fb502020-08-28 05:18:53 +0200144 }
avm999630f9503f2020-07-27 13:56:52 +0200145
avm99963129fb502020-08-28 05:18:53 +0200146 if (options.stickysidebarheaders) {
147 injectStyles(
avm999630bc113a2020-09-07 13:02:11 +0200148 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
149 }
150
151 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
152 injectStylesheet(
153 chrome.runtime.getURL('injections/ccdarktheme_switch.css'));
154
155 var darkThemeSwitch = document.createElement('material-button');
156 darkThemeSwitch.classList.add('TWPT-dark-theme');
157 darkThemeSwitch.setAttribute('button', '');
158 darkThemeSwitch.setAttribute(
159 'title', chrome.i18n.getMessage('inject_ccdarktheme_helper'));
160
161 darkThemeSwitch.addEventListener('click', e => {
162 chrome.storage.sync.get(null, currentOptions => {
163 currentOptions.ccdarktheme_switch_status =
164 !options.ccdarktheme_switch_status;
165 chrome.storage.sync.set(currentOptions, _ => {
166 location.reload();
167 });
168 });
169 });
170
171 var switchContent = document.createElement('div');
172 switchContent.classList.add('content');
173
174 var icon = document.createElement('material-icon');
175
176 var i = document.createElement('i');
177 i.classList.add('material-icon-i', 'material-icons-extended');
178 i.textContent = 'brightness_4';
179
180 icon.appendChild(i);
181 switchContent.appendChild(icon);
182 darkThemeSwitch.appendChild(switchContent);
183
avm9996306167752020-09-08 00:50:36 +0200184 var badgeContent = document.createElement('div');
185 badgeContent.classList.add('TWPT-badge');
186 badgeContent.setAttribute(
187 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [
188 chrome.i18n.getMessage('appName')
189 ]));
190
191 var badgeI = document.createElement('i');
192 badgeI.classList.add('material-icon-i', 'material-icons-extended');
193 badgeI.textContent = 'repeat';
194
195 badgeContent.appendChild(badgeI);
196 darkThemeSwitch.appendChild(badgeContent);
197
avm999630bc113a2020-09-07 13:02:11 +0200198 var rightControl = document.querySelector('header .right-control');
199 rightControl.style.width =
200 (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px';
201 rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch);
avm99963129fb502020-08-28 05:18:53 +0200202 }
avm99963129942f2020-09-08 02:07:18 +0200203
204 if (options.ccforcehidedrawer) {
205 var drawer = document.querySelector('material-drawer');
206 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
207 document.querySelector('.material-drawer-button').click();
208 }
209 }
avm99963129fb502020-08-28 05:18:53 +0200210});