blob: 4dba6141d106cfa99d412167317354bb897b45b2 [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
avm999638e0c1002020-12-03 16:54:20 +010035function applyDragAndDropFix(node) {
36 console.debug('Adding link drag&drop fix to ', node);
37 node.addEventListener('drop', e => {
38 if (e.dataTransfer.types.includes('text/uri-list')) {
39 e.stopImmediatePropagation();
40 console.debug('Stopping link drop event propagation.');
41 }
42 }, true);
43}
44
avm99963847ee632019-03-27 00:57:44 +010045function mutationCallback(mutationList, observer) {
46 mutationList.forEach((mutation) => {
avm99963b69eb3d2020-08-20 02:03:44 +020047 if (mutation.type == 'childList') {
48 mutation.addedNodes.forEach(function(node) {
49 if (typeof node.classList !== 'undefined') {
50 if (options.thread && node.classList.contains('load-more-bar')) {
51 intersectionObserver.observe(
52 node.querySelector('.load-more-button'));
avm99963d0757252019-03-30 20:13:00 +010053 }
54
avm99963b69eb3d2020-08-20 02:03:44 +020055 if (options.threadall && node.classList.contains('load-more-bar')) {
56 intersectionObserver.observe(
57 node.querySelector('.load-all-button'));
avm999636d9c5fe2019-06-04 00:35:53 +020058 }
59
avm99963b69eb3d2020-08-20 02:03:44 +020060 if (options.history && ('parentNode' in node) &&
61 node.parentNode !== null && ('tagName' in node.parentNode) &&
62 node.parentNode.tagName == 'EC-USER') {
63 var nameElement = node.querySelector('.name span');
avm99963d0757252019-03-30 20:13:00 +010064 if (nameElement !== null) {
avm99963943b8492020-08-31 23:40:43 +020065 var forumId =
66 location.href.split('/forum/')[1].split('/')[0] || '0';
67
avm9996314116b02020-11-02 14:04:14 +010068 var name = escapeUsername(nameElement.textContent);
avm99963943b8492020-08-31 23:40:43 +020069 var query1 = encodeURIComponent(
avm99963ad65e752020-09-01 00:13:59 +020070 '(creator:"' + name + '" | replier:"' + name +
71 '") forum:' + forumId);
avm99963943b8492020-08-31 23:40:43 +020072 var query2 = encodeURIComponent(
avm99963ad65e752020-09-01 00:13:59 +020073 '(creator:"' + name + '" | replier:"' + name +
74 '") forum:any');
avm9996306167752020-09-08 00:50:36 +020075
76 var container = document.createElement('div');
77 container.classList.add('TWPT-previous-posts');
78
79 var badge = document.createElement('div');
80 badge.classList.add('TWPT-badge');
81 badge.setAttribute(
82 'title',
83 chrome.i18n.getMessage(
84 'inject_extension_badge_helper',
85 [chrome.i18n.getMessage('appName')]));
86
87 var badgeI = document.createElement('i');
88 badgeI.classList.add(
89 'material-icon-i', 'material-icons-extended');
90 badgeI.textContent = 'repeat';
91
92 badge.appendChild(badgeI);
93 container.appendChild(badge);
94
95 var linkContainer = document.createElement('div');
96 linkContainer.classList.add('TWPT-previous-posts--links');
97
98 addProfileHistoryLink(linkContainer, 'forum', query1);
99 addProfileHistoryLink(linkContainer, 'all', query2);
100
101 container.appendChild(linkContainer);
102
103 node.querySelector('.main-card-content').appendChild(container);
avm99963d0757252019-03-30 20:13:00 +0100104 }
105 }
avm999638e0c1002020-12-03 16:54:20 +0100106
107 // We target both tags because in different contexts different
108 // elements containing the text editor get added to the DOM structure.
109 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
110 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
111 // directly.
112 if (options.ccdragndropfix && ('tagName' in node) &&
113 (node.tagName == 'EC-MOVABLE-DIALOG' ||
114 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
115 applyDragAndDropFix(node);
116 }
avm99963d0757252019-03-30 20:13:00 +0100117 }
avm99963847ee632019-03-27 00:57:44 +0100118 });
119 }
120 });
121}
122
avm99963adf90862020-04-12 13:27:45 +0200123function intersectionCallback(entries, observer) {
avm99963847ee632019-03-27 00:57:44 +0100124 entries.forEach(entry => {
125 if (entry.isIntersecting) {
126 entry.target.click();
127 }
128 });
129};
130
131var observerOptions = {
132 childList: true,
avm99963b69eb3d2020-08-20 02:03:44 +0200133 subtree: true,
avm99963847ee632019-03-27 00:57:44 +0100134}
135
avm99963129fb502020-08-28 05:18:53 +0200136var intersectionOptions = {
137 root: document.querySelector('.scrollable-content'),
138 rootMargin: '0px',
139 threshold: 1.0,
140};
avm99963847ee632019-03-27 00:57:44 +0100141
avm99963129fb502020-08-28 05:18:53 +0200142chrome.storage.sync.get(null, function(items) {
143 options = items;
avm99963cbea3142019-03-28 00:48:15 +0100144
avm99963a2945b62020-11-27 00:32:02 +0100145 var startup =
146 JSON.parse(document.querySelector('html').getAttribute('data-startup'));
147 authuser = startup[2][1] || '0';
148
avm99963129fb502020-08-28 05:18:53 +0200149 mutationObserver = new MutationObserver(mutationCallback);
avm99963e4cac402020-12-03 16:10:58 +0100150 mutationObserver.observe(document.body, observerOptions);
avm99963cbea3142019-03-28 00:48:15 +0100151
avm99963129fb502020-08-28 05:18:53 +0200152 intersectionObserver =
153 new IntersectionObserver(intersectionCallback, intersectionOptions);
avm99963122dc9b2019-03-30 18:44:18 +0100154
avm99963129fb502020-08-28 05:18:53 +0200155 if (options.fixedtoolbar) {
156 injectStyles(
avm999630bc113a2020-09-07 13:02:11 +0200157 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
avm99963129fb502020-08-28 05:18:53 +0200158 }
avm99963ae6a26d2020-04-12 14:03:51 +0200159
avm99963129fb502020-08-28 05:18:53 +0200160 if (options.increasecontrast) {
avm999630bc113a2020-09-07 13:02:11 +0200161 injectStyles(
avm99963a2a06442020-11-25 21:11:10 +0100162 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
avm99963129fb502020-08-28 05:18:53 +0200163 }
avm999630f9503f2020-07-27 13:56:52 +0200164
avm99963129fb502020-08-28 05:18:53 +0200165 if (options.stickysidebarheaders) {
166 injectStyles(
avm999630bc113a2020-09-07 13:02:11 +0200167 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
168 }
169
170 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
171 injectStylesheet(
172 chrome.runtime.getURL('injections/ccdarktheme_switch.css'));
173
174 var darkThemeSwitch = document.createElement('material-button');
175 darkThemeSwitch.classList.add('TWPT-dark-theme');
176 darkThemeSwitch.setAttribute('button', '');
177 darkThemeSwitch.setAttribute(
178 'title', chrome.i18n.getMessage('inject_ccdarktheme_helper'));
179
180 darkThemeSwitch.addEventListener('click', e => {
181 chrome.storage.sync.get(null, currentOptions => {
182 currentOptions.ccdarktheme_switch_status =
183 !options.ccdarktheme_switch_status;
184 chrome.storage.sync.set(currentOptions, _ => {
185 location.reload();
186 });
187 });
188 });
189
190 var switchContent = document.createElement('div');
191 switchContent.classList.add('content');
192
193 var icon = document.createElement('material-icon');
194
195 var i = document.createElement('i');
196 i.classList.add('material-icon-i', 'material-icons-extended');
197 i.textContent = 'brightness_4';
198
199 icon.appendChild(i);
200 switchContent.appendChild(icon);
201 darkThemeSwitch.appendChild(switchContent);
202
avm9996306167752020-09-08 00:50:36 +0200203 var badgeContent = document.createElement('div');
204 badgeContent.classList.add('TWPT-badge');
205 badgeContent.setAttribute(
206 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [
207 chrome.i18n.getMessage('appName')
208 ]));
209
210 var badgeI = document.createElement('i');
211 badgeI.classList.add('material-icon-i', 'material-icons-extended');
212 badgeI.textContent = 'repeat';
213
214 badgeContent.appendChild(badgeI);
215 darkThemeSwitch.appendChild(badgeContent);
216
avm999630bc113a2020-09-07 13:02:11 +0200217 var rightControl = document.querySelector('header .right-control');
218 rightControl.style.width =
219 (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px';
220 rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch);
avm99963129fb502020-08-28 05:18:53 +0200221 }
avm99963129942f2020-09-08 02:07:18 +0200222
223 if (options.ccforcehidedrawer) {
224 var drawer = document.querySelector('material-drawer');
225 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
226 document.querySelector('.material-drawer-button').click();
227 }
228 }
avm99963129fb502020-08-28 05:18:53 +0200229});