Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Two Column HN Frontpage for Firefox/Chrome
2 points by brudgers on Dec 2, 2016 | hide | past | favorite | 1 comment
A GreaseMonkey [Firefox] or TamperMonkey [Chrome/Chromium] script to create a two column layout on the Hacker News front page. Useful to avoid scrolling. Probably not useful for much else.

    // ==UserScript==
    // @name        hmMainPage
    // @namespace   com.kludgecode.hn.demo
    // @include     https://news.ycombinator.com/news
    // @version     1
    // @grant       none
    // ==/UserScript==

    (function () {
      // itemList[0] is the target document element
      var itemList = document.getElementsByClassName('itemlist')[0];
      // r is the container for the content we build
      var r = document.createElement('tr');
      // it will display two columns
      var left = document.createElement('td');
      var right = document.createElement('td');
      // An array of the first row of each item
      var row1 = Array.prototype.slice.call(document.getElementsByClassName('athing'), 0);
      // the second row of each item; a table body that wraps both rows; a table that wraps the table body
      var row2 = []; tb = []; t = [];
      // control the loops
      var len = row1.length;

      for (var i = 0; i < len; i++) {
        row2.push(row1[i].nextElementSibling);
        tb.push(document.createElement('tb'));
        t.push(document.createElement('table'));
      };
      
      for (var i = 0; i < len; i++) {
        tb[i].appendChild(row1[i]);
      };
 
      for (var i = 0; i < len; i++) {
        tb[i].appendChild(row2[i]);
      };
 
      for (var i = 0; i < len; i++) {
        t[i].appendChild(tb[i]);
      };
 
      for (var i = 0; i < len / 2; i++) {
        left.appendChild(t[i])
      };
 
      for (var i = len / 2; i < len; i++) {
        right.appendChild(t[i])
      };
 
      r.appendChild(left);
      r.appendChild(right);
 
      itemList.innerHTML = "";
 
      itemList.append(r)
    })();



I wrote too much, imagine that, and had to remove some comments to comply with 2000 character limit. Nothing magical, just a kludge.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: