Search... Ctrl + K

Trigger everytime a new item text on HackerNews match at least one mention (hackernews) - trigger module
One script reply has been approved by the moderators Verified

Created by admin 128 days ago Used 71 times 1 Point

For every new item since last trigger (as identified by being above the last max ID), collect the ones that match at least of the mention

No comments yet

Points: 0
One script reply has been approved by the moderators
import * as wmill from "";

const MAX_LOOKBACK = 100

export async function main(mentions: string[]) {
  let lastState = await wmill.getInternalState();
  console.log(`lastState: ${lastState}`);

  let maxItem = await getMaxItem();
  console.log(`maxItem: ${maxItem}`);

  if (!lastState) {
    console.log(`First run of trigger, looking back to MAX_LOOKBACK (100) elements`);
    lastState = maxItem - MAX_LOOKBACK;

  maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK)

  const items = [];
  for (let i = lastState; i < maxItem; i++) {
    console.log(`fetching id: ${i}`);
    const item = await getItem(i)
    if (mentions.find((mention) => item.text?.includes(mention))) {
  await wmill.setInternalState(maxItem);

  return items;

export async function getMaxItem() {
  const res = await fetch("");
  return Number(await res.text());

export async function getItem(id: number) {
  const res = await fetch(
  return res.json();

Submitted by admin 128 days ago

Edited 128 days ago

No comments yet