blob: eda996034bc98203a491bd46d134c7bd09e69611 [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
avm99963847ee632019-03-27 00:57:44 +010017function mutationCallback(mutationList, observer) {
18 mutationList.forEach((mutation) => {
avm99963b69eb3d2020-08-20 02:03:44 +020019 if (mutation.type == 'childList') {
20 mutation.addedNodes.forEach(function(node) {
21 if (typeof node.classList !== 'undefined') {
22 if (options.thread && node.classList.contains('load-more-bar')) {
23 intersectionObserver.observe(
24 node.querySelector('.load-more-button'));
avm99963d0757252019-03-30 20:13:00 +010025 }
26
avm99963b69eb3d2020-08-20 02:03:44 +020027 if (options.threadall && node.classList.contains('load-more-bar')) {
28 intersectionObserver.observe(
29 node.querySelector('.load-all-button'));
avm999636d9c5fe2019-06-04 00:35:53 +020030 }
31
avm99963b69eb3d2020-08-20 02:03:44 +020032 if (options.history && ('parentNode' in node) &&
33 node.parentNode !== null && ('tagName' in node.parentNode) &&
34 node.parentNode.tagName == 'EC-USER') {
35 var nameElement = node.querySelector('.name span');
avm99963d0757252019-03-30 20:13:00 +010036 if (nameElement !== null) {
avm9996318d043a2020-05-29 12:20:47 +020037 var name = nameElement.innerHTML;
avm99963b69eb3d2020-08-20 02:03:44 +020038 var query = encodeURIComponent(
39 '(creator:"' + name + '" | replier:"' + name + '") -forum:0');
40 var urlpart = encodeURIComponent('query=' + query);
41 var link = document.createElement('a');
42 link.setAttribute(
43 'href',
44 'https://support.google.com/s/community/search/' + urlpart);
45 link.innerText = chrome.i18n.getMessage('inject_previousposts');
46 node.querySelector('.main-card-content')
47 .appendChild(document.createElement('br'));
48 node.querySelector('.main-card-content').appendChild(link);
avm99963d0757252019-03-30 20:13:00 +010049 }
50 }
51 }
avm99963847ee632019-03-27 00:57:44 +010052 });
53 }
54 });
55}
56
avm99963adf90862020-04-12 13:27:45 +020057function intersectionCallback(entries, observer) {
avm99963847ee632019-03-27 00:57:44 +010058 entries.forEach(entry => {
59 if (entry.isIntersecting) {
60 entry.target.click();
61 }
62 });
63};
64
65var observerOptions = {
66 childList: true,
67 attributes: true,
avm99963b69eb3d2020-08-20 02:03:44 +020068 subtree: true,
avm99963847ee632019-03-27 00:57:44 +010069}
70
avm99963129fb502020-08-28 05:18:53 +020071var intersectionOptions = {
72 root: document.querySelector('.scrollable-content'),
73 rootMargin: '0px',
74 threshold: 1.0,
75};
avm99963847ee632019-03-27 00:57:44 +010076
avm99963129fb502020-08-28 05:18:53 +020077chrome.storage.sync.get(null, function(items) {
78 options = items;
avm99963cbea3142019-03-28 00:48:15 +010079
avm99963129fb502020-08-28 05:18:53 +020080 mutationObserver = new MutationObserver(mutationCallback);
81 mutationObserver.observe(
82 document.querySelector('.scrollable-content'), observerOptions);
avm99963cbea3142019-03-28 00:48:15 +010083
avm99963129fb502020-08-28 05:18:53 +020084 intersectionObserver =
85 new IntersectionObserver(intersectionCallback, intersectionOptions);
avm99963122dc9b2019-03-30 18:44:18 +010086
avm99963129fb502020-08-28 05:18:53 +020087 if (options.fixedtoolbar) {
88 injectStyles(
89 'ec-bulk-actions{position: sticky; top: 0; background: white; z-index: 96;}');
90 }
avm99963ae6a26d2020-04-12 14:03:51 +020091
avm99963129fb502020-08-28 05:18:53 +020092 if (options.increasecontrast) {
93 injectStyles('.thread-summary.read{background: #ecedee!important;}');
94 }
avm999630f9503f2020-07-27 13:56:52 +020095
avm99963129fb502020-08-28 05:18:53 +020096 if (options.stickysidebarheaders) {
97 injectStyles(
98 'material-drawer .main-header{background: #fff; position: sticky; top: 0; z-index: 1;}');
99 }
100
101 if (options.profileindicator) {
avm99963a560aba2020-08-31 13:32:29 +0200102 injectScript(
avm99963e51444e2020-08-31 14:50:06 +0200103 chrome.runtime.getURL('injections/profileindicator_inject.js'));
104 injectStylesheet(
105 chrome.runtime.getURL('injections/profileindicator_inject.css'));
avm99963129fb502020-08-28 05:18:53 +0200106
107 // In order to pass i18n strings to the injected script, which doesn't have
108 // access to the chrome.i18n API.
109 window.addEventListener('geti18nString', evt => {
110 var request = evt.detail;
111 var response = {
112 string: chrome.i18n.getMessage(request.msg),
113 requestId: request.id
114 };
avm99963a560aba2020-08-31 13:32:29 +0200115 window.dispatchEvent(
116 new CustomEvent('sendi18nString', {detail: response}));
avm99963b69eb3d2020-08-20 02:03:44 +0200117 });
avm99963129fb502020-08-28 05:18:53 +0200118 }
119});