HN highlight fresh comments

Highlights fresh comments on Hacker News.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        HN highlight fresh comments
// @description Highlights fresh comments on Hacker News.
// @version     1
// @namespace   https://greasyfork.org/en/scripts/6955-hacker-fresh
// @include     http://news.ycombinator.com/*
// @include     https://news.ycombinator.com/*
// @grant       GM_addStyle
// ==/UserScript==

GM_addStyle(".fresh-item { background-color: #FEE67F; border-radius: 3px; }");

(function() {
  'use strict';
  function get_comment_id(vote_box) {
    var item_link_ele = vote_box.parentElement.querySelector('.comhead > a:nth-child(2)');

    // Check for deleted comment
    if (item_link_ele === null) {
      return null;
    }

    var link = new URL(item_link_ele.href);
    return link.searchParams.get('id');
  }

  var all_vote_box = document.querySelectorAll('tbody > tr > td[valign=top]');

  for (var e of all_vote_box) {
    e.classList.add("vote-box");

    var item_id = get_comment_id(e);
    if (item_id !== null) {
      var status = localStorage.getItem(item_id);

      if (status === null) {
        e.classList.add("fresh-item");
        localStorage.setItem(item_id, "seen");
      }
    }
  }
})();