ireadit

A browser extension that hides the comment section of link posts on reddit and hacker news if the article has not been read.


ireadit

/

ireadit

/

reddit-old.js

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
var ireadit = {
  link: ''
};
init();

function init()
{
  check_link();
}

function check_link()
{
  let ctx = document.getElementById('siteTable');
  ctx = ctx.firstChild;

  if (! ctx) return;

  let link = ctx.getAttribute('data-url');

  let regex = RegExp('^http[s]{0,1}://[^\r]+$', 'gi');
  let match = regex.test(link);
  if (! match) return;

  ireadit.link = link;
  check_history(link);
}

function check_history(url)
{
  let req = {
    type: 'check_history',
    url: url
  };

  chrome.runtime.sendMessage(req, (res) => {
    if (! res.url)
    {
      hide_comments();
    }
  });
}

function hide_comments()
{
  let ctx = document.getElementsByClassName('commentarea')[0];
  if (ctx)
  {
    let el_msg =
    el('div', [['id', 'ireadit-note']], '', [
      el('p', [], '', ['Comments Hidden by <a href="https://octobanana.com/software/ireadit" title="ireadit">ireadit</a>: Read the <a href="' + ireadit.link + '" title="' + ireadit.link + '">article</a> to view the comment section, or <a href="" title="view comments" id="ireadit-toggle">click here</a>.'])
    ]);
    ctx.insertAdjacentElement("afterend", el_msg);

    let toggle = document.getElementById('ireadit-toggle');
    if (toggle)
    {
      toggle.onclick = () => {
        show_comments();
        return false;
      }
    }
  }

  let el_css = el('style', [['id', 'ireadit-css'], ['type', 'text/css']], `
    #ireadit-note {
      padding: 1rem;
      font-size: 1rem;
    }
    #ireadit-note a {
      cursor: pointer;
      font-weight: bold;
    }
    #ireadit-note a:hover {
      text-decoration: underline;
    }
    .commentarea {
      display: none !important;
    }
    ` , [])
  document.body.appendChild(el_css);
}

function show_comments()
{
  let note = document.getElementById('ireadit-note');
  if (note)
  {
    note.parentNode.removeChild(note);
  }

  let css = document.getElementById('ireadit-css');
  if (css)
  {
    css.parentNode.removeChild(css);
  }
}

function el(tag, attr, text, children)
{
  let root = document.createElement(tag);

  if (text.length > 0)
  {
    let t = document.createTextNode(text);
    root.appendChild(t);
  }

  for (e of attr)
  {
    if (e.length == 1)
    {
      root.setAttribute(e[0], '');
    }
    else if (e.length == 2)
    {
      root.setAttribute(e[0], e[1]);
    }
  }

  for (e of children)
  {
    if (typeof e === 'string' || e instanceof String)
    {
      root.innerHTML += e;
    }
    else
    {
      root.appendChild(e);
    }
  }

  return root;
}
Back to Top