aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/reddit/webview.js
blob: e8498ac34d8d9a4f257357a34377a75806eabd01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const _path = _interopRequireDefault(require('path'));

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

module.exports = Ferdium => {
  const getMessages = () => {
    const elements = document.querySelectorAll(
      '#HeaderUserActions--Messages > a > span',
    );
    let count = 0;

    if (elements[0]) {
      count = Ferdium.safeParseInt(elements[0].textContent);
    }

    Ferdium.setBadge(count);
  };

  if (document.querySelectorAll('.promotedlink').length > 0) {
    for (const sponsoredLink of document.querySelectorAll('.promotedlink')) {
      if (
        sponsoredLink.parentElement &&
        sponsoredLink.parentElement.parentElement
      ) {
        sponsoredLink.parentElement.parentElement.style.display = 'none';
      }
    }
  }

  Ferdium.loop(getMessages);

  // Use CSS to hide Google Ads
  Ferdium.injectCSS(_path.default.join(__dirname, 'service.css'));

  Ferdium.handleDarkMode(isEnabled => {
    // Open dropdown menu if not already open
    const menu = document.querySelector('#USER_DROPDOWN_ID');
    if (menu && menu.getAttribute('aria-expanded') === 'false') {
      menu.click();
    }

    setTimeout(() => {
      // Check if service is already in right mode
      const btn = document.querySelector('[role=menu] button button');
      const checked = btn && btn.getAttribute('aria-checked') === 'true';

      if (
        ((checked && !isEnabled) || (!checked && isEnabled)) && // Click the button to switch between modes
        btn
      ) {
        btn.click();
      }
    }, 50);
  });
};