arca.live page mover

2023. 5. 14. 오후 4:58:27

当前为 2023-05-16 提交的版本,查看 最新版本

// ==UserScript==
// @name        arca.live page mover
// @namespace   Violentmonkey Scripts
// @match       https://arca.live/*
// @grant       none
// @version     1.0.1.0
// @author      7r
// @license     MIT
// @description 2023. 5. 14. 오후 4:58:27
// ==/UserScript==
// 유저스크립트로 변경된 코드

(function () {
  function getMovedPage(url, direction) {
    if (!url.match(/(.*\/write)/)) {
      const queryParams = {};
      const [paths, queryString] = url.split('?');
      const path = paths.split("/").filter(part => part !== "" && !part.includes(":"));
      // alert(`path0: ${path[0]}\npath1: ${path[1]}\npath2: ${path[2]}\npath3: ${path[3]}\n`)

      if(queryString){
        queryString.split("&").forEach(pair=>{
          const[key,value]=pair.split("=");
          queryParams[key]=decodeURIComponent(value||"")
        });
      }
      let openedPosting = path[3] ? (path.splice(3, 1), true) : false;

      queryParams["p"] = getQueryParams(queryParams["p"], direction, openedPosting);

      const updatedQueryString = Object.keys(queryParams)
        .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`)
        .join("&");
      const updatedUrl = `${path.join("/")}?${updatedQueryString}`;

      location.href = `https://${updatedUrl}`;
    }
  }

  function getQueryParams(queryParamsPage, direction, openedPosting) {
    const numPage = parseInt(queryParamsPage);
    return (queryParamsPage && openedPosting) ? numPage : (direction ? numPage - 1 || 1 : numPage + 1) || (openedPosting || direction ? 1 : 2);
  }

  function isInputElement(event) {
    return ['INPUT', 'TEXTAREA'].includes(event.target.nodeName) || event.target.classList.contains('fr-element');
  }

  document.addEventListener('keydown', function (event) {
  if (
    !isInputElement(event) &&          // Check if the event doesn't originate from an input element
    !event.altKey &&                   // Check if the Alt key is not pressed
    !event.ctrlKey &&                  // Check if the Ctrl key is not pressed
    !event.shiftKey &&                 // Check if the Shift key is not pressed
    event.keyCode !== 17 &&            // Check if the key code is not specifically for the Ctrl key
    event.keyCode !== 18 &&            // Check if the key code is not specifically for the Alt key
    event.keyCode !== 16               // Check if the key code is not specifically for the Shift key
  ) {
      const isLeftArrow = event.keyCode === 37;
      const isRightArrow = event.keyCode === 39;
      if (isLeftArrow || isRightArrow) {
        getMovedPage(document.location.href, isLeftArrow);
        event.preventDefault();
      }
    }
  });
})();