Skip to content

Custom host rules (cheat sheet)

darkred edited this page Oct 11, 2021 · 1 revision

Basics

Property (meaning) Syntax (and examples)
"r" regular expression
"q" query {"r":"imagebam\\.com/image/", "q":"img[id]"}
"c" text caption {"r":"pinterest\\.com/pin/", "q":".pinImage", "c":"meta[property='og:description']"}

Special Cases

Property (meaning) Syntax (and examples)
"s" substitution {"r":"hotimg\\.com/image/([a-z0-9]+)", "s":"http://www.hotimg.com/direct/$1"}
vim/sed syntax {"r":"hotimg\\.com/image", "s":"/image/direct/"}
list of subst. patterns {"r":"example\\.com/pics", "s":["/small/large/", "/small/medium/"]}
"html":true search inside HTML source (instead of href and src attributes of links and images.)

By default, the regular expression is applied on href and src attributes of links and images. MPIV looks at thumbnail locations first and surrounding links second.

Insert "html":true to search inside their HTML source instead. This way your regular expression is able to access URL segments in e.g. data attributes and inline styles.
However, processing large chunks of HTML is bad for performance. You should therefore avoid using "html":true without additional constrains (explained further below).

If "html":true is present, you must provide a substitution pattern.

"xhr":true images will be downloaded in an alternative way which should circumvent obnoxious protections.

Some sites (usually the ones with money-making schemes and obnoxious ads) force users to view their images directly on their site by prohibiting hotlinking. Insert the property "xhr":true into the host rule and images will be downloaded in an alternative way which should circumvent such protections.

{"r":"stooorage\\.com/show/", "q":"#page_body div div img", "xhr":true}

"e" process elements other than <a> and <img>, e.g. <li>

f you want MPIV to process elements other than <a> and <img> because a site uses let's say <li> elements with CSS backgrounds as thumbnails, you need to specify a selector for them in property "e". Keep in mind that even now regular expressions will only be applied on href and src attributes. That means if the matching element lacks href and src attributes, you need to set "html":true or write your own parsing code.

Property "e" can also be used as an elegant alternative to "r".

{"e":"a.avatar", "s":"/small/large/"}

"d" site's domain

In order to avoid needless evaluation of rules that make only sense on a single website, write the site's domain (or a part of it) in property "d".

{"d":"example-shop", "r":"data-product-pic=\"(http+?)\"", "s":"$1", "html":true}

Advanced Use

Advanced Use

Rule properties "s", "q" and "c" may contain JavaScript code which provides a maximum of flexibility.

While "s" and "q" can either return a single URL or an array of URLs, "c" is expected to return a descriptive text string.

{"r":"example\\.com/foo", "s":"if(node.className == 'ad') return ''; else return m.input.toLowerCase();"}

{"e":"a.pic", "s":"return ['png', 'gif', 'jpg'].map(function(ext) { return node.dataset.base + ext; });"}

If "s" returns false, MPIV will proceed to the next rule. If "s" returns '' (empty string), no further rule will be evaluated.

"s" substitution

"s" available variables:

m RegExp result array

node HTML element that triggered popup

"q" query

"q" and "c" available variables:

doc DOM tree of remote page (when "q" exists and HTTP response could be parsed)

text raw HTTP response / source code of HTML page

node HTML element that triggered popup (in local DOM tree)

"c" text caption
"follow":true

Property "follow":true causes MPIV to completely restart rule evaluation with the URL returned by "q" or "s" as input. This comes in handy when you deal with sites that don't host images themselves. If neither "q" nor "s" exists, MPIV will follow all HTTP redirections (Location header) and restart evaluation with the last URL requested. Rules with "html":true will be skipped during re-evaluation if the new input URL was taken from a different page.

{"r":"example\\.com/view", "q":"a[href*="imagebam"]", "follow":true}

{"r":"example\\.com/redirect\.php\?decrypt=", "follow":true}

"rect" A few sites clip their thumbnails by putting them in containers smaller than the thumbnails themselves. As a result, MPIV's popup stays open longer than you would expect because your mouse cursor hasn't left the invisible part of the thumbnail yet. If you encounter a situation like this, write a CSS selector in property "rect" to guide MPIV to an ancestor node whose bounding rectangle makes more sense to you.
"manual":true Insert property "manual":true to disable automatic popup activation on a case-by-case basis.
"css"

Use property "css" to apply CSS styling to the page in case the rule gets chosen.

{"e":"a.user-profile", "q":"#user-pic", "css":"#user-hovercard { display:none; }"}

"g" gallery

(from here)
Originally it was only for the remote URLs, but in this fork it can be used on current page as well by specifying "s":"gallery".

For function mode the parameters are:

  • text - remote response text, undefined in "s":"gallery" mode

  • doc - remote response as document, undefined in "s":"gallery" mode

  • url - remote response URL, undefined in "s":"gallery" mode

  • m - the match object/array

  • rule - the rule object so you can use its properties

  • node - the original hovered/matched node

  • cb - the callback function to provide results if your function generates the gallery asynchronously, simply call it like this: cb(items)


For declarative syntax:

  • fix function parameters:

    • s - string: caption, title, url

    • isURL - whether s is for the url property

  • both entry and image are in the same document, which is remote by default.

  • entry is the gallery item container so you can use short relative selectors for image and caption inside that container e.g. "entry":".item", "image":"img", "caption":"span.description"

"e" parent-child

(from here)
"e": {".parent-selector": ".image-selector"} syntax makes MPIV find a .parent-selector and then find its child element .image-selector, which will be processed further in a safe declarative "s" rule without js code.

"e": {".parent1": ".image1", ".parent2": ".image2"} is an example of using multiple parent-child relations.

"e": ".selector" syntax is still supported but it's problematic on sites with a hover overlay, which prevents the image from being reported to MPIV, because rules have to use js code (potentially malicious) in "s" to pick the correct child element.

MPIV has basic support for HTML5 video. Host rules may point to source elements and return URLs ending in webm/mp4.
Clone this wiki locally