0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

複数ページ構成のweb記事の次のページの url を作る (Javascript版)

0
Last updated at Posted at 2026-02-07

こんにちは。
複数ページ構成のweb記事の次のページの url を作る」を Javascript コードへ移植しました。

ChatGPT に作ってもらいました。Bookmarklet として使えるようにして活用するよう勧められました。

function nextPageUrl(currentUrl) {
  const url = new URL(currentUrl);
  const host = url.hostname;
  const path = url.pathname;

  // --- gendai.media ---
  if (host.endsWith("gendai.media")) {
    if (!url.searchParams.has("page")) {
      url.searchParams.set("page", "2");
      return url.toString();
    }
  }

  // --- natgeo.nikkeibp.co.jp ---
  if (host === "natgeo.nikkeibp.co.jp") {
    if (!url.searchParams.has("P")) {
      url.searchParams.set("P", "2");
      return url.toString();
    }
  }

  // --- ascii.jp ---
  if (host === "ascii.jp") {
    if (!path.match(/\/\d+\/$/)) {
      url.pathname = path.replace(/\/$/, "") + "/2/";
      return url.toString();
    }
  }

  // --- newsweekjapan.jp ---
  if (host.endsWith("newsweekjapan.jp")) {
    const m = path.match(/^(.*?)(\.php)$/);
    if (m && !m[1].endsWith("_2")) {
      url.pathname = m[1] + "_2" + m[2];
      return url.toString();
    }
  }

  // --- fallback: generic ?page=2 ---
  if (!url.search) {
    url.searchParams.set("page", "2");
    return url.toString();
  }

  return null;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?