Add Trailing Slash to Narou URL

なろうの小説トップページのURLが「/n0001a」「/n0001a/?p=1」のとき「/n0001a/」へリダイレクトする。

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Add Trailing Slash to Narou URL
// @namespace    haaarug
// @version      1.2
// @description  なろうの小説トップページのURLが「/n0001a」「/n0001a/?p=1」のとき「/n0001a/」へリダイレクトする。
// @license      CC0
// @match        https://ncode.syosetu.com/*
// @run-at       document-start
// ==/UserScript==

(function () {
    'use strict';

    const url = new URL(window.location.href);

    // クエリが ?p=1 のとき → クエリ削除してそのままリダイレクト(スラッシュは追加しない)
    if (url.search === '?p=1') {
        url.search = '';
        window.location.replace(url.toString());
        return;
    }

    // クエリがない & pathname がスラッシュで終わっていないとき → スラッシュ追加
    if (!url.search && !url.pathname.endsWith('/')) {
        url.pathname += '/';
        window.location.replace(url.toString());
    }

    // それ以外(クエリがある or スラッシュ付き)は何もしない
})();