Skip to content

Commit

Permalink
Merge pull request #165 from caorushizi/feat/addForm
Browse files Browse the repository at this point in the history
feat: ✨  修改创建修改表单
  • Loading branch information
caorushizi authored May 26, 2024
2 parents 6fb7e94 + 0d0839f commit 1f838a9
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 373 deletions.
5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ export default [
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
rules: {
"@typescript-eslint/no-explicit-any": "warn",
},
},
];
2 changes: 2 additions & 0 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@eslint/js": "^9.3.0",
"@types/electron-devtools-installer": "^2.2.5",
"@types/fs-extra": "^11.0.4",
"@types/glob": "^8.1.0",
"@types/gulp": "^4.0.17",
"@types/lodash": "^4.17.4",
"@types/mime-types": "^2.1.4",
Expand All @@ -40,6 +41,7 @@
"esbuild": "^0.21.4",
"esbuild-register": "^3.5.0",
"eslint": "^9.3.0",
"glob": "^10.4.1",
"globals": "^15.3.0",
"gulp": "^5.0.0",
"prebuild-install": "^7.1.2",
Expand Down
1 change: 0 additions & 1 deletion packages/main/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ async function watchTask() {
})
.on("error", (error) => {
consola.error(error);
process.exit();
});
return Promise.resolve();
}
Expand Down
8 changes: 4 additions & 4 deletions packages/main/src/controller/DownloadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class DownloadController implements Controller {
@inject(TYPES.MainWindow)
private readonly mainWindow: MainWindow,
@inject(TYPES.WebviewService)
private readonly webviewService: WebviewService,
private readonly webviewService: WebviewService
) {}

@handle("show-download-dialog")
Expand All @@ -36,12 +36,12 @@ export default class DownloadController implements Controller {
this.webviewService.sendToWindow(
"show-download-dialog",
data,
image.toDataURL(),
image.toDataURL()
);
}

@handle("add-download-item")
async addDownloadItem(e: IpcMainEvent, video: DownloadItem) {
async addDownloadItem(e: IpcMainEvent, video: Omit<DownloadItem, "id">) {
const item = await this.videoRepository.addVideo(video);
// 这里向页面发送消息,通知页面更新
this.mainWindow.send("download-item-notifier", item);
Expand All @@ -63,7 +63,7 @@ export default class DownloadController implements Controller {
}

@handle("download-now")
async downloadNow(e: IpcMainEvent, video: DownloadItem) {
async downloadNow(e: IpcMainEvent, video: Omit<DownloadItem, "id">) {
// 添加下载项
const item = await this.addDownloadItem(e, video);
// 开始下载
Expand Down
5 changes: 2 additions & 3 deletions packages/main/src/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ export async function sleep(second = 1): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, second * 1000));
}

export function formatHeaders(headersStr: string): string {
const headers: Record<string, any> | null = JSON.parse(headersStr);
export function formatHeaders(headers: Record<string, string>): string {
if (!headers) return "";
const formatted = Object.entries(headers)
.map(([key, value]) => `${key}:${value}`)
.join("|");
.join("\n");
return formatted;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface DownloadItem {
headers?: string;
status?: DownloadStatus;
isLive?: boolean;
numberOfEpisodes?: number;
teleplay?: boolean;
}

export enum DownloadFilter {
Expand Down
7 changes: 2 additions & 5 deletions packages/main/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const electronApi = {
return ipcRenderer.invoke("get-favorites");
},
addFavorite(
favorite: Omit<Favorite, "id" | "createdDate" | "updatedDate">,
favorite: Omit<Favorite, "id" | "createdDate" | "updatedDate">
): Promise<Favorite> {
return ipcRenderer.invoke("add-favorite", favorite);
},
Expand Down Expand Up @@ -53,7 +53,7 @@ const electronApi = {
},
setAppStore(
key: keyof AppStore,
val: AppStore[keyof AppStore],
val: AppStore[keyof AppStore]
): Promise<void> {
return ipcRenderer.invoke("set-app-store", key, val);
},
Expand Down Expand Up @@ -137,9 +137,6 @@ const electronApi = {
setUserAgent(isMobile: boolean): Promise<void> {
return ipcRenderer.invoke("webview-change-user-agent", isMobile);
},
downloadItem(data: Omit<DownloadItem, "id">) {
return ipcRenderer.invoke("add-download-item", data);
},
getDownloadLog(id: number): Promise<string> {
return ipcRenderer.invoke("get-download-log", id);
},
Expand Down
15 changes: 8 additions & 7 deletions packages/main/src/services/SniffingHelperService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { DownloadType } from "../interfaces.ts";
import { TYPES } from "../types.ts";
import ElectronLogger from "../vendor/ElectronLogger.ts";
import EventEmitter from "events";
import { session } from "electron";
import { PERSIST_WEBVIEW } from "../helper/index.ts";
import { OnCompletedListenerDetails } from "electron/main";
import { OnSendHeadersListenerDetails, session } from "electron";
import { PERSIST_WEBVIEW, formatHeaders } from "../helper/index.ts";

export interface SourceParams {
url: string;
documentURL: string;
name: string;
type: DownloadType;
headers?: string;
}

export interface SourceFilter {
Expand Down Expand Up @@ -49,7 +49,7 @@ export class SniffingHelper extends EventEmitter {

constructor(
@inject(TYPES.ElectronLogger)
private readonly logger: ElectronLogger,
private readonly logger: ElectronLogger
) {
super();
}
Expand Down Expand Up @@ -87,7 +87,7 @@ export class SniffingHelper extends EventEmitter {

start() {
const viewSession = session.fromPartition(PERSIST_WEBVIEW);
viewSession.webRequest.onCompleted(this.onCompleted);
viewSession.webRequest.onSendHeaders(this.onSendHeaders);
}

send = (item: SourceParams) => {
Expand All @@ -100,8 +100,8 @@ export class SniffingHelper extends EventEmitter {
}
};

private onCompleted = (details: OnCompletedListenerDetails): void => {
const { url } = details;
private onSendHeaders = (details: OnSendHeadersListenerDetails): void => {
const { url, requestHeaders } = details;
const { title, url: documentURL } = this.pageInfo;

listLoop: for (const filter of filterList) {
Expand All @@ -117,6 +117,7 @@ export class SniffingHelper extends EventEmitter {
documentURL,
name: title,
type: filter.type,
headers: formatHeaders(requestHeaders),
});
break listLoop;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin/src/components/BilibiliButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LitElement, html, css } from "lit";
import { customElement, property } from "lit/decorators.js";
import { BILIBILI_DOWNLOAD_BUTTON, showDownloadDialog } from "../helper";
import $ from "jquery";
import { DownloadType } from "../types";

@customElement("bilibili-button")
export class BilibiliButton extends LitElement {
Expand Down Expand Up @@ -39,7 +40,7 @@ export class BilibiliButton extends LitElement {
const videoImage = $(BILIBILI_DOWNLOAD_BUTTON).eq(this.index);
const url = videoImage.attr("href") || "";
const name = videoImage.parent().find(".bili-video-card__info--tit").text();
showDownloadDialog([{ name, url, type: "bilibili" }]);
showDownloadDialog([{ name, url, type: DownloadType.bilibili }]);
}

render() {
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin/src/components/FloatButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface SourceData {
documentURL: string;
name: string;
type: DownloadType;
headers?: string;
}

@customElement("float-button")
Expand Down Expand Up @@ -133,7 +134,7 @@ export class FloatButton extends LitElement {

getPosition = (
newLeft: number,
newTop: number,
newTop: number
): { left: number; top: number } => {
if (!this.button) return { left: 0, top: 0 };

Expand Down Expand Up @@ -210,6 +211,8 @@ export class FloatButton extends LitElement {

function init() {
const floatButton = document.createElement("float-button");
floatButton.style.position = "fixed";
floatButton.style.zIndex = "9999999999999";
document.body.appendChild(floatButton);

// 向主进程发送插件准备好的消息
Expand Down
7 changes: 2 additions & 5 deletions packages/plugin/src/helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { nanoid } from "nanoid";
import { DownloadItem } from "../../../main/types/interfaces";

const eventMap = new Map();

Expand Down Expand Up @@ -29,11 +30,7 @@ export interface Item {
type: any;
}

export function downloadItem(item: Item) {
window.electron.downloadItem(item);
}

export function showDownloadDialog(item: Item[]) {
export function showDownloadDialog(item: Omit<DownloadItem, "id">[]) {
window.electron.showDownloadDialog(item);
}

Expand Down
2 changes: 0 additions & 2 deletions packages/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
"@types/react-dom": "^18.3.0",
"@types/react-redux": "^7.1.33",
"@types/sort-by": "^1.2.3",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "7.10.0",
"@vitejs/plugin-react-swc": "^3.7.0",
"eslint": "^9.3.0",
"globals": "^15.3.0",
Expand Down
Loading

0 comments on commit 1f838a9

Please sign in to comment.