Fresh Lobsters

Highlights fresh comments on Lobste.rs.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          Fresh Lobsters
// @version       0.0.3
// @namespace     http://userscripts.psbarrett.com
// @description	  Highlights fresh comments on Lobste.rs.
// @include       https://lobste.rs/*
// @grant         GM_addStyle
// ==/UserScript==
'use strict'

// Workaround Backwards Incompatible Change
if (typeof GM_addStyle == 'undefined') {
  var GM_addStyle = (aCss) => {
    'use strict';
    let head = document.getElementsByTagName('head')[0];
    if (head) {
      let style = document.createElement('style');
      style.setAttribute('type', 'text/css');
      style.textContent = aCss;
      head.appendChild(style);
      return style;
    }
    return null;
  };
}

GM_addStyle(
`
.byline.fresh-item > span,
.byline a.fresh-item {
  color:red;
}
`);

function get_comment_id(byline) {
  let a = byline.querySelector('a');
  return a.name;
}

function get_comment_time(byline) {
  let item_link_ele = byline.querySelector('span');
  return item_link_ele.title;
}

//
// Comments
//
let all_byline = document.querySelectorAll('.comment .byline');

for (let byline of all_byline) {
  let comment_id = get_comment_id(byline);
  let comment_time = get_comment_time(byline);
  
  if (comment_id !== null && comment_time !== null) {
    var status = localStorage.getItem(comment_id);

    if (status === null || comment_time !== status) {
      byline.classList.add("fresh-item");
      localStorage.setItem(comment_id, comment_time);
    }
  }
}

//
// Comment Links
//
let stories = document.querySelectorAll('.stories li');

for (let story of stories) {
  let story_id = story.dataset.shortid;
  let comment_link = story.querySelector('.byline .comments_label a');
  let story_comment_count = comment_link.textContent.trim().split(" ")[0].trim();

  // If Parsed Properly, Check Count, Mark Changed
  if (story_id !== null && story_comment_count !== null && story_comment_count !== 'no') {
    var status = localStorage.getItem(story_id);

    if (status !== null && story_comment_count !== status) {
      comment_link.classList.add("fresh-item");
    }
  }

  // Save Story Comment Count when Visited
  if (stories.length == 1) {
    localStorage.setItem(story_id, story_comment_count);
  }
}