From 8e8fa817a9bbe9b3a2a9fdcacf72bb8f4b761c82 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Mon, 23 Mar 2026 15:35:21 +0100 Subject: [PATCH] archive old.reddit links when given new reddit link --- reddit-sub.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 reddit-sub.js diff --git a/reddit-sub.js b/reddit-sub.js new file mode 100644 index 0000000..dc77611 --- /dev/null +++ b/reddit-sub.js @@ -0,0 +1,61 @@ +class RedditNewOld +{ + // required: an id for this behavior, will be displayed in the logs + // when the behavior is run. + static id = "reddit-new-old"; + + // required: a function that checks if a behavior should be run + // for a given page. + // This function can check the DOM / window.location to determine + // what page it is on. The first behavior that returns 'true' + // for a given page is used on that page. + static isMatch() { + return window.location.href.match( + /https:\/\/(www\.)?reddit\.com\/r\/[A-Za-z0-9]+(\/?$|best|hot|new|rising|top\/?(\?t=(hour|day|week|month|year|all))?|\/comments\/.+)$/ + ); + } + + // required: typically should be left as-is. + // must return an object, with optional fields: + // state: passes fixed state to behavior context (ctx) + // opts: passes options to behavior context (ctx). this may be + // useful when injecting behaviors directly into the browser, and + // provides a basis for passing options to custom behaviors from + // browsertrix and archiveweb.page in the future. + static init() { + return {}; + } + + // optional: if true, will also check isMatch() and possibly run + // this behavior in each iframe. + // if false, or not defined, this behavior will be skipped for iframes. + static runInIframe = false; + + // optional: if defined, provides a way to define a custom way to determine + // when a page has finished loading beyond the standard 'load' event. + // + // if defined, the crawler will await 'awaitPageLoad()' before moving on to + // post-crawl processing operations, including link extraction, screenshots, + // and running main behavior + async awaitPageLoad() { + + } + + // required: the main behavior async iterator, which should yield for + // each 'step' in the behavior. + // When the iterator finishes, the behavior is done. + // (See below for more info) + async* run(ctx) { + const { Lib } = ctx; + + // archive the old view of the subreddit as well + const url = new URL(window.location.href) + if (url.hostname === "reddit.com" || url.hostname === "www.reddit.com") { + url.hostname = "old.reddit.com"; + } + const old_url = url.toString(); + + await Lib.addLink(old_url); + yield Lib.getState(ctx, "old-reddit link added to queue"); + } +}