blob: 2e2554a7fd4c19dc5bde6df49a6a176bdd4b3951 [file] [log] [blame]
avm99963cbea3142019-03-28 00:48:15 +01001var intersectionObserver;
2
avm99963e51444e2020-08-31 14:50:06 +02003function injectStylesheet(stylesheetName) {
4 var link = document.createElement('link');
5 link.setAttribute('rel', 'stylesheet');
6 link.setAttribute('href', stylesheetName);
7 document.head.appendChild(link);
8}
9
10function injectStyles(css) {
11 injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css));
12}
13
14function injectScript(scriptName) {
15 var script = document.createElement('script');
16 script.src = scriptName;
17 document.head.appendChild(script);
18}
19
avm99963cbea3142019-03-28 00:48:15 +010020function intersectionCallback(entries, observer) {
21 entries.forEach(entry => {
22 if (entry.isIntersecting) {
23 entry.target.click();
24 }
25 });
26};
27
28var intersectionOptions = {
avm99963b69eb3d2020-08-20 02:03:44 +020029 threshold: 1.0,
30};
avm99963cbea3142019-03-28 00:48:15 +010031
32chrome.storage.sync.get(null, function(items) {
avm99963b69eb3d2020-08-20 02:03:44 +020033 var path = document.location.pathname.split('/');
34 if (path[path.length - 1] == 'new' ||
35 (path.length > 1 && path[path.length - 1] == '' &&
36 path[path.length - 2] == 'new')) {
avm999637db655f2020-08-18 11:44:31 +020037 return;
38 }
39
avm99963b69eb3d2020-08-20 02:03:44 +020040 var redirectLink = document.querySelector('.community-console');
avm99963134f1ef2019-03-30 19:05:27 +010041 if (items.redirect && redirectLink !== null) {
42 window.location = redirectLink.href;
43 } else {
avm999637db655f2020-08-18 11:44:31 +020044 var button =
avm99963b69eb3d2020-08-20 02:03:44 +020045 document.querySelector('.thread-all-replies__load-more-button');
avm99963134f1ef2019-03-30 19:05:27 +010046 if (items.thread && button !== null) {
avm999637db655f2020-08-18 11:44:31 +020047 intersectionObserver =
48 new IntersectionObserver(intersectionCallback, intersectionOptions);
avm99963134f1ef2019-03-30 19:05:27 +010049 intersectionObserver.observe(button);
50 }
avm999637db655f2020-08-18 11:44:31 +020051 var allbutton =
avm99963b69eb3d2020-08-20 02:03:44 +020052 document.querySelector('.thread-all-replies__load-all-button');
avm999636d9c5fe2019-06-04 00:35:53 +020053 if (items.threadall && button !== null) {
avm999637db655f2020-08-18 11:44:31 +020054 intersectionObserver =
55 new IntersectionObserver(intersectionCallback, intersectionOptions);
avm999636d9c5fe2019-06-04 00:35:53 +020056 intersectionObserver.observe(allbutton);
57 }
avm99963e51444e2020-08-31 14:50:06 +020058
59 if (items.profileindicator) {
60 injectScript(
61 chrome.runtime.getURL('injections/profileindicator_inject.js'));
62 injectStylesheet(
63 chrome.runtime.getURL('injections/profileindicator_inject.css'));
64
65 // In order to pass i18n strings to the injected script, which doesn't
66 // have access to the chrome.i18n API.
67 window.addEventListener('geti18nString', evt => {
68 var request = evt.detail;
69 var response = {
70 string: chrome.i18n.getMessage(request.msg),
71 requestId: request.id
72 };
73 window.dispatchEvent(
74 new CustomEvent('sendi18nString', {detail: response}));
75 });
76 }
avm99963cbea3142019-03-28 00:48:15 +010077 }
78});