Add Trailing Slash to Narou URL

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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 スラッシュ付き)は何もしない
})();