Skip to content

Commit

Permalink
Update WxscsPaser.js
Browse files Browse the repository at this point in the history
Fix running plugin when on chapter page
  • Loading branch information
phazei authored Dec 30, 2024
1 parent 9516ccd commit 8bdf969
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions plugin/js/parsers/WxscsPaser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ class WxscsParser extends Parser {
}

async getChapterUrls(dom) {
// Check if we're on a chapter page
if (!dom.querySelector("div#all-chapter")) {
// Extract book id from script tag info
let bookId = this.extractBookId(dom);
if (bookId) {
// Fetch the book's index page
let indexUrl = `https://www.wxscs.com/book/${bookId}/`;
let indexDom = (await HttpClient.wrapFetch(indexUrl)).responseXML;
return this.getChapterUrls(indexDom);
}
}

// Extract urls from the chapter list div
return [...dom.querySelectorAll("div#all-chapter .panel-body a")]
.map(a => util.hyperLinkToChapter(a));
Expand Down Expand Up @@ -56,6 +68,29 @@ class WxscsParser extends Parser {
return dom.querySelector("div#cont-body");
}

extractBookId(dom) {
// Try to get book ID from script tag
let scripts = [...dom.querySelectorAll("script")]
.map(s => s.textContent)
.filter(s => s.includes("book.id="));

if (scripts.length > 0) {
let match = scripts[0].match(/book\.id="(\d+)"/);
if (match) {
return match[1];
}
}

// Fallback: try to get from URL
let url = new URL(dom.baseURI);
let parts = url.pathname.split("/");
if (parts.length >= 3 && parts[1] === "book") {
return parts[2];
}

return null;
}

// Get chapter title from h1 element
findChapterTitle(dom) {
return dom.querySelector("h1.cont-title");
Expand All @@ -68,8 +103,31 @@ class WxscsParser extends Parser {

// Get author (optional)
extractAuthor(dom) {
let authorLabel = dom.querySelector("div.book-info a[href*='author']");
return authorLabel?.textContent ?? super.extractAuthor(dom);
// Try book info div (for index page)
let authorLink = dom.querySelector("div.book-info a[href*='author']");
if (authorLink?.textContent) {
return authorLink.textContent;
}

// Try meta tag
let metaAuthor = dom.querySelector("meta[property='og:novel:author']")?.content;
if (metaAuthor) {
return metaAuthor;
}

// Try book object in script (for chapter pages)
let scripts = [...dom.querySelectorAll("script")]
.map(s => s.textContent)
.filter(s => s.includes("book.author="));

if (scripts.length > 0) {
let match = scripts[0].match(/book\.author="([^"]+)"/);
if (match) {
return match[1];
}
}

return super.extractAuthor(dom);
}

removeUnwantedElementsFromContentElement(element) {
Expand Down

0 comments on commit 8bdf969

Please sign in to comment.