Skip to content

Commit

Permalink
支持Kakuyomu新UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenSheiBot authored and yingziwu committed Jan 5, 2024
1 parent a156d12 commit 6b3b42b
Showing 1 changed file with 63 additions and 9 deletions.
72 changes: 63 additions & 9 deletions src/rules/onePage/original/kakuyomu.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLSpanElement>, 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<HTMLDivElement>, 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<HTMLButtonElement>): 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<HTMLSpanElement>('span');
const divElements = document.querySelectorAll<HTMLDivElement>('div');
const buttons = document.querySelectorAll<HTMLButtonElement>('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) => {
Expand All @@ -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",
});

0 comments on commit 6b3b42b

Please sign in to comment.