Site rules
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#
{
"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#
| Field | Type | Meaning |
|---|---|---|
id | text, up to 80 | Unique id. Made for you when you leave it out. |
name | text, up to 100 | A name to recognise it in the settings. |
hosts | 1 to 20 host names | The sites it is for. example.com also covers www.example.com and every other subdomain. A leading *. or www. is dropped. |
paths | regular expression, up to 200 | Only pages whose path matches it, written without slashes around it, for example ^/blog/. |
root | selector or up to 10 selectors | The element(s) that hold the article. Several selectors are several areas: all that are on the page are saved, in page order. |
fallbackRoot | up to 8 selectors | Backup areas, tried in order when none of root is on the page (the site changed its markup). |
title, author, date | a selector or a list of up to 8 | Where 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. |
remove | up to 60 selectors | Parts to leave out: menus, related posts, share bars. |
map | up 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. |
expand | up to 20 selectors | Parts to click open before reading (toggles whose content is only drawn when opened). They are closed again afterwards. |
rows | selector | Parts the page throws away again when they scroll out of view (Notion). While scrolling they are remembered and put back for reading. Needs scroll. |
detect | selector | Recognises 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, waitMs | selector, ms | Wait until the selector exists (the page draws its text late), for up to waitMs (default 6000, at most 15000). |
scroll | true or a selector | Scroll the page, or the element, once from top to bottom, so lazy content loads. |
allowShort | true | Accept articles with less text than the automatic reader would. |
keepSmall | true | Keep 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:
| Kind | Looks like | Notes |
|---|---|---|
| CSS | main article | The default. Checked with the browser's own parser, so a wrong one is refused with the field name. |
| XPath | xpath://div[@id='x'] | Everything after xpath: is the expression. |
| Text | text:Related posts | Matches a part by the words in it. |
A selector is at most 300 characters.
Which rule is used#
- Your own rules come first. A rule you made for a host always beats a built-in one.
- A rule covers a page when one of its
hostsequals the page's host (withoutwww.) or is its parent domain, andpaths, if set, matches the path. The most specific host (the longest) wins. - A rule with
detectalso covers pages on any address where that selector exists, unless a rule names the site. - 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#
- Open the page, press Choose the article area and click the part. The rule is made for you. Open Site rules to read it.
- Add
removefor the parts that came along that you do not want. - Add
mapentries for parts that are headings or quotes drawn as plain boxes. - 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.