blob: 14556b814151911169d2d5716a0ecaa6331b8fe1 [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(
56 '(creator:"' + name + '" | replier:"' + name + '") forum:'+forumId);
57 var query2 = encodeURIComponent(
58 '(creator:"' + name + '" | replier:"' + name + '") forum:any');
59 addProfileHistoryLink(node, 'forum', query1);
60 addProfileHistoryLink(node, 'all', query2);
avm99963d0757252019-03-30 20:13:00 +010061 }
62 }
63 }
avm99963847ee632019-03-27 00:57:44 +010064 });
65 }
66 });
67}
68
avm99963adf90862020-04-12 13:27:45 +020069function intersectionCallback(entries, observer) {
avm99963847ee632019-03-27 00:57:44 +010070 entries.forEach(entry => {
71 if (entry.isIntersecting) {
72 entry.target.click();
73 }
74 });
75};
76
77var observerOptions = {
78 childList: true,
79 attributes: true,
avm99963b69eb3d2020-08-20 02:03:44 +020080 subtree: true,
avm99963847ee632019-03-27 00:57:44 +010081}
82
avm99963129fb502020-08-28 05:18:53 +020083var intersectionOptions = {
84 root: document.querySelector('.scrollable-content'),
85 rootMargin: '0px',
86 threshold: 1.0,
87};
avm99963847ee632019-03-27 00:57:44 +010088
avm99963129fb502020-08-28 05:18:53 +020089chrome.storage.sync.get(null, function(items) {
90 options = items;
avm99963cbea3142019-03-28 00:48:15 +010091
avm99963129fb502020-08-28 05:18:53 +020092 mutationObserver = new MutationObserver(mutationCallback);
93 mutationObserver.observe(
94 document.querySelector('.scrollable-content'), observerOptions);
avm99963cbea3142019-03-28 00:48:15 +010095
avm99963129fb502020-08-28 05:18:53 +020096 intersectionObserver =
97 new IntersectionObserver(intersectionCallback, intersectionOptions);
avm99963122dc9b2019-03-30 18:44:18 +010098
avm99963129fb502020-08-28 05:18:53 +020099 if (options.fixedtoolbar) {
100 injectStyles(
101 'ec-bulk-actions{position: sticky; top: 0; background: white; z-index: 96;}');
102 }
avm99963ae6a26d2020-04-12 14:03:51 +0200103
avm99963129fb502020-08-28 05:18:53 +0200104 if (options.increasecontrast) {
105 injectStyles('.thread-summary.read{background: #ecedee!important;}');
106 }
avm999630f9503f2020-07-27 13:56:52 +0200107
avm99963129fb502020-08-28 05:18:53 +0200108 if (options.stickysidebarheaders) {
109 injectStyles(
110 'material-drawer .main-header{background: #fff; position: sticky; top: 0; z-index: 1;}');
111 }
112
113 if (options.profileindicator) {
avm99963a560aba2020-08-31 13:32:29 +0200114 injectScript(
avm99963e51444e2020-08-31 14:50:06 +0200115 chrome.runtime.getURL('injections/profileindicator_inject.js'));
116 injectStylesheet(
117 chrome.runtime.getURL('injections/profileindicator_inject.css'));
avm99963129fb502020-08-28 05:18:53 +0200118
119 // In order to pass i18n strings to the injected script, which doesn't have
120 // access to the chrome.i18n API.
121 window.addEventListener('geti18nString', evt => {
122 var request = evt.detail;
123 var response = {
124 string: chrome.i18n.getMessage(request.msg),
125 requestId: request.id
126 };
avm99963a560aba2020-08-31 13:32:29 +0200127 window.dispatchEvent(
128 new CustomEvent('sendi18nString', {detail: response}));
avm99963b69eb3d2020-08-20 02:03:44 +0200129 });
avm99963129fb502020-08-28 05:18:53 +0200130 }
131});