Add Keyboard Shortcut to Toggle AvistaZ Header Visibility

Add keyboard shortcut to toggle header visibility on AvistaZ Network sites

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Add Keyboard Shortcut to Toggle AvistaZ Header Visibility
// @description Add keyboard shortcut to toggle header visibility on AvistaZ Network sites
// @namespace   https://avistaz.to/profile/dryeyes
// @match       *://privatehd.to/*
// @match       *://avistaz.to/*
// @match       *://cinemaz.to/*
// @version     0.1.0
// @grant       GM_getValue
// @grant       GM_setValue
// @locale      English (en)
// ==/UserScript==
(function(){
  'use strict';

  let cleanedUrl = window.location.href.replace(/(#.*)$/, '');
  let bodyNode;
  let header;
  let navbar;
  let bodyPaddingTop;

  async function getShowHeaderValue() {
    let gmPromise = await GM_getValue("ShowHeader", true);
    console.log("GetValue ShowHeader:", gmPromise);
    return gmPromise;
  }
  async function setShowHeaderValue(value) {
    await GM_setValue("ShowHeader", value);
  }

  function optionallyShowHeader(show) {
    console.log("OptionallyShowHeader:", show ? "Show," : "Hide,",
                "currently " + (header.style.display === "" ? "Visible" : "Hidden"));
    if (show) {
      if (header.style.display === "none") {
        header.style.display = "";
        bodyNode.style.paddingTop = bodyPaddingTop;
        navbar.scrollIntoView(true);
        window.scrollBy(0,-(navbar.offsetHeight));
      }
    } else {
      if (header.style.display === "") {
        bodyPaddingTop = bodyNode.style.paddingTop;
        header.style.display = "none";
        bodyNode.style.paddingTop = 0;
        header.scrollIntoView(true);
      }
    }
  }

  function onKeyupHandler (evt) {
    //console.log("evt:", evt);
    if (evt.ctrlKey && !evt.altKey && !evt.shiftKey && !evt.metaKey) {
      //console.log("evt.key:", evt.key);
      if (evt.key === "Home") {
        if (header.style.display === "") {
          optionallyShowHeader (false);
          setShowHeaderValue(false).then(function(){});
        } else {
          optionallyShowHeader (true);
          setShowHeaderValue(true).then(function(){});
        }
      }
    }
  }

  function onVisibilityChange(evt) {
    console.log("VisibilityChangeEvent:", evt);
    if (document.visibilityState === "visible") {
      getShowHeaderValue().then(optionallyShowHeader);
    }
  }

  function optionallyShowHeaderAndScroll(show) {
    optionallyShowHeader(show);
    let torrentsBlock = document.querySelector("section > div.block");

    // On Browse Torrents pages, scroll to the Torrents table block
    if (navbar !== null && torrentsBlock !== null &&
        !torrentsBlock.classList.contains("block-titled")) { // avoid scrolling on Torrent details pages
      // Avoid Chrome auto-scroll to previous scroll position on reloading.
      if ('scrollRestoration' in history) {
        history.scrollRestoration = 'manual';
      }

      torrentsBlock.id = "torrents"
      let newUrl = cleanedUrl + "#torrents";
      //window.location.href = newUrl;
      //history.pushState(null, newUrl); // manually save position in history
      torrentsBlock.scrollIntoView(true);
      let scrollAmount = 10;
      if (show) {
        scrollAmount += navbar.offsetHeight;
      }
      console.log("ScrollAmount:", scrollAmount);
      window.scrollBy(0,-scrollAmount);
    }
  }

  function onLoadHandler() {
    console.log("ToggleAvistaZHeaderVisibility Load event occurred:", cleanedUrl);

    bodyNode = document.querySelector("body");
    header = document.querySelector("header");
    navbar = document.querySelector("div.container");
    getShowHeaderValue().then(optionallyShowHeaderAndScroll);

    document.addEventListener('visibilitychange', onVisibilityChange, false);
  }

  console.log("UserScript running");
  window.addEventListener('load', onLoadHandler, false);
  window.addEventListener('keyup', onKeyupHandler, false);
})();