Greasy Fork is available in English.

IMDb: Link 'em all!

Adds all kinds of links to IMDb, customizable!

// ==UserScript==
// @name        IMDb: Link 'em all!
// @description Adds all kinds of links to IMDb, customizable!
// @namespace   https://greasyfork.org/en/users/8981-buzz
// @match       *://*.imdb.com/title/tt*/*
// @connect     *
// @require     https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @require     https://unpkg.com/preact@10.5.7/dist/preact.umd.js
// @require     https://unpkg.com/preact@10.5.7/hooks/dist/hooks.umd.js
// @license     GPLv2
// @noframes
// @author      buzz
// @version     2.0.14
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_xmlhttpRequest
// @grant       GM.getValue
// @grant       GM.setValue
// @grant       GM.xmlHttpRequest
// ==/UserScript==
(function (preact, hooks) {
  'use strict';

  var version = "2.0.14";
  var description = "Adds all kinds of links to IMDb, customizable!";
  var homepage = "https://github.com/buzz/imdb-link-em-all#readme";

  const DESCRIPTION = description;
  const HOMEPAGE = homepage;
  const NAME_VERSION = `Link 'em all! v${version}`;
  const SITES_URL = 'https://raw.githubusercontent.com/buzz/imdb-link-em-all/master/sites.json'; // gets replaced by rollup!
  const GM_CONFIG_KEY = 'config';
  const GREASYFORK_URL = 'https://greasyfork.org/scripts/17154-imdb-link-em-all';
  const DEFAULT_CONFIG = {
    enabled_sites: [],
    fetch_results: true,
    first_run: true,
    open_blank: true,
    show_category_captions: true
  };
  const CATEGORIES = {
    search: 'Search',
    movie_site: 'Movie sites',
    pub_tracker: 'Public trackers',
    priv_tracker: 'Private trackers',
    streaming: 'Streaming',
    filehoster: 'Filehosters',
    subtitles: 'Subtitles',
    tv: 'TV'
  };
  const FETCH_STATE = {
    LOADING: 0,
    NO_RESULTS: 1,
    RESULTS_FOUND: 2,
    NO_ACCESS: 3,
    TIMEOUT: 4,
    ERROR: 5
  };

  var img$8 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAABkklEQVQoz1VRTUsCYRBe/AHe/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qfvgBfcJTuKMi+xKfdMCZgIp6eRK8TYu2+w2oA4PwDm+5qVK218XmNLN7xxILqKfS7pGqTWekLmuVtV65STs8hA73RqJQQP5+CP3KKACamHj7FlGBDawfH00kEW0MuA8o9AmA6qMrSHqwTIAoM08hAkHkN0ES3UYfotBGdiNFu5cr2AmgJobOPET7nhxEMuU/o40soSjO7iHbbVNgnUen6pY0/AOCTbC7PuV44H0f8Cetg5g9zP5aU7loDcfwGcrKyzYdvwUUAAAAASUVORK5CYII=";

  var img$4 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAGrSURBVDjLvZPZLkNhFIV75zjvYm7VGFNCqoZUJ+roKUUpjRuqp61Wq0NKDMelGGqOxBSUIBKXWtWGZxAvobr8lWjChRgSF//dv9be+9trCwAI/vIE/26gXmviW5bqnb8yUK028qZjPfoPWEj4Ku5HBspgAz941IXZeze8N1bottSo8BTZviVWrEh546EO03EXpuJOdG63otJbjBKHkEp/Ml6yNYYzpuezWL4s5VMtT8acCMQcb5XL3eJE8VgBlR7BeMGW9Z4yT9y1CeyucuhdTGDxfftaBO7G4L+zg91UocxVmCiy51NpiP3n2treUPujL8xhOjYOzZYsQWANyRYlU4Y9Br6oHd5bDh0bCpSOixJiWx71YY09J5pM/WEbzFcDmHvwwBu2wnikg+lEj4mwBe5bC5h1OUqcwpdC60dxegRmR06TyjCF9G9z+qM2uCJmuMJmaNZaUrCSIi6X+jJIBBYtW5Cge7cd7sgoHDfDaAvKQGAlRZYc6ltJlMxX03UzlaRlBdQrzSCwksLRbOpHUSb7pcsnxCCwngvM2Rm/ugUCi84fycr4l2t8Bb6iqTxSCgNIAAAAAElFTkSuQmCC";

  var img$3 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK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t/Nclx/aAZnSRlhQwYABkJtVU2AGl0KX4lsfoMBYm9BACH5BAkFAAQALAEAAAAPABAAAAZVQIJQOOBwBsMkYoEgbAAATpKogAY4UOnn8BEuoNHiBnmAUq3TbZfInLrf08uFEArB6db6/VKd39l/bxgXAm4XEn54iUMSUUMChF4bVItJHR2BRIVuQQAh+QQFBQABACwAAAAAEAAPAAAGWsCAcCgAgQTDZIBxYQRAAABIKWR4FB5mdPGshIYXrKJpRFYAFeI1qwx5k0UkNUlxjOdVTcAexWuwf1EOflEadQ4UeEt6io0DcwwLTmqPbQpccGAZVZKKIiJzQQA7";

  const iconSrcs = {
    cog: img$8,
    error: img$7,
    info: img$6,
    lock: img$5,
    tick: img$4,
    timeout: img$3,
    world: img$1,
    x: img$2,
    spinner: img
  };
  const Icon = ({
    className,
    title,
    type
  }) => preact.h("img", {
    alt: `${type} icon`,
    className: className,
    src: iconSrcs[type],
    title: title
  });

  function styleInject(css, ref) {
    if ( ref === void 0 ) ref = {};
    var insertAt = ref.insertAt;

    if (!css || typeof document === 'undefined') { return; }

    var head = document.head || document.getElementsByTagName('head')[0];
    var style = document.createElement('style');
    style.type = 'text/css';

    if (insertAt === 'top') {
      if (head.firstChild) {
        head.insertBefore(style, head.firstChild);
      } else {
        head.appendChild(style);
      }
    } else {
      head.appendChild(style);
    }

    if (style.styleSheet) {
      style.styleSheet.cssText = css;
    } else {
      style.appendChild(document.createTextNode(css));
    }
  }

  var css_248z$6 = ".Options_options__5TB2e {\n  margin-top: 10px;\n}\n\n  .Options_options__5TB2e > label > span {\n    margin-left: 10px;\n}\n";
  var css$6 = {"options":"Options_options__5TB2e"};
  styleInject(css_248z$6);

  const Options = ({
    options
  }) => {
    const optionLabels = options.map(([key, title, val, setter]) => preact.h("label", {
      key: key
    }, preact.h("input", {
      checked: val,
      onInput: ev => setter(ev.target.checked),
      type: "checkbox"
    }), preact.h("span", null, title), preact.h("br", null)));
    return preact.h("div", {
      className: css$6.options
    }, optionLabels);
  };

  const SiteIcon = ({
    className,
    site,
    title
  }) => site.icon ? preact.h("img", {
    alt: site.title,
    className: className,
    src: site.icon,
    title: title
  }) : null;

  var css_248z$5 = ".Sites_searchBar__omy0k {\n  display: flex;\n  flex-direction: row;\n  margin-bottom: 1em;\n}\n\n  .Sites_searchBar__omy0k .Sites_searchInput__0o5oY {\n    background-color: rgba(255, 255, 255, 0.9);\n    border-radius: 3px;\n    border-top-color: #949494;\n    border: 1px solid #a6a6a6;\n    box-shadow: 0 1px 0 rgba(0, 0, 0, .07) inset;\n    display: flex;\n    flex-direction: row;\n    height: 24px;\n    line-height: normal;\n    outline: 0;\n    padding: 3px 7px;\n    transition: all 100ms linear;\n    width: 100%;\n}\n\n  .Sites_searchBar__omy0k .Sites_searchInput__0o5oY:focus-within {\n      background-color: #fff;\n      border-color: #e77600;\n      box-shadow: 0 0 2px 2px rgba(228, 121, 17, 0.25);\n}\n\n  .Sites_searchBar__omy0k .Sites_searchInput__0o5oY > * {\n      background-color: transparent;\n      border: none;\n      height: 16px;\n}\n\n  .Sites_searchBar__omy0k .Sites_searchInput__0o5oY > button {\n      margin: 0 0 0 0.7em;\n      padding: 0;\n}\n\n  .Sites_searchBar__omy0k .Sites_searchInput__0o5oY > input {\n      flex-grow: 1;\n      outline: none;\n      padding: 0 0 0 0.5em;\n}\n\n  .Sites_searchBar__omy0k .Sites_resultCount__xMc-y {\n    font-weight: bold;\n    margin-left: 2em;\n    min-width: 140px;\n    text-align: right;\n}\n\n  .Sites_searchBar__omy0k .Sites_resultCount__xMc-y > span {\n      color: black;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 {\n    display: flex;\n    flex-wrap: wrap;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 h4 {\n      width: 100%;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 label {\n      align-items: center;\n      color: #444;\n      display: flex;\n      flex-flow: row;\n      padding: 0 6px;\n      transition: color 100ms;\n      width: 25%;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 label:hover {\n        color: #222;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 label.Sites_checked__nqnSg span {\n        color: black;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 label .Sites_title__4rEy0 {\n        flex-grow: 1;\n        overflow: hidden;\n        text-overflow: ellipsis;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 label input {\n        margin-right: 4px;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 label .Sites_extraIcon__YYfVy {\n        height: 12px;\n        margin-left: 4px;\n        width: 12px;\n}\n\n.Sites_siteList__4rCbT .Sites_catList__Fv8G0 label .Sites_siteIcon__GRVSj {\n        flex-shrink: 0;\n        height: 16px;\n        margin-right: 6px;\n        width: 16px;\n}\n";
  var css$5 = {"searchBar":"Sites_searchBar__omy0k","searchInput":"Sites_searchInput__0o5oY","resultCount":"Sites_resultCount__xMc-y","siteList":"Sites_siteList__4rCbT","catList":"Sites_catList__Fv8G0","checked":"Sites_checked__nqnSg","title":"Sites_title__4rEy0","extraIcon":"Sites_extraIcon__YYfVy","siteIcon":"Sites_siteIcon__GRVSj"};
  styleInject(css_248z$5);

  const SearchInput = ({
    q,
    setQ
  }) => preact.h("div", {
    className: css$5.searchInput
  }, preact.h("span", null, "\uD83D\uDD0D"), preact.h("input", {
    onInput: e => {
      setQ(e.target.value.toLowerCase().trim());
    },
    placeholder: "Search",
    value: q
  }), preact.h("button", {
    style: {
      display: q.length ? 'unset' : 'none'
    },
    title: "Clear",
    type: "button",
    onClick: () => setQ('')
  }, preact.h(Icon, {
    type: "x"
  })));
  const DummyIcon = ({
    size
  }) => {
    const sizePx = `${size}px`;
    const style = {
      display: 'inline-block',
      height: sizePx,
      width: sizePx
    };
    return preact.h("div", {
      className: css$5.siteIcon,
      style: style
    });
  };
  const SiteLabel = ({
    checked,
    setEnabled,
    site
  }) => {
    const input = preact.h("input", {
      checked: checked,
      onInput: e => setEnabled(prev => e.target.checked ? [...prev, site.id] : prev.filter(id => id !== site.id)),
      type: "checkbox"
    });
    const icon = site.icon ? preact.h(SiteIcon, {
      className: css$5.siteIcon,
      site: site,
      title: site.title
    }) : preact.h(DummyIcon, {
      size: 16
    });
    const title = preact.h("span", {
      className: css$5.title,
      title: site.title
    }, site.title);
    const extraIcons = [site.noAccessMatcher ? preact.h(Icon, {
      className: css$5.extraIcon,
      title: "Access restricted",
      type: "lock"
    }) : null, site.noResultsMatcher ? preact.h(Icon, {
      className: css$5.extraIcon,
      title: "Site supports fetching of results",
      type: "tick"
    }) : null];
    return preact.h("label", {
      className: checked ? css$5.checked : null
    }, input, icon, " ", title, " ", extraIcons);
  };
  const CategoryList = ({
    enabled,
    name,
    setEnabled,
    sites
  }) => {
    const siteLabels = sites.map(site => preact.h(SiteLabel, {
      checked: enabled.includes(site.id),
      setEnabled: setEnabled,
      site: site
    }));
    return preact.h("div", {
      className: css$5.catList
    }, preact.h("h4", null, name, " ", preact.h("span", null, "(", sites.length, ")")), siteLabels);
  };
  const Sites = ({
    enabledSites,
    setEnabledSites,
    sites
  }) => {
    const [q, setQ] = hooks.useState('');
    const catSites = Object.keys(CATEGORIES).map(cat => {
      const s = sites.filter(site => site.category === cat);
      if (q.length) {
        return s.filter(site => site.title.toLowerCase().includes(q));
      }
      return s;
    });
    const cats = Object.entries(CATEGORIES).map(([cat, catName], i) => catSites[i].length ? preact.h(CategoryList, {
      enabled: enabledSites,
      key: cat,
      name: catName,
      setEnabled: setEnabledSites,
      sites: catSites[i]
    }) : null);
    const total = catSites.reduce((acc, s) => acc + s.length, 0);
    return preact.h(preact.Fragment, null, preact.h("div", {
      className: css$5.searchBar
    }, preact.h(SearchInput, {
      q: q,
      setQ: setQ
    }), preact.h("div", {
      className: css$5.resultCount
    }, "Showing ", preact.h("span", null, total), " sites.")), preact.h("div", {
      className: css$5.siteList
    }, cats));
  };

  var css_248z$4 = ".About_about__wuWQp {\n  padding: 1em 0;\n  position: relative;\n}\n\n  .About_about__wuWQp ul > li {\n    margin-bottom: 0;\n}\n\n  .About_about__wuWQp h2 {\n    font-size: 20px;\n    margin: 0.5em 0;\n}\n\n  .About_about__wuWQp > *:last-child {\n    margin-bottom: 0;\n}\n\n  .About_about__wuWQp .About_top__jQHYs {\n    text-align: center;\n}\n\n  .About_about__wuWQp .About_content__hReHO {\n    width: 61.8%;\n    margin: 0 auto;\n}\n";
  var css$4 = {"about":"About_about__wuWQp","top":"About_top__jQHYs","content":"About_content__hReHO"};
  styleInject(css_248z$4);

  const About = () => preact.h("div", {
    className: css$4.about
  }, preact.h("div", {
    className: css$4.top
  }, preact.h("h3", null, "\uD83C\uDFA5 ", NAME_VERSION), preact.h("p", null, DESCRIPTION)), preact.h("div", {
    className: css$4.content
  }, preact.h("h2", null, "\uD83D\uDD17 Links"), preact.h("ul", null, preact.h("li", null, preact.h("a", {
    target: "_blank",
    rel: "noreferrer",
    href: HOMEPAGE
  }, "GitHub")), preact.h("li", null, preact.h("a", {
    target: "_blank",
    rel: "noreferrer",
    href: GREASYFORK_URL
  }, "Greasy Fork"))), preact.h("h2", null, "\u2728 Contributions"), preact.h("p", null, "Add new sites or update existing entries."), preact.h("ul", null, preact.h("li", null, preact.h("a", {
    target: "_blank",
    rel: "noreferrer",
    href: "https://github.com/buzz/imdb-link-em-all/issues/new"
  }, "Open a GitHub issue"), ' ', "or"), preact.h("li", null, preact.h("a", {
    target: "_blank",
    rel: "noreferrer",
    href: "https://greasyfork.org/en/scripts/17154-imdb-link-em-all/feedback"
  }, "Give feedback"), ' ', "on Greasy Fork.")), preact.h("p", null, preact.h("em", null, "Thanks to all the contributors!"), " \uD83D\uDC4D"), preact.h("h2", null, "\u2696 License"), preact.h("p", null, "This script is licensed under the terms of the", ' ', preact.h("a", {
    target: "_blank",
    rel: "noreferrer",
    href: "https://github.com/buzz/imdb-link-em-all/blob/master/LICENSE"
  }, "GPL-2.0 License"), ".")));

  var css_248z$3 = ".Config_popover__qMfu9 {\n  background-color: #a5a5a5;\n  border-radius: 4px;\n  box-shadow: 0 0 2em rgba(0, 0, 0, 0.1);\n  color: #333;\n  display: block;\n  font-family: Verdana, Arial, sans-serif;\n  font-size: 11px;\n  left: calc(-800px + 35px);\n  line-height: 1.5rem;\n  padding: 10px;\n  position: absolute;\n  top: calc(20px + 8px);\n  white-space: nowrap;\n  width: 800px;\n  z-index: 100;\n}\n.Config_popover__qMfu9.Config_layout-legacy__M6fyd {\n    left: calc(-800px + 235px);\n}\n.Config_popover__qMfu9.Config_layout-legacy__M6fyd:before {\n      right: calc(235px - 2 * 8px);\n}\n.Config_popover__qMfu9:before {\n    border-bottom: 8px solid #a5a5a5;\n    border-left: 8px solid transparent;\n    border-right: 8px solid transparent;\n    border-top: 8px solid transparent;\n    content: \"\";\n    display: block;\n    height: 8px;\n    right: calc(35px - 2 * 8px);\n    position: absolute;\n    top: calc(-2 * 8px);\n    width: 0;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK {\n    display: flex;\n    flex-direction: column;\n    text-shadow: 0 1px 0 rgba(255, 255, 255, 0.2);\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 {\n      display: flex;\n      flex-direction: row;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 .Config_link__GTbGq {\n        flex-grow: 1;\n        text-align: right;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 .Config_link__GTbGq > a {\n          color: #333;\n          margin-left: 12px;\n          margin-right: 4px;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 .Config_link__GTbGq > a:visited {\n            color: #333;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 > button {\n        background-color: rgba(0, 0, 0, 0.05);\n        border-bottom-left-radius: 0;\n        border-bottom-right-radius: 0;\n        border-bottom: transparent;\n        border-left: 1px solid rgba(0, 0, 0, 0.25);\n        border-right: 1px solid rgba(0, 0, 0, 0.25);\n        border-top-left-radius: 2px;\n        border-top-right-radius: 2px;\n        border-top: 1px solid rgba(0, 0, 0, 0.25);\n        color: #424242;\n        font-size: 12px;\n        margin: 0 6px 0 0;\n        outline: none;\n        padding: 0 6px;\n        transform: translateY(1px);\n        text-shadow: 0 1px 0 rgba(255, 255, 255, 0.2);\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 > button:hover {\n          background-color: rgba(0, 0, 0, 0.1);\n          color: #222;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 > button.Config_active__vD-Fl {\n          background-color: #c2c2c2;\n          color: #222;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 > button:last-child {\n          margin-right: 0;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_top__6DKJ8 > button > img {\n          vertical-align: text-bottom;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_body__wtDKH {\n      background-color: #c2c2c2;\n      border-bottom-left-radius: 2px;\n      border-bottom-right-radius: 2px;\n      border-top-right-radius: 2px;\n      border: 1px solid rgba(0, 0, 0, 0.25);\n      padding: 12px 10px 12px;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_body__wtDKH > div {\n        overflow: hidden;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_body__wtDKH > div > *:first-child {\n          margin-top: 0;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_body__wtDKH > div > *:last-child {\n          margin-bottom: 0;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_controls__-N2ev {\n      display: flex;\n      flex-direction: row;\n      margin-top: 10px;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_controls__-N2ev > div:first-child {\n        flex-grow: 1;\n}\n.Config_popover__qMfu9 .Config_inner__oVRAK .Config_controls__-N2ev button {\n        padding-bottom: 0;\n        padding-top: 0;\n        margin-right: 12px;\n}\n";
  var css$3 = {"popover":"Config_popover__qMfu9","layout-legacy":"Config_layout-legacy__M6fyd","inner":"Config_inner__oVRAK","top":"Config_top__6DKJ8","link":"Config_link__GTbGq","active":"Config_active__vD-Fl","body":"Config_body__wtDKH","controls":"Config_controls__-N2ev"};
  styleInject(css_248z$3);

  const OPTIONS = [['show_category_captions', 'Show category captions'], ['open_blank', 'Open links in new tab'], ['fetch_results', 'Automatically fetch results']];
  const Config = ({
    config,
    layout,
    setConfig,
    setShow,
    show,
    sites
  }) => {
    const [enabledSites, setEnabledSites] = hooks.useState(config.enabled_sites);
    const showCategoryCaptionsArr = hooks.useState(config.show_category_captions);
    const openBlankArr = hooks.useState(config.open_blank);
    const fetchResultsArr = hooks.useState(config.fetch_results);
    const [showCategoryCaptions, setShowCategoryCaptions] = showCategoryCaptionsArr;
    const [openBlank, setOpenBlank] = openBlankArr;
    const [fetchResults, setFetchResults] = fetchResultsArr;
    const optStates = [showCategoryCaptionsArr, openBlankArr, fetchResultsArr];
    const options = OPTIONS.map((opt, i) => [...opt, ...optStates[i]]);
    const [tab, setTab] = hooks.useState(0);
    const tabs = [{
      title: 'Sites',
      icon: 'world',
      comp: preact.h(Sites, {
        enabledSites: enabledSites,
        setEnabledSites: setEnabledSites,
        sites: sites
      })
    }, {
      title: 'Options',
      icon: 'cog',
      comp: preact.h(Options, {
        options: options
      })
    }, {
      title: 'About',
      icon: 'info',
      comp: preact.h(About, null)
    }];
    const onClickCancel = () => {
      setShow(false);
      // Restore state
      setEnabledSites(config.enabled_sites);
      setFetchResults(config.fetch_results);
      setOpenBlank(config.open_blank);
      setShowCategoryCaptions(config.show_category_captions);
    };
    const onClickSave = () => {
      setConfig({
        enabled_sites: enabledSites,
        fetch_results: fetchResults,
        open_blank: openBlank,
        show_category_captions: showCategoryCaptions
      });
      setShow(false);
    };
    return preact.h("div", {
      className: `${css$3.popover} ${css$3['layout-' + layout]}`,
      style: {
        display: show ? 'block' : 'none'
      }
    }, preact.h("div", {
      className: css$3.inner
    }, preact.h("div", {
      className: css$3.top
    }, tabs.map(({
      title,
      icon
    }, i) => preact.h("button", {
      className: tab === i ? css$3.active : null,
      type: "button",
      onClick: () => setTab(i)
    }, preact.h(Icon, {
      title: title,
      type: icon
    }), " ", title)), preact.h("div", {
      className: css$3.link
    }, preact.h("a", {
      target: "_blank",
      rel: "noreferrer",
      href: HOMEPAGE
    }, "\uD83C\uDFA5 ", NAME_VERSION))), preact.h("div", {
      className: css$3.body
    }, tabs.map(({
      comp
    }, i) => preact.h("div", {
      style: {
        display: tab === i ? 'block' : 'none'
      }
    }, comp))), preact.h("div", {
      className: css$3.controls
    }, preact.h("div", null, preact.h("button", {
      className: "btn primary small",
      onClick: onClickSave,
      type: "button"
    }, "OK"), preact.h("button", {
      className: "btn small",
      onClick: onClickCancel,
      type: "button"
    }, "Cancel")))));
  };

  function _extends() {
    _extends = Object.assign ? Object.assign.bind() : function (target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i];
        for (var key in source) {
          if (Object.prototype.hasOwnProperty.call(source, key)) {
            target[key] = source[key];
          }
        }
      }
      return target;
    };
    return _extends.apply(this, arguments);
  }

  const replaceFields = (str, {
    id,
    title,
    year
  }, encode = true) => str.replace(new RegExp('{{IMDB_TITLE}}', 'g'), encode ? encodeURIComponent(title) : title).replace(new RegExp('{{IMDB_ID}}', 'g'), id).replace(new RegExp('{{IMDB_YEAR}}', 'g'), year);

  const checkResponse = (resp, site) => {
    // Likely a redirect to login page
    if (resp.responseHeaders && resp.responseHeaders.includes('Refresh: 0; url=')) {
      return FETCH_STATE.NO_ACCESS;
    }

    // There should be a responseText
    if (!resp.responseText) {
      return FETCH_STATE.ERROR;
    }

    // Detect Blogger content warning
    if (resp.responseText.includes('The blog that you are about to view may contain content only suitable for adults.')) {
      return FETCH_STATE.NO_ACCESS;
    }

    // Detect CloudFlare anti DDOS page
    if (resp.responseText.includes('Checking your browser before accessing')) {
      return FETCH_STATE.NO_ACCESS;
    }

    // Check site access
    if (site.noAccessMatcher) {
      const matchStrings = Array.isArray(site.noAccessMatcher) ? site.noAccessMatcher : [site.noAccessMatcher];
      if (matchStrings.some(matchString => resp.responseText.includes(matchString))) {
        return FETCH_STATE.NO_ACCESS;
      }
    }
    // Check results
    if (Array.isArray(site.noResultsMatcher)) {
      // Advanced ways of checking, currently only EL_COUNT is supported
      const [checkType, selector, compType, number] = site.noResultsMatcher;
      const m = resp.responseHeaders.match(/content-type:\s([^\s;]+)/);
      const contentType = m ? m[1] : 'text/html';
      let doc;
      try {
        const parser = new DOMParser();
        doc = parser.parseFromString(resp.responseText, contentType);
      } catch (e) {
        console.error('Could not parse document!');
        return FETCH_STATE.ERROR;
      }
      switch (checkType) {
        case 'EL_COUNT':
          {
            let result;
            try {
              result = doc.querySelectorAll(selector);
            } catch (err) {
              console.error(err);
              return FETCH_STATE.ERROR;
            }
            if (compType === 'GT') {
              if (result.length > number) {
                return FETCH_STATE.RESULTS_FOUND;
              }
            }
            if (compType === 'LT') {
              if (result.length < number) {
                return FETCH_STATE.RESULTS_FOUND;
              }
            }
            break;
          }
      }
      return FETCH_STATE.NO_RESULTS;
    }
    const matchStrings = Array.isArray(site.noResultsMatcher) ? site.noResultsMatcher : [site.noResultsMatcher];
    if (matchStrings.some(matchString => resp.responseText.includes(matchString))) {
      return FETCH_STATE.NO_RESULTS;
    }
    return FETCH_STATE.RESULTS_FOUND;
  };
  const useResultFetcher = (imdbInfo, site) => {
    const [fetchState, setFetchState] = hooks.useState(null);
    hooks.useEffect(() => {
      let xhr;
      if (site.noResultsMatcher) {
        // Site supports result fetching
        const {
          url
        } = site;
        const isPost = Array.isArray(url);
        const opts = {
          timeout: 20000,
          onload: resp => setFetchState(checkResponse(resp, site)),
          onerror: resp => {
            console.error(`Failed to fetch results from URL '${url}': ${resp.statusText}`);
            setFetchState(FETCH_STATE.ERROR);
          },
          ontimeout: () => setFetchState(FETCH_STATE.TIMEOUT)
        };
        if (isPost) {
          const [postUrl, fields] = url;
          opts.method = 'POST';
          opts.url = postUrl;
          opts.headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
          };
          opts.data = Object.keys(fields).map(key => {
            const val = replaceFields(fields[key], imdbInfo, false);
            return `${key}=${val}`;
          }).join('&');
        } else {
          opts.method = 'GET';
          opts.url = replaceFields(url, imdbInfo);
        }
        xhr = GM.xmlHttpRequest(opts);
        setFetchState(FETCH_STATE.LOADING);
      }
      return () => {
        if (xhr && xhr.abort) {
          xhr.abort();
        }
      };
    }, [imdbInfo, site]);
    return fetchState;
  };

  var css_248z$2 = ".SiteLink_linkWrapper__wGnJ- {\n  display: inline-block;\n  margin-right: 4px;\n}\n\n  .SiteLink_linkWrapper__wGnJ- img {\n    vertical-align: baseline;\n}\n\n  .SiteLink_linkWrapper__wGnJ- a {\n    white-space: pre-line;\n}\n\n  .SiteLink_linkWrapper__wGnJ- a > img {\n      height: 16px;\n      width: 16px;\n      margin-right: 4px;\n}\n\n  .SiteLink_linkWrapper__wGnJ- .SiteLink_resultsIcon__mjHYM {\n    margin-left: 4px;\n}\n";
  var css$2 = {"linkWrapper":"SiteLink_linkWrapper__wGnJ-","resultsIcon":"SiteLink_resultsIcon__mjHYM"};
  styleInject(css_248z$2);

  const ResultsIndicator = ({
    imdbInfo,
    site
  }) => {
    const fetchState = useResultFetcher(imdbInfo, site);
    let iconType;
    let title;
    switch (fetchState) {
      case FETCH_STATE.LOADING:
        iconType = 'spinner';
        title = 'Loading…';
        break;
      case FETCH_STATE.NO_RESULTS:
        iconType = 'x';
        title = 'No Results found!';
        break;
      case FETCH_STATE.RESULTS_FOUND:
        iconType = 'tick';
        title = 'Results found!';
        break;
      case FETCH_STATE.NO_ACCESS:
        iconType = 'lock';
        title = 'You have to login to this site!';
        break;
      case FETCH_STATE.TIMEOUT:
        iconType = 'timeout';
        title = 'You have to login to this site!';
        break;
      case FETCH_STATE.ERROR:
        iconType = 'error';
        title = 'Error fetching results! (See dev console for details)';
        break;
      default:
        return null;
    }
    return preact.h(Icon, {
      className: css$2.resultsIcon,
      title: title,
      type: iconType
    });
  };

  // As it is not possible to open links with POST request we need a trick
  const usePostLink = (url, openBlank, imdbInfo) => {
    const formEl = hooks.useRef();
    const isPost = Array.isArray(url);
    const href = isPost ? url[0] : replaceFields(url, imdbInfo, false);
    const onClick = event => {
      if (isPost && formEl.current) {
        event.preventDefault();
        formEl.current.submit();
      }
    };
    hooks.useEffect(() => {
      if (isPost) {
        const [postUrl, fields] = url;
        const form = document.createElement('form');
        form.action = postUrl;
        form.method = 'POST';
        form.style.display = 'none';
        form.target = openBlank ? '_blank' : '_self';
        Object.keys(fields).forEach(key => {
          const input = document.createElement('input');
          input.type = 'text';
          input.name = key;
          input.value = replaceFields(fields[key], imdbInfo, false);
          form.appendChild(input);
        });
        document.body.appendChild(form);
        formEl.current = form;
      }
      return () => {
        if (formEl.current) {
          formEl.current.remove();
        }
      };
    });
    return [href, onClick];
  };

  const Sep = () => preact.h(preact.Fragment, null, "\xA0", preact.h("span", {
    className: "ghost"
  }, "|"));
  const SiteLink = ({
    config,
    imdbInfo,
    last,
    site
  }) => {
    const extraAttrs = config.open_blank ? {
      target: '_blank',
      rel: 'noreferrer'
    } : {};
    const [href, onClick] = usePostLink(site.url, config.open_blank, imdbInfo);
    return preact.h("span", {
      className: css$2.linkWrapper
    }, preact.h("a", _extends({
      className: "ipc-link ipc-link--base",
      href: href,
      onClick: onClick
    }, extraAttrs), preact.h(SiteIcon, {
      site: site
    }), preact.h("span", null, site.title)), config.fetch_results ? preact.h(ResultsIndicator, {
      imdbInfo: imdbInfo,
      site: site
    }) : null, last ? null : preact.h(Sep, null));
  };

  var css_248z$1 = ".LinkList_linkList__beWAL {\n  line-height: 1.6rem\n}\n\n.LinkList_h4__OVHW- {\n  margin-top: 0.5rem\n}\n";
  var css$1 = {"linkList":"LinkList_linkList__beWAL","h4":"LinkList_h4__OVHW-"};
  styleInject(css_248z$1);

  const LinkList = ({
    config,
    imdbInfo,
    sites
  }) => Object.entries(CATEGORIES).map(([category, categoryName]) => {
    const catSites = sites.filter(site => site.category === category && config.enabled_sites.includes(site.id));
    if (!catSites.length) {
      return null;
    }
    const caption = config.show_category_captions ? preact.h("h4", {
      className: css$1.h4
    }, categoryName) : null;
    return preact.h(preact.Fragment, null, caption, preact.h("div", {
      className: css$1.linkList
    }, catSites.map((site, i) => preact.h(SiteLink, {
      config: config,
      imdbInfo: imdbInfo,
      last: i === catSites.length - 1,
      site: site
    }))));
  });

  var css_248z = ".App_configWrapper__bVP2M {\n  position: absolute;\n  right: 20px;\n  top: 20px;\n}\n\n  .App_configWrapper__bVP2M > button {\n    background: transparent;\n    border: none;\n    cursor: pointer;\n    outline: none;\n    padding: 0;\n}\n\n  .App_configWrapper__bVP2M > button > img {\n      vertical-align: baseline;\n}\n";
  var css = {"configWrapper":"App_configWrapper__bVP2M"};
  styleInject(css_248z);

  // Note: GM.* only work in async functions
  const restoreConfig = async () => JSON.parse(await GM.getValue(GM_CONFIG_KEY));
  const saveConfig = async config => GM.setValue(GM_CONFIG_KEY, JSON.stringify(config));
  const useConfig = () => {
    const [config, setConfig] = hooks.useState();
    hooks.useEffect(() => {
      restoreConfig().then(c => setConfig(c)).catch(() => setConfig(DEFAULT_CONFIG));
    }, []);
    hooks.useEffect(() => {
      if (config) {
        saveConfig(config);
      }
    }, [config]);
    return {
      config,
      setConfig
    };
  };

  const loadSites = () => new Promise((resolve, reject) => GM.xmlHttpRequest({
    method: 'GET',
    url: SITES_URL,
    nocache: true,
    onload({
      response,
      status,
      statusText
    }) {
      if (status === 200) {
        try {
          resolve(JSON.parse(response).sort((a, b) => a.title.localeCompare(b.title)));
        } catch (e) {
          reject(e);
        }
      } else {
        reject(new Error(`LTA: Could not load sites (URL=${SITES_URL}): ${status} ${statusText}`));
      }
    },
    onerror({
      status,
      statusText
    }) {
      reject(new Error(`LTA: Could not load sites (URL=${SITES_URL}): ${status} ${statusText}`));
    }
  }));
  const useSites = () => {
    const [sites, setSites] = hooks.useState([]);
    hooks.useEffect(() => {
      loadSites().then(s => setSites(s)).catch(err => setSites(err.message));
    }, []);
    return sites;
  };

  const App = ({
    imdbInfo
  }) => {
    const {
      config,
      setConfig
    } = useConfig();
    const sites = useSites();
    const [showConfig, setShowConfig] = hooks.useState(false);
    hooks.useEffect(() => {
      if (config && config.first_run) {
        setShowConfig(true);
        setConfig(prev => ({
          ...prev,
          first_run: false
        }));
      }
    }, [config]);
    if (typeof sites === 'string') {
      return sites; // Display error message
    }

    if (!config || !sites.length) {
      return null;
    }
    return preact.h(preact.Fragment, null, imdbInfo.layout === 'legacy' ? preact.h("hr", null) : null, preact.h("div", {
      className: css.configWrapper
    }, preact.h("button", {
      onClick: () => setShowConfig(cur => !cur),
      title: "Configure",
      type: "button"
    }, preact.h(Icon, {
      type: "cog"
    })), preact.h(Config, {
      config: config,
      layout: imdbInfo.layout,
      setConfig: setConfig,
      setShow: setShowConfig,
      sites: sites,
      show: showConfig
    })), preact.h(LinkList, {
      config: config,
      imdbInfo: imdbInfo,
      sites: sites
    }));
  };

  const divId = '__LTA__';
  const detectLayout = mUrl => {
    // Currently there seem to be 3 different IMDb layouts:
    // 1) "legacy": URL ends with '/reference'
    if (['reference', 'combined'].includes(mUrl[2])) {
      return ['legacy', 'h3[itemprop=name]', '.titlereference-section-overview > *:last-child'];
    }
    // 2) "redesign2020": Redesign 2020
    //    https://www.imdb.com/preferences/beta-control?e=tmd&t=in&u=/title/tt0163978/
    if (document.querySelector('main section > .ipc-page-content-container')) {
      return ['redesign2020', 'title', 'main > * > section > div'];
    }
    // 3) "new": The old default (has been around for many years)
    return ['new', 'h1', '.title-overview'];
  };
  const parseImdbInfo = () => {
    // TODO: extract type (TV show, movie, ...)

    // Parse IMDb number and layout
    const mUrl = /^\/title\/tt([0-9]{7,8})\/([a-z]*)/.exec(window.location.pathname);
    if (!mUrl) {
      throw new Error('LTA: Could not parse IMDb URL!');
    }
    const [layout, titleSelector, containerSelector] = detectLayout(mUrl);
    const info = {
      id: mUrl[1],
      layout
    };
    info.title = document.querySelector(titleSelector).innerText.trim();
    const mTitle = /^(.+)\s+\((\d+)\)/.exec(info.title);
    if (mTitle) {
      info.title = mTitle[1].trim();
      info.year = parseInt(mTitle[2].trim(), 10);
    }
    return [info, containerSelector];
  };
  const [imdbInfo, containerSelector] = parseImdbInfo();
  const injectAndStart = () => {
    let injectionEl = document.querySelector(containerSelector);
    if (!injectionEl) {
      throw new Error('LTA: Could not find target container!');
    }
    const container = document.createElement('div');
    container.id = divId;
    container.style.position = 'relative';
    if (imdbInfo.layout === 'redesign2020') {
      container.className = 'ipc-page-content-container ipc-page-content-container--center';
      container.style.backgroundColor = 'white';
      container.style.padding = '0 var(--ipt-pageMargin)';
      container.style.minHeight = '50px';
      injectionEl.prepend(container);
    } else {
      container.classList.add('article');
      injectionEl.appendChild(container);
    }
    preact.render(preact.h(App, {
      imdbInfo: imdbInfo
    }), container);
  };
  const containerWatchdog = () => {
    const container = document.querySelector(`#${divId}`);
    if (container === null) {
      injectAndStart();
    }
    window.setTimeout(containerWatchdog, 1000);
  };
  window.setTimeout(containerWatchdog, 500);

})(preact, preactHooks);