arca.live page mover

2024. 5. 30. 오후 4:58:27

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name arca.live page mover
// @match https://arca.live/*
// @grant GM_setValue
// @grant GM_getValue
// @version 2.1.1
// @author 7r
// @license MIT
// @description 2024. 5. 30. 오후 4:58:27
// @namespace Violentmonkey Scripts
// ==/UserScript==
 
(() => {
    const CACHE_KEY = 'arca_live_page_cache';
    const cache = GM_getValue(CACHE_KEY, {});
    
    const isInputElement = ({target}) => 
        ['INPUT', 'TEXTAREA'].includes(target.nodeName) || 
        target.classList.contains('fr-element');
    
    const removeRepeatedSubstring = (str, substr) => 
        str.replace(new RegExp(`/${substr}`, 'g'), '');
 
    const getMovedPage = (url, isLeftArrow) => {
        if (url.includes("/write")) return;
 
        const urlObj = new URL(url);
        const pathParts = urlObj.pathname.split('/').filter(Boolean);
        const isPostDetail = pathParts.length > 2;
 
        const currentPage = parseInt(urlObj.searchParams.get('p')) || 1;
        
        if (!currentPage && !isPostDetail && !isLeftArrow) return;
 
        const newPage = isLeftArrow 
            ? Math.max(currentPage - 1, 1) 
            : (isPostDetail ? currentPage : currentPage + 1);
 
        const params = new URLSearchParams();
        params.set('p', newPage);
        urlObj.hash = '';
        urlObj.search = params.toString();
 
        const newUrl = isPostDetail 
            ? removeRepeatedSubstring(urlObj.toString(), pathParts.at(-1)) 
            : urlObj.toString();
            
        cache[newUrl] = newPage;
        location.href = newUrl;
    };
 
    window.addEventListener('keydown', e => {
        if (isInputElement(e) || 
            e.altKey || 
            e.ctrlKey || 
            e.shiftKey || 
            [16, 17, 18].includes(e.keyCode)) return;
 
        const isLeftArrow = e.keyCode === 37;
        const isRightArrow = e.keyCode === 39;
 
        if (isLeftArrow || isRightArrow) {
            getMovedPage(document.location.href, isLeftArrow);
            e.preventDefault();
        }
    });
 
    GM_setValue(CACHE_KEY, cache);
})();