Skip to content

Commit

Permalink
Chore: refactor (#596)
Browse files Browse the repository at this point in the history
* use WebContentView instead of deprecated BrowserView API

* refactor providers

* fix forwardRef warning
  • Loading branch information
an-lee committed May 10, 2024
1 parent 8ad2666 commit 8afb942
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 134 deletions.
32 changes: 16 additions & 16 deletions enjoy/src/main/providers/audible-provider.ts
@@ -1,6 +1,6 @@
import log from "@main/logger";
import $ from "cheerio";
import { BrowserView, ipcMain } from "electron";
import { WebContentsView, ipcMain } from "electron";

const logger = log.scope("providers/audible-provider");

Expand All @@ -13,23 +13,29 @@ export class AudibleProvider {

scrape = async (path: string) => {
logger.debug(`Scraping ${this.baseURL + path}`);
return new Promise<string>((resolve, reject) => {
const view = new BrowserView();
return new Promise<string>((resolve, _reject) => {
const view = new WebContentsView();
view.webContents.loadURL(this.baseURL + path);
view.webContents.on("did-finish-load", () => {
logger.debug(`Scraped ${this.baseURL + path}`);
view.webContents
.executeJavaScript(`document.documentElement.innerHTML`)
.then((html) => resolve(html as string));
.then((html) => {
resolve(html as string);
})
.finally(() => view.webContents.close());
});
view.webContents.on("did-fail-load", () => {
logger.error(`Failed to scrape ${this.baseURL + path}`);
reject();
logger.warn(`Failed to scrape ${this.baseURL + path}`);
view.webContents.close();
resolve("");
});
});
};

extractCategories = async (html: string) => {
if (!html) return [];

const categories: { id: number; label: string }[] = [];

$.load(html)(".leftSlot a.refinementFormLink").each((_, el) => {
Expand All @@ -47,6 +53,8 @@ export class AudibleProvider {
};

extractAudios = async (html: string) => {
if (!html) return { books: [], hasNextPage: false };

const books: AudibleBookType[] = [];

$.load(html)("li.bc-list-item.productListItem").each((_, el) => {
Expand Down Expand Up @@ -114,19 +122,11 @@ export class AudibleProvider {

registerIpcHandlers = () => {
ipcMain.handle("audible-provider-categories", async () => {
try {
return await this.categories();
} catch (error) {
logger.error(error);
}
return await this.categories();
});

ipcMain.handle("audible-provider-bestsellers", async (_, params) => {
try {
return await this.bestsellers(params);
} catch (error) {
logger.error(error);
}
return await this.bestsellers(params);
});
};
}
44 changes: 18 additions & 26 deletions enjoy/src/main/providers/ted-provider.ts
@@ -1,13 +1,13 @@
import log from "@main/logger";
import $ from "cheerio";
import { BrowserView, ipcMain } from "electron";
import { WebContentsView, ipcMain } from "electron";

const logger = log.scope("providers/ted-provider");

export class TedProvider {
scrape = async (url: string) => {
return new Promise<string>((resolve, reject) => {
const view = new BrowserView();
return new Promise<string>((resolve, _reject) => {
const view = new WebContentsView();
view.webContents.loadURL(url);
logger.debug("started scraping", url);

Expand All @@ -17,21 +17,23 @@ export class TedProvider {
.executeJavaScript(`document.documentElement.innerHTML`)
.then((html) => resolve(html as string))
.finally(() => {
(view.webContents as any).destroy();
view.webContents.close();
});
});
view.webContents.on(
"did-fail-load",
(_event, _errorCode, error, validatedURL) => {
logger.error("failed scraping", url, error, validatedURL);
(view.webContents as any).destroy();
reject();
logger.warn("Failed to scrape", url, error, validatedURL);
view.webContents.close();
resolve("");
}
);
});
};

extractTalks = async (html: string) => {
if (!html) return [];

try {
const json = $.load(html)("#__NEXT_DATA__").text();
const data = JSON.parse(json);
Expand All @@ -43,6 +45,8 @@ export class TedProvider {
};

extractIdeas = async (html: string) => {
if (!html) return [];

const ideas: TedIdeaType[] = [];
$.load(html)(".post.block").each((_, el) => {
const url = $(el).find("a.block-post-link").attr("href");
Expand Down Expand Up @@ -72,7 +76,7 @@ export class TedProvider {
}

return new Promise<{ audio: string; video: string }>((resolve, reject) => {
const view = new BrowserView();
const view = new WebContentsView();
view.webContents.loadURL(url);
logger.debug("started loading", url);

Expand Down Expand Up @@ -102,15 +106,15 @@ export class TedProvider {
reject();
})
.finally(() => {
(view.webContents as any).destroy();
view.webContents.close();
});
});
view.webContents.on(
"did-fail-load",
(_event, _errorCode, error, validatedURL) => {
logger.error("failed loading", url, error, validatedURL);
(view.webContents as any).destroy();
reject();
view.webContents.close();
reject(error);
}
);
});
Expand All @@ -128,27 +132,15 @@ export class TedProvider {

registerIpcHandlers = () => {
ipcMain.handle("ted-provider-talks", async () => {
try {
return await this.talks();
} catch (error) {
logger.error(error);
}
return await this.talks();
});

ipcMain.handle("ted-provider-download-talk", async (_, url) => {
try {
return await this.downloadTalk(url);
} catch (error) {
logger.error(error);
}
return await this.downloadTalk(url);
});

ipcMain.handle("ted-provider-ideas", async () => {
try {
return await this.ideas();
} catch (error) {
logger.error(error);
}
return await this.ideas();
});
};
}
10 changes: 5 additions & 5 deletions enjoy/src/main/providers/youtube-provider.ts
@@ -1,13 +1,13 @@
import log from "@main/logger";
import $ from "cheerio";
import { BrowserView, ipcMain } from "electron";
import { WebContentsView, ipcMain } from "electron";

const logger = log.scope("providers/youtube-provider");

export class YoutubeProvider {
scrape = async (url: string) => {
return new Promise<string>((resolve, reject) => {
const view = new BrowserView();
const view = new WebContentsView();
view.webContents.loadURL(url);
logger.debug("started scraping", url);

Expand All @@ -17,14 +17,14 @@ export class YoutubeProvider {
.executeJavaScript(`document.documentElement.innerHTML`)
.then((html) => resolve(html as string))
.finally(() => {
(view.webContents as any).destroy();
view.webContents.close();
});
});
view.webContents.on(
"did-fail-load",
(_event, _errorCode, error, validatedURL) => {
logger.error("failed scraping", url, error, validatedURL);
(view.webContents as any).destroy();
logger.warn("failed scraping", url, error, validatedURL);
view.webContents.close();
reject();
}
);
Expand Down
54 changes: 16 additions & 38 deletions enjoy/src/main/window.ts
@@ -1,7 +1,7 @@
import {
app,
BrowserWindow,
BrowserView,
WebContentsView,
Menu,
ipcMain,
shell,
Expand Down Expand Up @@ -145,22 +145,16 @@ main.init = () => {
);

logger.debug("view-load", url);
const view = new BrowserView();
view.setBackgroundColor("#fff");
mainWindow.setBrowserView(view);
const view = new WebContentsView();
mainWindow.contentView.addChildView(view);

view.setBounds({
x: Math.round(x),
y: Math.round(y),
width: Math.round(width),
height: Math.round(height),
});
view.setAutoResize({
width: true,
height: true,
horizontal: true,
vertical: true,
});

view.webContents.on("did-navigate", (_event, url) => {
event.sender.send("view-on-state", {
state: "did-navigate",
Expand All @@ -176,7 +170,7 @@ main.init = () => {
url: validatedURL,
});
(view.webContents as any).destroy();
mainWindow.removeBrowserView(view);
mainWindow.contentView.removeChildView(view);
}
);
view.webContents.on("did-finish-load", () => {
Expand Down Expand Up @@ -221,27 +215,17 @@ main.init = () => {

ipcMain.handle("view-remove", () => {
logger.debug("view-remove");
mainWindow.getBrowserViews().forEach((view) => {
(view.webContents as any).destroy();
mainWindow.removeBrowserView(view);
mainWindow.contentView.children.forEach((view) => {
mainWindow.contentView.removeChildView(view);
});
});

ipcMain.handle("view-hide", () => {
logger.debug("view-hide");
const view = mainWindow.getBrowserView();
const view = mainWindow.contentView.children[0];
if (!view) return;

const bounds = view.getBounds();
logger.debug("current view bounds", bounds);
if (bounds.width === 0 && bounds.height === 0) return;

view.setBounds({
x: -Math.round(bounds.width),
y: -Math.round(bounds.height),
width: 0,
height: 0,
});
view.setVisible(false);
});

ipcMain.handle(
Expand All @@ -255,25 +239,19 @@ main.init = () => {
height: number;
}
) => {
const view = mainWindow.getBrowserView();
const view = mainWindow.contentView.children[0];
if (!view) return;
if (!bounds) return;

logger.debug("view-show", bounds);
const { x = 0, y = 0, width = 0, height = 0 } = bounds || {};
view.setBounds({
x: Math.round(x),
y: Math.round(y),
width: Math.round(width),
height: Math.round(height),
});
view.setVisible(true);
}
);

ipcMain.handle("view-scrape", (event, url) => {
logger.debug("view-scrape", url);
const view = new BrowserView();
mainWindow.setBrowserView(view);
const view = new WebContentsView();
view.setVisible(false);
mainWindow.contentView.addChildView(view);

view.webContents.on("did-navigate", (_event, url) => {
event.sender.send("view-on-state", {
Expand All @@ -290,7 +268,7 @@ main.init = () => {
url: validatedURL,
});
(view.webContents as any).destroy();
mainWindow.removeBrowserView(view);
mainWindow.contentView.removeChildView(view);
}
);
view.webContents.on("did-finish-load", () => {
Expand All @@ -302,7 +280,7 @@ main.init = () => {
html,
});
(view.webContents as any).destroy();
mainWindow.removeBrowserView(view);
mainWindow.contentView.removeChildView(view);
});
});

Expand Down

0 comments on commit 8afb942

Please sign in to comment.