Feat: Create custom header changer (#694)
* Add custom header adder * Make custom headers work in apip * remove unnecesarry console.logs * Remove add-cookie button remains * getFile and remove unnecesarry ifs * Add warning and copy to code function
This commit is contained in:
@@ -6,19 +6,6 @@ import express from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import * as pluginApi from "./test_web/api/plugins";
|
||||
import localizedFormat from "dayjs/plugin/localizedFormat";
|
||||
interface GlobalState {
|
||||
UserAgent?: string,
|
||||
Cookie?: string,
|
||||
}
|
||||
let globalState: GlobalState = {
|
||||
}
|
||||
|
||||
export const getHeaders = () => {
|
||||
return {
|
||||
'User-Agent': globalState.UserAgent,
|
||||
'Cookie': globalState.Cookie,
|
||||
};
|
||||
};
|
||||
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
@@ -26,14 +13,27 @@ const host = "localhost";
|
||||
|
||||
const dirname = path.join(__dirname, "..");
|
||||
|
||||
export const getHeaders = () => {
|
||||
return pluginApi.fetchHeaders;
|
||||
};
|
||||
|
||||
app.use((req, res, next) => {
|
||||
globalState.UserAgent = req.headers["user-agent"];
|
||||
console.log('---------');
|
||||
console.log('default headers: ', globalState);
|
||||
console.log('---------');
|
||||
console.log();
|
||||
const xHeaders = req.headers["x-custom-headers"];
|
||||
if (xHeaders && typeof xHeaders === "string") {
|
||||
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")));
|
||||
@@ -41,18 +41,10 @@ app.get("/all_plugins", (req, res) => {
|
||||
const allPlugins = pluginApi.all_plugins();
|
||||
res.json(allPlugins);
|
||||
});
|
||||
app.post("/add_cookie", async (req, res) => {
|
||||
globalState.Cookie = req.body['cookie'];
|
||||
console.log('---------');
|
||||
console.log('default headers: ', globalState);
|
||||
console.log('---------');
|
||||
console.log();
|
||||
res.json({});
|
||||
});
|
||||
app.post("/filters", async (req, res) => {
|
||||
const filters = await pluginApi.getFilter(req.body['pluginRequirePath']);
|
||||
const filters = await pluginApi.getFilter(req.body["pluginRequirePath"]);
|
||||
res.json(filters || []);
|
||||
})
|
||||
});
|
||||
app.post("/popularNovels/", async (req, res) => {
|
||||
const filters = req.body["filters"] || {};
|
||||
const showLatestNovels = req.body["showLatestNovels"] || false;
|
||||
@@ -81,7 +73,7 @@ app.post("/parseChapter/", async (req, res) => {
|
||||
req.body["pluginRequirePath"],
|
||||
req.body["chapterUrl"]
|
||||
);
|
||||
res.json(chapterText);
|
||||
res.send(chapterText);
|
||||
});
|
||||
app.post("/fetchImage/", async (req, res) => {
|
||||
const base64 = await pluginApi.fetchImage(
|
||||
|
||||
Reference in New Issue
Block a user