Reload other tabs

Reload selected tabs on key combination

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reload other tabs
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Reload selected tabs on key combination
// @author       Wojciech Duda
// @license      GNU GPLv3
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @require            https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant        GM_log
// @grant        GM_setValue
// @grant        GM_getValue
// @grant         GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_addValueChangeListener
// @grant        GM_saveTab
// @grant        GM_getTab

// ==/UserScript==

(function () {
  //IMPORTANT!
  //Here provide a code of the key that is going to reload all selected pages when pushed.
  //To get a key code go to https://www.toptal.com/developers/keycode
  const secondKey = 83;
  //Here decide if you want to combine the key with control key.
  const control = true;

  ("use strict");
  const rtp = "Reload this tab";
  const drtp = "Don't reload this tab";
  const rap = "Reload all tabs";
  const thisid = Math.random();

  class Tab {
    constructor() {
      if (!GM_getValue("rt")) GM_setValue("rt", []);
      this.#globalData = GM_getValue("rt");
      this.#doReload = window.sessionStorage.getItem("doReload") || false;
    }
    get globalData() {
      return this.#globalData;
    }
    set globalData(val) {
      GM_setValue("rt", val);
      this.#globalData = val;
    }
    get doReload() {
      return this.#doReload;
    }
    set doReload(val) {
      window.sessionStorage.setItem("doReload", true);
      this.#doReload = val;
    }
    #globalData;
    #doReload;
  }
  GM_config.init({
    id: "maincfg",
    fields: {
      key: {
        label: "Code of key to press. Check on https://www.toptal.com/developers/keycode",
        type: "number",
        default: "83",
      },
      useControl: {
        label: "Combine with control?",
        type: "checkbox",
        default: "true",
      },
    },
  });

  const tab = new Tab();

  let rtpid, drtpid;

  function toggleMenu() {
    tab.doReload = !tab.doReload;
    setMenu();
  }

  function setMenu() {
    if (tab.doReload) {
      let newTabs = tab.globalData;
      newTabs = newTabs.splice(newTabs.indexOf(thisid), 1);
      drtpid = GM_registerMenuCommand(drtp, toggleMenu);
      GM_unregisterMenuCommand(rtpid);
      tab.globalData = newTabs;
    } else {
      let newTabs = tab.globalData;
      newTabs.push(thisid);
      rtpid = GM_registerMenuCommand(rtp, toggleMenu);
      GM_unregisterMenuCommand(drtpid);
      tab.globalData = newTabs;
    }
  }

  function reloadAll() {
    GM_setValue("reloadTrigger", Math.random());
  }

  function showConfig(){
    GM_config.open();
    const el = document.createElement('style');
    document.head.appendChild(el);
    document.getElementById('maincfg').style +=`height: 45vh !important;
    position: fixed;
    top: 50%;
    width: 40vw;
    left: 50%;
    opacity: 1;
    display: block;`;
  }

  GM_addValueChangeListener("reloadTrigger", () => {
    if (tab.doReload) location.reload();
  });



  GM_registerMenuCommand(rap, reloadAll);
  GM_registerMenuCommand(
    "Config",
    showConfig
  );
  setMenu(tab.doReload);

  window.addEventListener(
    "keydown",
    /**
     *
     * @param {KeyboardEvent} e
     */
    (e) => {
      if (e.keyCode === GM_config.get('key') && e.ctrlKey === GM_config.get('useControl')) reloadAll();
    }
  );
})();