diff --git a/src/rules/onePage/original/kakuyomu.ts b/src/rules/onePage/original/kakuyomu.ts index 966bb59f..ce5a8b5b 100644 --- a/src/rules/onePage/original/kakuyomu.ts +++ b/src/rules/onePage/original/kakuyomu.ts @@ -1,17 +1,70 @@ import { mkRuleClass } from "../template"; +// Define a type for elements that can be clicked +type ClickableElement = HTMLElement & { click: () => void }; + +// Function to handle clicking based on aria-describedby and span id +function clickButtonFromSpan(spanElements: NodeListOf, targetText: string): void { + spanElements.forEach(span => { + if (span.textContent?.includes(targetText)) { + const describedById = span.id; + if (describedById) { + const button = document.querySelector(`button[aria-describedby="${describedById}"]`) as ClickableElement | null; + button?.click(); + } + } + }); +} + +// Function to handle clicking based on target text within a div inside a button +function clickButtonFromDiv(divElements: NodeListOf, targetText: string): void { + divElements.forEach(div => { + if (div.textContent?.includes(targetText)) { + let parent: HTMLElement | null = div; + while (parent && parent.nodeName !== 'BUTTON') { + parent = parent.parentElement; + } + (parent as ClickableElement | null)?.click(); + } + }); +} + +// Function to handle clicking a button with an SVG and H3, but only if the SVG lacks a specific class +function clickButtonWithSVGAndH3(buttons: NodeListOf): void { + buttons.forEach(button => { + const svg = button.querySelector('svg[class^="Icons_icon"]') as SVGSVGElement | null; + const h3 = button.querySelector('h3'); + if (svg && h3) { + const hasFlipClass = Array.from(svg.classList).some(className => className.startsWith('Icons_flip')); + if (!hasFlipClass) { + button.click(); + } + } + }); +} + +// Gather all relevant elements +const spanElements = document.querySelectorAll('span'); +const divElements = document.querySelectorAll('div'); +const buttons = document.querySelectorAll('button[class^="Button_button"]'); + +// Execute each function with the appropriate elements and target text +clickButtonFromSpan(spanElements, "…続きを読む"); +clickButtonFromDiv(divElements, "つづきを表示"); +clickButtonWithSVGAndH3(buttons); + export const kakuyomu = () => mkRuleClass({ bookUrl: document.location.href, bookname: ( - document.querySelector("#workTitle > a") as HTMLElement + document.querySelector("h1") as HTMLElement ).innerText.trim(), author: ( document.querySelector( - "#workAuthor-activityName > a" + "div[class*=partialGiftWidgetActivityName] > a" ) as HTMLAnchorElement ).innerText.trim(), - introDom: document.querySelector("#introduction") as HTMLElement, + introDom: document.querySelector("div[class*=CollapseTextWithKakuyomuLinks]") as HTMLElement, introDomPatch: (dom) => dom, coverUrl: null, additionalMetadatePatch: (additionalMetadate) => { @@ -20,14 +73,15 @@ export const kakuyomu = () => ).map((a) => (a as HTMLAnchorElement).innerText); return additionalMetadate; }, - aList: document.querySelectorAll("li.widget-toc-episode > a"), - getAName: (dom) => - ( - dom.querySelector("span.widget-toc-episode-titleLabel") as HTMLElement - ).innerText.trim(), - sections: document.querySelectorAll("li.widget-toc-chapter > span"), + aList: document.querySelectorAll("a[class*=WorkTocSection_link]"), + getAName: (aElem) => + ( + aElem.querySelector('div[class*="Typography"]') as HTMLElement + ).innerText.trim(), + sections: document.querySelectorAll("h3"), getSName: (dom) => (dom as HTMLElement).innerText.trim(), getContent: (dom) => dom.querySelector(".widget-episodeBody"), contentPatch: (dom) => dom, language: "ja", }); +