Skip to content

Commit

Permalink
Removed option to use bookmark favicons because it's too slow
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Oct 13, 2019
1 parent bd23b21 commit a188d9f
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 218 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@
"package:publish": "./node_modules/.bin/electron-builder --config electron-builder-config.yml --publish onTag"
},
"devDependencies": {
"@types/cheerio": "^0.22.13",
"@types/color": "^3.0.0",
"@types/jest": "^24.0.17",
"@types/mathjs": "^5.0.1",
"@types/node-powershell": "^3.1.0",
"@types/vue-color": "^2.4.2",
"axios": "^0.19.0",
"cheerio": "^1.0.0-rc.3",
"color": "^3.1.2",
"electron": "^5.0.9",
"electron-builder": "^21.2.0",
Expand Down
2 changes: 0 additions & 2 deletions src/common/config/browser-bookmarks-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { Browser } from "../../main/plugins/browser-bookmarks-plugin/browser";
export interface BrowserBookmarksOptions {
isEnabled: boolean;
browser: Browser;
useFavicons: boolean;
}

export const defaultBrowserBookmarksOptions: BrowserBookmarksOptions = {
browser: Browser.GoogleChrome,
isEnabled: true,
useFavicons: false,
};
1 change: 1 addition & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ function updateMainWindowSize(searchResultCount: number, appearanceOptions: Appe
function reloadApp() {
updateMainWindowSize(0, config.appearanceOptions);
searchEngine = getProductionSearchEngine(config, translationSet, logger);
refreshAllIndexes();
mainWindow.reload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export interface BrowserBookmarkRepository {
browser: Browser;
defaultIcon: Icon;
getBrowserBookmarks(): Promise<BrowserBookmark[]>;
updateUseFaviconOption(useNativeIcons: boolean): void;
}
3 changes: 0 additions & 3 deletions src/main/plugins/browser-bookmarks-plugin/browser-bookmark.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Icon } from "../../../common/icon/icon";

export interface BrowserBookmark {
name: string;
url: string;
icon: Icon;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,8 @@ export class BrowserBookmarksPlugin implements SearchPlugin {
this.translations = translationSet;

const browserChanged = updatedConfig.browserBookmarksOptions.browser !== this.config.browser;
const useFaviconsOptionChanged = updatedConfig.browserBookmarksOptions.useFavicons !== this.config.useFavicons;

if (useFaviconsOptionChanged) {
this.browserBookmarkRepositories.forEach((browserBookmarkRepository) => {
browserBookmarkRepository.updateUseFaviconOption(updatedConfig.browserBookmarksOptions.useFavicons);
});
}

if (useFaviconsOptionChanged || browserChanged) {
if (browserChanged) {
this.config = updatedConfig.browserBookmarksOptions;
this.refreshIndex()
.then(() => resolve())
Expand Down Expand Up @@ -106,7 +99,7 @@ export class BrowserBookmarksPlugin implements SearchPlugin {
: `${this.config.browser} ${this.translations.browserBookmark}`,
executionArgument: browserBookmark.url,
hideMainWindowAfterExecution: true,
icon: browserBookmark.icon,
icon: this.getMatchingBrowserBookmarkRepository().defaultIcon,
name: browserBookmark.name || browserBookmark.url,
needsUserConfirmationBeforeExecution: false,
originPluginType: this.pluginType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { BrowserBookmark } from "./browser-bookmark";
import { FileHelpers } from "../../../common/helpers/file-helpers";
import { isValidUrl } from "../../../common/helpers/url-helpers";
import { Browser } from "./browser";
import axios from "axios";
import * as cheerio from "cheerio";
import { IconType } from "../../../common/icon/icon-type";
import { Icon } from "../../../common/icon/icon";

Expand All @@ -16,11 +14,9 @@ export class GoogleChromeBookmarkRepository implements BrowserBookmarkRepository
};

private readonly bookmarkFilePath: string;
private useFavicons: boolean;

constructor(bookmarkFilePath: string, useFavicons: boolean) {
constructor(bookmarkFilePath: string) {
this.bookmarkFilePath = bookmarkFilePath;
this.useFavicons = useFavicons;
}

public getBrowserBookmarks(): Promise<BrowserBookmark[]> {
Expand All @@ -32,11 +28,7 @@ export class GoogleChromeBookmarkRepository implements BrowserBookmarkRepository
.then((data) => {
const jsonParsed = JSON.parse(data);
const bookmarks: BrowserBookmark[] = this.getBookmarksFromObject(jsonParsed.roots.bookmark_bar);
this.getIcons(bookmarks)
.then((result) => {
resolve(result);
})
.catch((err) => reject(err));
resolve(bookmarks);
})
.catch((err) => reject(`Can't read bookmarks file: ${err}`));
} else {
Expand All @@ -47,10 +39,6 @@ export class GoogleChromeBookmarkRepository implements BrowserBookmarkRepository
});
}

public updateUseFaviconOption(useFavicons: boolean) {
this.useFavicons = useFavicons;
}

private getBookmarksFromObject(data: any): any[] {
let result: any[] = [];

Expand All @@ -76,65 +64,4 @@ export class GoogleChromeBookmarkRepository implements BrowserBookmarkRepository

return result;
}

private getIcons(bookmarks: BrowserBookmark[]): Promise<BrowserBookmark[]> {
return new Promise((resolve, reject) => {
if (bookmarks.length === 0) {
resolve([]);
} else {
const promises = bookmarks.map((bookmark) => this.getIcon(bookmark));
Promise.all(promises)
.then((result) => {
resolve(result);
})
.catch((err) => reject(err));
}
});
}

private getIcon(bookmark: BrowserBookmark): Promise<BrowserBookmark> {
return new Promise((resolve) => {
const handleResult = (icon?: Icon) => {
if (icon) {
bookmark.icon = icon;
} else {
bookmark.icon = this.defaultIcon;
}
resolve(bookmark);
};

if (this.useFavicons) {
axios.get(bookmark.url)
.then((data) => {
const $ = cheerio.load(data.data);
const favicons = $(`link[rel="icon"]`).toArray();
if (favicons.length > 0) {
if (favicons[0].attribs && favicons[0].attribs.href) {
handleResult({
parameter: this.getFaviconUrl(favicons[0].attribs.href, bookmark),
type: IconType.URL,
});
} else {
handleResult();
}
} else {
handleResult();
}
})
.catch(() => handleResult());
} else {
handleResult();
}
});
}

private getFaviconUrl(faviconUrl: string, bookmark: BrowserBookmark): string {
if (!faviconUrl.startsWith("http://") &&
!faviconUrl.startsWith("https://") &&
!faviconUrl.startsWith("www.")) {
const slash = faviconUrl.startsWith("/") ? "" : "/";
faviconUrl = `http://${new URL(bookmark.url).host}${slash}${faviconUrl}`;
}
return faviconUrl;
}
}
2 changes: 1 addition & 1 deletion src/main/production/production-search-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function getProductionSearchEngine(config: UserConfigOptions, translation
new BrowserBookmarksPlugin(
config.browserBookmarksOptions,
translationSet,
[new GoogleChromeBookmarkRepository(chromeBookmarksFilePath, config.browserBookmarksOptions.useFavicons)],
[new GoogleChromeBookmarkRepository(chromeBookmarksFilePath)],
urlExecutor,
),
];
Expand Down
Loading

0 comments on commit a188d9f

Please sign in to comment.