archive old.reddit links when given new reddit link

This commit is contained in:
2026-03-23 15:35:21 +01:00
commit 8e8fa817a9

61
reddit-sub.js Normal file
View File

@@ -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");
}
}