Skip to content

Commit 311a592

Browse files
committed
[frontend] more robust navigate
1 parent 70bc1e9 commit 311a592

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

frontend/src/browser.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { createMenu } from "./components/Menu";
99
import { StatefulClass } from "./StatefulClass";
1010
import { Tab, type SerializedTab } from "./Tab";
1111
import { createDelegate } from "dreamland/core";
12+
import tlds from "tlds";
1213

1314
export const pushTab = createDelegate<Tab>();
1415
export const popTab = createDelegate<Tab>();
@@ -101,6 +102,31 @@ export class Browser extends StatefulClass {
101102
popTab(tab);
102103
}
103104

105+
searchNavigate(url: string) {
106+
function validTld(hostname: string) {
107+
for (const tld of tlds) {
108+
if (hostname.endsWith("." + tld)) {
109+
return true;
110+
}
111+
}
112+
return false;
113+
}
114+
115+
// TODO: dejank
116+
if (URL.canParse(url)) {
117+
this.activetab.pushNavigate(new URL(url));
118+
} else if (
119+
URL.canParse("https://" + url) &&
120+
validTld(new URL("https://" + url).hostname)
121+
) {
122+
let fullurl = new URL("https://" + url);
123+
this.activetab.pushNavigate(fullurl);
124+
} else {
125+
const search = `https://google.com/search?q=${encodeURIComponent(url)}`;
126+
this.activetab.pushNavigate(new URL(search));
127+
}
128+
}
129+
104130
build(): HTMLElement {
105131
let shell = <Shell tabs={use(this.tabs)} activetab={use(this.activetab)} />;
106132

@@ -137,15 +163,6 @@ export class Browser extends StatefulClass {
137163
refresh={() => {
138164
this.activetab.frame.reload();
139165
}}
140-
navigate={(url: string) => {
141-
// TODO: dejank
142-
if (URL.canParse(url)) {
143-
this.activetab.pushNavigate(new URL(url));
144-
} else {
145-
const search = `https://google.com/search?q=${encodeURIComponent(url)}`;
146-
this.activetab.pushNavigate(new URL(search));
147-
}
148-
}}
149166
/>
150167
{shell}
151168
</div>

frontend/src/components/Omnibox.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Spacer.style = css`
2424
export const UrlInput: Component<
2525
{
2626
tabUrl: URL;
27-
navigate: (url: string) => void;
2827
selectContent: Delegate<void>;
2928
},
3029
{
@@ -112,7 +111,7 @@ export const UrlInput: Component<
112111
this.active = false;
113112
this.input.blur();
114113

115-
this.navigate(this.value);
114+
browser.searchNavigate(this.value);
116115
}}
117116
class:focused={use(this.focusindex).map(
118117
(i) => i - 1 === this.overflowItems.indexOf(item)
@@ -249,7 +248,6 @@ UrlInput.style = css`
249248

250249
export const Omnibox: Component<{
251250
tabUrl: URL;
252-
navigate: (url: string) => void;
253251
goBack: () => void;
254252
goForwards: () => void;
255253
refresh: () => void;
@@ -284,7 +282,6 @@ export const Omnibox: Component<{
284282
<UrlInput
285283
selectContent={selectContent}
286284
tabUrl={use(this.tabUrl)}
287-
navigate={this.navigate}
288285
></UrlInput>
289286
<Spacer></Spacer>
290287
<IconButton icon={iconExtension}></IconButton>

frontend/src/components/Shell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const Shell: Component<{
7272
) as HTMLElement;
7373
if (!container) throw new Error(`No container found for tab ${tab.id}`);
7474

75-
tab.screenshot = URL.createObjectURL(await toBlob(container));
75+
// tab.screenshot = URL.createObjectURL(await toBlob(container));
7676
});
7777

7878
return <div></div>;

frontend/src/pages/NewTab.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { css, type Component } from "dreamland/core";
22
import type { Tab } from "../Tab";
3+
import { browser } from "../main";
34

45
export const NewTab: Component<
56
{
@@ -15,7 +16,7 @@ export const NewTab: Component<
1516
on:keydown={(e: KeyboardEvent) => {
1617
if (e.key === "Enter") {
1718
e.preventDefault();
18-
this.tab.history.push(new URL(e.target!.value));
19+
browser.searchNavigate(e.target!.value);
1920
}
2021
}}
2122
placeholder="Search Google or type A URL"

0 commit comments

Comments
 (0)