Skip to content

Commit

Permalink
#716 更改linovelib字体获取方式
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm0206 committed Jan 4, 2025
1 parent 0c88a8d commit dde1b30
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/lib/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface NextPageParseOptions {
chapterUrl: string;
charset: string;
selector: string;
domPatch?: (dom: Document) => Promise<Document>;
contentPatch: (_content: HTMLElement, doc: Document) => HTMLElement;
getNextPage: (doc: Document) => string;
continueCondition: (content: HTMLElement, nextLink: string) => boolean;
Expand All @@ -46,6 +47,7 @@ export async function nextPageParse({
chapterUrl,
charset,
selector,
domPatch,
contentPatch,
getNextPage,
continueCondition,
Expand All @@ -59,6 +61,9 @@ export async function nextPageParse({

let flag = false;
do {
if (domPatch) {
doc = await domPatch(doc);
}
let _content = doc.querySelector(selector) as HTMLElement;

const nextLink = getNextPage(doc);
Expand Down
3 changes: 3 additions & 0 deletions src/rules/special/original/kadokado.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { rm } from "../../../lib/dom";
export class kadokado extends BaseRuleClass {
public constructor() {
super();
this.maxRunLimit = 1;
this.sleepTime = 700;
this.maxSleepTime = 4000;
this.concurrencyLimit = 1;
this.attachmentMode = "TM";
}
Expand Down
110 changes: 108 additions & 2 deletions src/rules/twoPage/linovelib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { rm, sandboxed } from "../../lib/dom";
import { Book } from "../../main/Book";
import { Chapter } from "../../main/Chapter";
import { table } from "../lib/linovelib";
import { log } from "../../log";
import { sleep } from "../../lib/misc";

const chapterFixSleepTime = 2000;
const concurrencyLimit = 1;
const sleepTime = 600;
const maxSleepTime = 3000;

const maxRunLimit = 1;
export const linovelib = () => {
const ToCurl = document.location.href;
const bookUrl = ToCurl.replace(/\/catalog$/, ".html");
Expand Down Expand Up @@ -41,7 +43,7 @@ export const linovelib = () => {
},
getAList: () => document.querySelectorAll(".chapter-list li.col-4 > a"),
getSections: () => document.querySelectorAll("#volume-list > div.volume"),
getSName: (sElem) => (sElem.querySelector(".volume-info >h2") as HTMLElement )?.innerText.trim(),
getSName: (sElem) => (sElem.querySelector(".volume-info >h2") as HTMLElement)?.innerText.trim(),
postHook: (chapter) => {
if (chapter.chapterUrl.startsWith("javascript")) {
chapter.status = Status.aborted;
Expand Down Expand Up @@ -74,6 +76,7 @@ export const linovelib = () => {
chapterUrl,
charset,
selector: "#TextContent",
domPatch: domFontFix,
contentPatch: (_content) => {
rm(".tp", true, _content);
rm(".bd", true, _content);
Expand Down Expand Up @@ -101,6 +104,7 @@ export const linovelib = () => {
}
return content;
},
maxRunLimit: maxRunLimit,
concurrencyLimit: concurrencyLimit,
sleepTime: sleepTime,
maxSleepTime: maxSleepTime,
Expand Down Expand Up @@ -224,6 +228,7 @@ export const wlinovelib = () => {
chapterUrl,
charset,
selector: "#acontent",
domPatch: domFontFix,
contentPatch: (_content) => {
rm(".cgo", true, _content);
rm("script", true, _content);
Expand All @@ -248,8 +253,109 @@ export const wlinovelib = () => {
return contentRaw;
},
contentPatch: (dom) => dom,
maxRunLimit: maxRunLimit,
concurrencyLimit: concurrencyLimit,
sleepTime: sleepTime,
maxSleepTime: maxSleepTime,
});
};

export async function domFontFix(dom: Document) {
const FontJS = 'font|read||sheet|family|url|public|woff2';
let isNeedFix = false;
dom.querySelectorAll("script").forEach((script) => {
if (script.innerHTML.includes(FontJS)) {
isNeedFix = true;
}
});
if (!isNeedFix) {
return dom;
}
const domPatch = dom.querySelector("#TextContent p:nth-last-of-type(2)") as HTMLElement;
if (domPatch) {
domPatch.innerHTML = await replaceCharacter(domPatch.innerHTML);
}
return dom;
}
export async function replaceCharacter(
inputText: string,
) {
const fontName = "read.woff2";
const fontlink = "https://www.linovelib.com/public/font/read.woff2";
let outputText = inputText;
const FontTable = await getFanqieFontTable(fontName, fontlink);
if (FontTable) {
for (const Character in FontTable) {
if (
Object.prototype.hasOwnProperty.call(FontTable, Character)
) {
const normalCharacter = FontTable[Character];
outputText = outputText.replaceAll(Character, normalCharacter);
}
}
// outputText = outputText.replace(/\u200c/g, "");
} else {
return `[linovelib-font]字体对照表 ${fontName} 未找到,请前往https://github.com/404-novel-project/Universal_font_tables 提交字体链接, ${fontlink}`;
}
return outputText;
}

async function getFanqieFontTable(fontName: string, fontlink: string) {
const FontTable = await fetchRemoteFont(fontName);
if (!FontTable) {
log.error(`[linovelib-font]字体对照表 ${fontName} 未找到,请前往https://github.com/404-novel-project/Universal_font_tables 提交字体链接, ${fontlink}`);
} else {
log.debug(`[linovelib-font]字体对照表 ${fontName}已找到,如果你认为字体对应有错误,请前往https://github.com/404-novel-project/Universal_font_tables 重新提交字体链接, ${fontlink}`);
}
return FontTable;
}

async function fetchRemoteFont(fontName: string) {
const url = `https://fastly.jsdelivr.net/gh/404-novel-project/Universal_font_tables@master/${fontName}.json`;
log.info(`[linovelib-font]开始请求远程字体对照表 ${fontName}`);
const retryLimit = 10;
let retry = retryLimit;
while (retry > 0) {
let responseStatus = -1;
try {
const response = await new Promise<FontTable | undefined>((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: (response) => {
responseStatus = response.status;
if (response.status >= 200 && response.status < 300) {
log.info(`[linovelib-font]远程字体对照表 ${fontName} 下载成功`);
resolve(JSON.parse(response.responseText) as FontTable);
}
else {
reject(new Error(`HTTP status ${response.status}`));
}
},
onerror: (error) => {
reject(error);
}
});
});

if (response) {
return response;
}
} catch (error) {
log.error(error);
retry--;
if (responseStatus === 404 || retry < 0) {
log.info(`[linovelib-font]远程字体对照表 ${fontName} 下载失败`);
return undefined;
}
else {
await sleep(2000);
continue;
}
}
}
}

interface FontTable {
[index: string]: string;
}
5 changes: 5 additions & 0 deletions src/rules/twoPage/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface MkRuleClassOptions {
) => Promise<HTMLElement | null>;
getContent?: (doc: Document) => HTMLElement | null;
contentPatch: (content: HTMLElement) => HTMLElement;
maxRunLimit?: number;
concurrencyLimit?: number;
sleepTime?: number;
maxSleepTime?: number;
Expand Down Expand Up @@ -67,6 +68,7 @@ export function mkRuleClass({
getContentFromUrl,
getContent,
contentPatch,
maxRunLimit,
concurrencyLimit,
sleepTime,
maxSleepTime,
Expand All @@ -80,6 +82,9 @@ export function mkRuleClass({
public constructor() {
super();
this.attachmentMode = "TM";
if (maxRunLimit) {
this.maxRunLimit = maxRunLimit;
}
if (concurrencyLimit) {
this.concurrencyLimit = concurrencyLimit;
}
Expand Down

0 comments on commit dde1b30

Please sign in to comment.