Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
style: format&lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuhanawa committed Aug 5, 2024
1 parent 3056876 commit 735824f
Show file tree
Hide file tree
Showing 29 changed files with 140 additions and 342 deletions.
35 changes: 8 additions & 27 deletions lib/src/build/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ function build(config, config_dev, startInfo, root) {
const jsCodemap = new Map();

if (!fs.existsSync(buildInfo.paths.configfile)) {
logger.warn(
`${root} will be skip, because '${buildInfo.paths.configfile}' not found.`,
);
logger.warn(`${root} will be skip, because '${buildInfo.paths.configfile}' not found.`);
return;
}
if (!fs.existsSync(buildInfo.paths.srcdir)) {
logger.warn(
`${root} will be skip, because '${buildInfo.paths.srcdir}' not found.`,
);
logger.warn(`${root} will be skip, because '${buildInfo.paths.srcdir}' not found.`);
return;
}
if (!fs.existsSync(buildInfo.paths.headerfile)) {
Expand All @@ -53,11 +49,7 @@ function build(config, config_dev, startInfo, root) {

for (const fullPath of getJsFilesRecursively(buildInfo.paths.srcdir)) {
try {
const code = processSingleJs(
path.basename(fullPath),
fullPath,
buildInfo,
);
const code = processSingleJs(path.basename(fullPath), fullPath, buildInfo);
jsCodemap.set(path.basename(fullPath), code);
} catch (error) {
logger.err(`processSingleJs: ${fullPath}`, error);
Expand All @@ -76,18 +68,14 @@ function processSingleJs(filename, filepath, buildInfo) {
const regex_STYLE = /\$STYLE\((.+?)\)/g;
code = code.replace(regex_STYLE, (_match, sassFileName) => {
let fixedSassFileName = sassFileName.replaceAll(/[\s"']/g, "");
if (!fixedSassFileName.toLowerCase().endsWith(".sass"))
fixedSassFileName += ".sass";
if (!fixedSassFileName.toLowerCase().endsWith(".sass")) fixedSassFileName += ".sass";
const sassFilePath = path.join(buildInfo.paths.styledir, fixedSassFileName);
return `\`${csso.minify(sass.compile(sassFilePath).css).css}\``;
});

const key = filename.substring(0, filename.indexOf("."));
let result = `\n// ${filename}\n`;
code = `${code.substring(
0,
code.indexOf("{") + 1,
)}\nkey: "${key}", ${code.substring(code.indexOf("{") + 1)}`;
code = `${code.substring(0, code.indexOf("{") + 1)}\nkey: "${key}", ${code.substring(code.indexOf("{") + 1)}`;

result += `addModule${code}\n`;
return result;
Expand All @@ -99,17 +87,12 @@ function getMainJsStr(folderPath) {
// 读取main.js文件(如果存在)
const mainJSPath = path.join(folderPath, "main.js");
if (fs.existsSync(mainJSPath)) {
concatenatedContent += `// main.js\n${fs.readFileSync(
mainJSPath,
"utf8",
)}\n`;
concatenatedContent += `// main.js\n${fs.readFileSync(mainJSPath, "utf8")}\n`;
}

// 读取所有main-*.js文件
const files = fs.readdirSync(folderPath);
const mainFiles = files.filter(
(file) => file.startsWith("main-") && file.endsWith(".js"),
);
const mainFiles = files.filter((file) => file.startsWith("main-") && file.endsWith(".js"));

for (const file of mainFiles) {
const filePath = path.join(folderPath, file);
Expand All @@ -125,9 +108,7 @@ function getFullCode(codemap, buildInfo) {
const config_js = res.get_config_js();
const debug_js = buildInfo.startInfo.debug ? res.get_debug_js() : "";

const indexStr = fs.existsSync(buildInfo.paths.indexfile)
? fs.readFileSync(buildInfo.paths.indexfile, "utf8")
: "";
const indexStr = fs.existsSync(buildInfo.paths.indexfile) ? fs.readFileSync(buildInfo.paths.indexfile, "utf8") : "";
const config = fs.readJsonSync(buildInfo.paths.configfile, "utf8");
const configVarStr = `var config = ${JSON.stringify(config, null, 0)};`;
const headerStr = `${fs.readFileSync(buildInfo.paths.headerfile, "utf8")}\n`
Expand Down
18 changes: 4 additions & 14 deletions lib/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ function main(startInfo) {
if (!fs.existsSync("gmu.json")) {
console.error("gmu.json not found");
fs.writeFileSync("gmu.json", res.get_default_gmu_json());
console.log(
"Now, gmu.json was created in the root folder of your project.",
);
console.log("Now, gmu.json was created in the root folder of your project.");
console.log("Please edit it and restart the script.");
return;
}
Expand All @@ -40,26 +38,18 @@ function main(startInfo) {

const subDirs = fs.readdirSync(config.root_path).filter((file) => {
const filePath = path.join(config.root_path, file);
return (
!config_dev.not_build.includes(file) &&
fs.statSync(filePath).isDirectory()
);
return !config_dev.not_build.includes(file) && fs.statSync(filePath).isDirectory();
});

const subPaths = subDirs.map((sub) => `${config.root_path}/${sub}`);

subPaths.forEach((scriptpath) =>
script.tryBuild(config, config_dev, startInfo, scriptpath),
);
subPaths.forEach((scriptpath) => script.tryBuild(config, config_dev, startInfo, scriptpath));

if (startInfo.watch) {
watcher.watch(config, config_dev, startInfo, subPaths);
}

if (
startInfo.server ||
(startInfo.command === "dev" && config_dev.server.alwaysStartOnDev)
) {
if (startInfo.server || (startInfo.command === "dev" && config_dev.server.alwaysStartOnDev)) {
server.tryStartServer(subPaths, config_dev.server.port);
}
}
Expand Down
8 changes: 3 additions & 5 deletions lib/src/utils/fs.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const fs = require("fs-extra");
const path = require("path");
const path = require("node:path");

function getJsFilesRecursively(dirPath) {
let results = [];
try {
for (const item of fs.readdirSync(dirPath)) {
const fullPath = path.join(dirPath, item);

if (fs.statSync(fullPath).isDirectory())
results = results.concat(getJsFilesRecursively(fullPath));
else if (path.extname(item).toLowerCase() === ".js")
results.push(fullPath);
if (fs.statSync(fullPath).isDirectory()) results = results.concat(getJsFilesRecursively(fullPath));
else if (path.extname(item).toLowerCase() === ".js") results.push(fullPath);
}
} catch (error) {
console.error("Error reading directory:", error);
Expand Down
25 changes: 5 additions & 20 deletions lib/src/utils/res.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,20 @@ const fs = require("fs-extra");
const path = require("node:path");

function get_core_js() {
return fs.readFileSync(
path.resolve(__dirname, "..", "..", "res/core.js"),
"utf8",
);
return fs.readFileSync(path.resolve(__dirname, "..", "..", "res/core.js"), "utf8");
}
function get_config_js() {
return fs.readFileSync(
path.resolve(__dirname, "..", "..", "res/config.js"),
"utf8",
);
return fs.readFileSync(path.resolve(__dirname, "..", "..", "res/config.js"), "utf8");
}
function get_debug_js() {
return fs.readFileSync(
path.resolve(__dirname, "..", "..", "res/debug.js"),
"utf8",
);
return fs.readFileSync(path.resolve(__dirname, "..", "..", "res/debug.js"), "utf8");
}

function get_default_gmu_json() {
return fs.readFileSync(
path.resolve(__dirname, "..", "..", "res/default.gmu.json"),
"utf8",
);
return fs.readFileSync(path.resolve(__dirname, "..", "..", "res/default.gmu.json"), "utf8");
}
function get_default_gmu_dev_json() {
return fs.readFileSync(
path.resolve(__dirname, "..", "..", "res/default.gmu.dev.json"),
"utf8",
);
return fs.readFileSync(path.resolve(__dirname, "..", "..", "res/default.gmu.dev.json"), "utf8");
}

module.exports = {
Expand Down
5 changes: 1 addition & 4 deletions userscripts/bilibili/src/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// (美化总开关)样式美化 & 自定义背景等
showInMenu: true,
value: () => {
if (
location.href === "https://www.bilibili.com/" &&
get("beautify_work_on_index") === false
)
if (location.href === "https://www.bilibili.com/" && get("beautify_work_on_index") === false)
return;

if (get("video_radius")) {
Expand Down
3 changes: 1 addition & 2 deletions userscripts/bilibili/src/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
delay(
() => {
for (const x of document.getElementsByClassName("reply-item")) check(x);
for (const x of document.getElementsByClassName("sub-reply-item"))
check(x);
for (const x of document.getElementsByClassName("sub-reply-item")) check(x);
},
2000,
{ loop: true },
Expand Down
2 changes: 1 addition & 1 deletion userscripts/bilibili/src/header.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
({
value: {
default:"",
default: "",
fixed: ".header-channel,.bili-header__bar.slide-down{display: none!important;}",
blur: $STYLE("header_blur"),
},
Expand Down
4 changes: 2 additions & 2 deletions userscripts/bilibili/src/home/banner_shadow.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
({
value:$STYLE("home/banner_shadow")
})
value: $STYLE("home/banner_shadow"),
});
4 changes: 1 addition & 3 deletions userscripts/bilibili/src/home/card_shadow.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
({
value: `.bili-video-card bili-video-card__wrap{box-shadow: ${cfg(
"card_shadow_value",
)};}`,
value: `.bili-video-card bili-video-card__wrap{box-shadow: ${cfg("card_shadow_value")};}`,
});
12 changes: 6 additions & 6 deletions userscripts/bilibili/src/home/header_channel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
({
value:{
default:null,
transparent:$STYLE("home/header_channel_transparent"),
hidden:$STYLE("home/header_channel_hidden")
}
})
value: {
default: null,
transparent: $STYLE("home/header_channel_transparent"),
hidden: $STYLE("home/header_channel_hidden"),
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
({
// 移除轮播图及周边区域
pages:["home"],
value: $STYLE("home/remove_carousel_and_feed_card")
})
// 移除轮播图及周边区域
pages: ["home"],
value: $STYLE("home/remove_carousel_and_feed_card"),
});
8 changes: 4 additions & 4 deletions userscripts/bilibili/src/home/remove_special_card.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
({
// 移除左上角带特殊标识的卡片
pages:["home"],
value: $STYLE("home/remove_special_card")
})
// 移除左上角带特殊标识的卡片
pages: ["home"],
value: $STYLE("home/remove_special_card"),
});
3 changes: 1 addition & 2 deletions userscripts/bilibili/src/hotkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
if (!img_view) return;

img_view.addEventListener("keydown", (e) => {
if (e.key === "Escape")
img_view.getElementsByClassName("close-container")[0].click();
if (e.key === "Escape") img_view.getElementsByClassName("close-container")[0].click();
if (e.key === "a" || e.key === "ArrowLeft")
img_view.getElementsByClassName("last-image")[0].click();
if (e.key === "d" || e.key === "ArrowRight")
Expand Down
20 changes: 6 additions & 14 deletions userscripts/bilibili/src/quickly_copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
feature.fn(
"[标题]链接",
() =>
`【${document.querySelector("h1.video-title").innerText}】\t${
location.origin
}${location.pathname}`,
`【${document.querySelector("h1.video-title").innerText}】\t${location.origin}${
location.pathname
}`,
);
},
BV: (feature) => {
Expand All @@ -19,10 +19,7 @@
feature.fn("链接", () => `${location.origin}${location.pathname}`);
},
title: (feature) => {
feature.fn(
"标题",
() => `${document.querySelector("h1.video-title").innerText}`,
);
feature.fn("标题", () => `${document.querySelector("h1.video-title").innerText}`);
},
off: null,
},
Expand All @@ -31,20 +28,15 @@
() => {
const h1 = document.querySelector("h1.video-title");
if (!h1) return;
if (
document.querySelector("h1.video-title").innerHTML.indexOf("🏷️") !== -1
)
return;
if (document.querySelector("h1.video-title").innerHTML.indexOf("🏷️") !== -1) return;

const text = getText();
const copy_btn = document.createElement("span");
copy_btn.title = `复制当前视频的${title}:${text}`;
copy_btn.style.cursor = "pointer";
copy_btn.style.fontSize = "22px";
copy_btn.innerText = "🏷️";
copy_btn.addEventListener("click", () =>
navigator.clipboard.writeText(text),
);
copy_btn.addEventListener("click", () => navigator.clipboard.writeText(text));

document.querySelector("h1.video-title").append(copy_btn);
},
Expand Down
10 changes: 5 additions & 5 deletions userscripts/bilibili/src/remove_keyword_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
showInMenu: true,
value: {
icon: () => ".icon.search-word:{display:none;}",
color: () =>
".icon.search-word:{display:none;} .search-word a{color: #222!important;}",
color: () => ".icon.search-word:{display:none;} .search-word a{color: #222!important;}",
link: () => {
delay(
() => {
const as = document.getElementsByClassName("search-word");
for (let i = 0; i < as.length; i++)
as[i].parentElement.outerHTML = as[
i
].parentElement.outerHTML.replace(as[i].outerHTML, as[i].outerText);
as[i].parentElement.outerHTML = as[i].parentElement.outerHTML.replace(
as[i].outerHTML,
as[i].outerText,
);
},
8000,
{ loop: true },
Expand Down
8 changes: 2 additions & 6 deletions userscripts/bilibili/src/video_cover_download.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@
if (old_pic === pic) return;
old_pic = pic;
setTimeout(() => {
const toolbar = document.querySelector(
"#arc_toolbar_report .video-toolbar-right",
);
const toolbar = document.querySelector("#arc_toolbar_report .video-toolbar-right");
if (!toolbar) return;
if (!toolbar.querySelector(".video-tool-more")) {
// 等待加载完全 否则会出bug
old_pic = "";
return;
}

toolbar
.querySelectorAll(".video-tool-getpic")
.forEach((e) => e.remove());
toolbar.querySelectorAll(".video-tool-getpic").forEach((e) => e.remove());

const btn = document.createElement("div");
btn.className = "video-toolbar-right-item video-tool-getpic";
Expand Down
Loading

0 comments on commit 735824f

Please sign in to comment.