Skip to the page
Social CrawlDocs

Site rules

Updated
View as Markdown

The data format of an extension site rule: every field, how a rule is matched to a page, the three kinds of selector, and how rules are stored, exported and imported.

On this page

A site rule tells the extension how to read the article of one website when its automatic reader gets it wrong. It is data only: CSS selectors and a few switches. It never runs code. The code is apps/extension/src/templates.ts and the reader that uses it is extract-article.ts.

You can make a rule with the mouse (see Site rules in the extension) or write it by hand under Settings → Site rules in the extension.

A rule#

JSON
{
  "name": "My blog",
  "hosts": ["blog.example.com"],
  "paths": "^/posts/",
  "root": "main article",
  "title": "h1.entry-title",
  "author": [".byline a", ".author"],
  "date": "time[datetime]",
  "remove": [".share-bar", ".related", "nav"],
  "map": [
    { "selector": ".callout", "as": "quote" },
    { "selector": ".big-heading", "as": "h2" }
  ],
  "expand": ["details.faq summary"],
  "waitFor": "main article",
  "scroll": true
}

A rule needs hosts and at least one of root, remove, map, title, author or date.

Fields#

FieldTypeMeaning
idtext, up to 80Unique id. Made for you when you leave it out.
nametext, up to 100A name to recognise it in the settings.
hosts1 to 20 host namesThe sites it is for. example.com also covers www.example.com and every other subdomain. A leading *. or www. is dropped.
pathsregular expression, up to 200Only pages whose path matches it, written without slashes around it, for example ^/blog/.
rootselector or up to 10 selectorsThe element(s) that hold the article. Several selectors are several areas: all that are on the page are saved, in page order.
fallbackRootup to 8 selectorsBackup areas, tried in order when none of root is on the page (the site changed its markup).
title, author, datea selector or a list of up to 8Where to find them when the page's own tags are not enough. A list is a list of fallbacks: the first that finds something wins.
removeup to 60 selectorsParts to leave out: menus, related posts, share bars.
mapup to 60 { selector, as }For parts built from plain boxes: say what each is. as is one of h1, h2, h3, h4, quote, code, rule, bullet, number, paragraph, file, ignore.
expandup to 20 selectorsParts to click open before reading (toggles whose content is only drawn when opened). They are closed again afterwards.
rowsselectorParts the page throws away again when they scroll out of view (Notion). While scrolling they are remembered and put back for reading. Needs scroll.
detectselectorRecognises pages made with a tool (Notion) whatever their address: the rule then applies there too, unless the site has a rule of its own.
waitFor, waitMsselector, msWait until the selector exists (the page draws its text late), for up to waitMs (default 6000, at most 15000).
scrolltrue or a selectorScroll the page, or the element, once from top to bottom, so lazy content loads.
allowShorttrueAccept articles with less text than the automatic reader would.
keepSmalltrueKeep pictures narrower than 120 px (logos and icons) that the reader leaves out. For pages where every picture is content, such as a README.

Selectors#

Each selector is one of three kinds:

KindLooks likeNotes
CSSmain articleThe default. Checked with the browser's own parser, so a wrong one is refused with the field name.
XPathxpath://div[@id='x']Everything after xpath: is the expression.
Texttext:Related postsMatches a part by the words in it.

A selector is at most 300 characters.

Which rule is used#

  1. Your own rules come first. A rule you made for a host always beats a built-in one.
  2. A rule covers a page when one of its hosts equals the page's host (without www.) or is its parent domain, and paths, if set, matches the path. The most specific host (the longest) wins.
  3. A rule with detect also covers pages on any address where that selector exists, unless a rule names the site.
  4. With no rule, the automatic reader runs: the page's <article>, or the block with the most text.

Built-in rules live in BUILTIN_TEMPLATES in templates.ts. They were checked against the sites' markup as known, not on the live pages.

Storage, export and import#

Rules are kept in the browser's chrome.storage.local, at most 200. They live in the extension, not on your server. Export my rules writes them as JSON, and Import rules reads such a file. Every rule is checked again on import by parseTemplate, which says what is wrong.

Write one#

  1. Open the page, press Choose the article area and click the part. The rule is made for you. Open Site rules to read it.
  2. Add remove for the parts that came along that you do not want.
  3. Add map entries for parts that are headings or quotes drawn as plain boxes.
  4. Press Save rule, save the page again, and look at the result in the library.

For a page that draws late, add waitFor. For lazy pages, add scroll.

Rules for the web app's view are a separate thing#

How a saved article is shown is set by a layout, not a site rule. See The layout language.