blob: eb0975b735a49fed22b06f1cdd9b9b44c30a8c98 [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(
103 'ec-bulk-actions{position: sticky; top: 0; background: white; z-index: 96;}');
104 }
avm99963ae6a26d2020-04-12 14:03:51 +0200105
avm99963129fb502020-08-28 05:18:53 +0200106 if (options.increasecontrast) {
107 injectStyles('.thread-summary.read{background: #ecedee!important;}');
108 }
avm999630f9503f2020-07-27 13:56:52 +0200109
avm99963129fb502020-08-28 05:18:53 +0200110 if (options.stickysidebarheaders) {
111 injectStyles(
112 'material-drawer .main-header{background: #fff; position: sticky; top: 0; z-index: 1;}');
113 }
avm99963129fb502020-08-28 05:18:53 +0200114});