Suki-Kira.com Comment Viewer

View comments on suki-kira.com (https://suki-kira.com/) without voting

// ==UserScript==
// @name        Suki-Kira.com Comment Viewer
// @name:ja     好き嫌い.com コメントビューア
// @namespace   https://greasyfork.org/users/1310758
// @description View comments on suki-kira.com (https://suki-kira.com/) without voting
// @description:ja 好き嫌い.com ( https://suki-kira.com/ ) の投票をせずコメントや評価結果を見る
// @match       https://suki-kira.com/*
// @license     MIT License
// @author      pachimonta
// @version     2024-08-16_002
// ==/UserScript==

(function() {
  'use strict';
  // Helper functions
  const $ = (selector, context = document) => context.querySelector(selector);
  const $$ = (selector, context = document) => [...context.querySelectorAll(selector)];
  const sanitizeText = (content) => {
    return content.replace(/[\x22\x3e]/g, match => `&#${match.codePointAt(0).toString().toUpperCase()};`);
  };
  if ($('input[name="id"]')) {
    const skId = $('input[name="id"]').value;
    document.cookie = `sk_${skId}=1;path=/people/result/`;
  }
  const voteForm = $('form[id="vote-form"]');
  if (voteForm) {
    const resultUrl = voteForm.action;

    const sanitizedUrl = sanitizeText(resultUrl);
    const textLink = `
<div style="text-align:center">
コメント表示:
[<a href="${sanitizedUrl}">最新</a> <a href="${sanitizedUrl}/?nxc=21">1-</a>]
[<a href="${sanitizedUrl}/like">好き派のみ</a> <a href="${sanitizedUrl}/like?nxc=21">1-</a>]
[<a href="${sanitizedUrl}/dislike">嫌い派のみ</a> <a href="${sanitizedUrl}/dislike?nxc=21">1-</a>]
</div>
`;
    const target = $('.vote.container') ? $('.vote.container') : voteForm;
    target.insertAdjacentHTML('afterend', textLink);
  }
  const pagination = $('.pagination');
  if (pagination) {
    $$('.pagination').forEach((elem) => {
      if ($('a[href*="?nxc="]', elem)) {
        const nextCommentElement = $('a[href*="?nxc="]', elem);
        if (nextCommentElement.getAttribute('href').endsWith("?nxc=21")) {
          return;
        }
        let lastUrl = nextCommentElement.getAttribute('href').replace(/\?.*/, '?nxc=21');
        const textLink = `
<li class="page-item last-page">
<a href="${sanitizeText(lastUrl)}" class="page-link" data-ci-pagination-page="1" rel="last">最初へ ››</a>
</li>
`;
        nextCommentElement.insertAdjacentHTML('afterend', textLink);
      }
    });
  }
})();