HN: style unread content

Save hashes of displayed comments locally and mark new ones when displayed for the first time

目前為 2021-04-13 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        HN: style unread content 
// @description Save hashes of displayed comments locally and mark new ones when displayed for the first time
// @namespace   myfonj
// @match       https://news.ycombinator.com/*
// @grant       none
// @version     1.0.2
// @author      myfonj
// ==/UserScript==

// https://greasyfork.org/en/scripts/423969/versions/new

// Styles. Just lame green-transparent right border, for now.

const VISIBILITY_DURATION_MS = 900;
const CLASSES = { new: 'new', read: 'read', old: 'old' };
document.head.appendChild(document.createElement('style')).textContent = `
.${CLASSES.new}  { border-right: 2px solid #3F36; display: block; padding-right: 1em; }
.${CLASSES.read} {	border-right: 2px solid #0F03; display: block; padding-right: 1em; }
.${CLASSES.old}  { /* nothing */ }
`;
const HASH_DIGEST_ALGO = 'SHA-1'; // 'SHA-256';
const MAP_EL_HASH = new WeakMap();
const MAP_EL_TIMEOUT = new WeakMap();
const LS_KEY = 'displayed_hashes_' + HASH_DIGEST_ALGO;
const SEEN_HASH_LIST = new Set((localStorage.getItem(LS_KEY) || '').split(','));
const ELS_TO_WATCH = document.querySelectorAll('.commtext,.storylink');
const ENTRY_CHECKER = (entry) => {
  const TGT = entry.target;
  if (entry.isIntersecting) {
    if (MAP_EL_TIMEOUT.get(TGT)) {
      // don't set twice on re-entry
      return
    }
    MAP_EL_TIMEOUT.set(
      TGT,
      window.setTimeout(function () {
        if (MAP_EL_TIMEOUT.get(TGT)) {
          TGT.classList.remove(CLASSES.new);
          TGT.classList.add(CLASSES.read);
          SEEN_HASH_LIST.add(MAP_EL_HASH.get(TGT))
          MAP_EL_TIMEOUT.delete(TGT);
          VIEWPORT_OBSERVER.unobserve(TGT);
          // TODO move the saving to LS to unload and/or blur for fewer writes
          localStorage.setItem(
            LS_KEY,
            Array.from(SEEN_HASH_LIST).join(',')
          );
        }
      }, VISIBILITY_DURATION_MS)
    );
  } else {
    MAP_EL_TIMEOUT.delete(TGT);
  }
}
const VIEWPORT_OBSERVER = new IntersectionObserver(
	(entries, observer) => {
		entries.forEach(_=>ENTRY_CHECKER(_));
	},
	{
		root: null,
		rootMargin: "-9%", // TODO use "lines" height here?
		threshold: 0
	}
);
ELS_TO_WATCH.forEach(async el => {
	const hash = await makeHash(el.textContent);
	if (SEEN_HASH_LIST.has(hash)) {
		el.classList.add(CLASSES.old);
		return;
	}
	el.classList.add(CLASSES.new);
	MAP_EL_HASH.set(el, hash);
	VIEWPORT_OBSERVER.observe(el);
});
async function makeHash (input) {
	return btoa(
		String.fromCharCode.apply(
			null,
			new Uint8Array(
				await crypto.subtle.digest(
					HASH_DIGEST_ALGO,
					(new TextEncoder()).encode(input)
				)
			)
		)
	);
};