Files
lnreader-plugins/index.ts
T
Marcin Majewski e1207c172a Add accordion and fix minor issues (#742)
* Add docs v0.0.1

* Add icon size warning

* Fix sourcenover caution

* Fix semver descriptions

* Filters fix

* Quickfix quickstart

* Add icon warning v2

* Fix typos

* Add NovelItem

* Update plugins host

* Update plugins host

* Remove unnecesarry .js and .dist

* Update plugins host

* Add host.ps1

* Fix readme.txt

* Fix READNE.txt, again

* Remove .js and .dist

* Add accordion

* Working pagination

* readme update with ost

* Add linux/windows hosting functions

* Add tswatch

* Fix fetch function

* Change plugin versions

* Fix chrome

* Fix workflows

* Fix search not showing results

* remove unnecesarry code

* Revert novelupdates changes

* Reverted NobleMTL instead of novelupdates, fixing that

* Revert nobleMTL to e346c7d0

* WTF NobleMTL?
2024-02-04 23:39:07 +07:00

148 lines
4.3 KiB
TypeScript

require("module-alias/register");
import path from "path";
import dayjs from "dayjs";
import express from "express";
import bodyParser from "body-parser";
import * as pluginApi from "./test_web/api/plugins";
import localizedFormat from "dayjs/plugin/localizedFormat";
const app = express();
const port = Number(process.argv[2]);
if (!port) {
process.exit(0);
}
const host = "localhost";
const dirname = path.join(__dirname, "..");
export const getHeaders = () => {
return pluginApi.fetchHeaders;
};
app.use((req, res, next) => {
const xHeaders = req.headers["x-custom-headers"];
if (xHeaders && typeof xHeaders === "string") {
for (const [headerName, headerValue] of Object.entries(
pluginApi.fetchHeaders
)) {
if (
typeof headerName === "string" &&
typeof headerValue === "string"
)
delete pluginApi.fetchHeaders[headerName];
}
const headers = JSON.parse(xHeaders);
for (const [headerName, headerValue] of Object.entries(headers)) {
if (
typeof headerName === "string" &&
typeof headerValue === "string"
)
pluginApi.fetchHeaders[headerName] = headerValue;
}
}
if (pluginApi.fetchHeaders.userAgent)
console.log("Custom headers: ", pluginApi.fetchHeaders);
next();
});
app.use(bodyParser.json());
app.use("/static", express.static(path.join(dirname, "test_web", "static")));
app.use("/icons", express.static(path.join(dirname, "icons")));
app.get("/all_plugins", (req, res) => {
const allPlugins = pluginApi.all_plugins();
res.json(allPlugins);
});
app.post("/filters", async (req, res) => {
const filters = await pluginApi.getFilter(req.body["pluginRequirePath"]);
res.json(filters || []);
});
app.post("/popularNovels/", async (req, res) => {
const requirePath = req.body["pluginRequirePath"];
const showLatestNovels = req.body["showLatestNovels"] || false;
const defaultFilters = await pluginApi.getFilter(requirePath);
const filters = showLatestNovels
? defaultFilters
: req.body["filters"] || defaultFilters;
const page = parseInt(req.body["page"]);
try {
const novels = await pluginApi.popularNovels(requirePath, page || 1, {
showLatestNovels,
filters,
});
res.json(novels);
} catch (err) {
res.json({ error: err });
}
});
app.post("/searchNovels/", async (req, res) => {
const page = parseInt(req.body["page"]) || 1;
try {
const novels = await pluginApi.searchNovels(
req.body["pluginRequirePath"],
page,
req.body["searchTerm"]
);
res.json(novels);
} catch (err) {
res.json({ error: err });
}
});
app.post("/parseNovelAndChapters/", async (req, res) => {
try {
const sourceNovel = await pluginApi.parseNovelAndChapters(
req.body["pluginRequirePath"],
req.body["novelUrl"]
);
res.json(sourceNovel);
} catch (err) {
res.json({ error: err });
}
});
app.post("/parseChapter/", async (req, res) => {
try {
const chapterText = await pluginApi.parseChapter(
req.body["pluginRequirePath"],
req.body["chapterUrl"]
);
res.send(chapterText);
} catch (err) {
res.json({ error: err });
}
});
app.post("/fetchImage/", async (req, res) => {
try {
const base64 = await pluginApi.fetchImage(
req.body["pluginRequirePath"],
req.body["url"]
);
res.send(base64);
} catch (err) {
res.json({ error: err });
}
});
app.get("/", (req, res) => {
res.sendFile(path.join(dirname, "test_web", "index.html"));
});
app.listen(port, host, () => {
console.log(`Testing plugins web listening on http://localhost:${port}`);
});
//Dayjs localization
const language = Intl?.DateTimeFormat()?.resolvedOptions()?.locale || "en";
require("dayjs/locale/ar");
require("dayjs/locale/de");
require("dayjs/locale/es");
require("dayjs/locale/it");
require("dayjs/locale/pt");
require("dayjs/locale/ru");
require("dayjs/locale/tr");
require("dayjs/locale/uk");
require("dayjs/locale/zh");
dayjs.locale(language);
dayjs.extend(localizedFormat);