From 7981890569e455cc35c56e3d425499cdc41c988a Mon Sep 17 00:00:00 2001 From: hitesh saini Date: Sun, 25 Aug 2019 13:15:45 +0530 Subject: [PATCH] Omnibox Integration (#1) * Integration Omnibox * Suggested changes | Omnibox integration * Suggested changes | Omnibox integration * bump version 0.0.3 --- manifest.json | 7 +++++-- src/background.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/manifest.json b/manifest.json index f67df2a..fee8cce 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "Short", "short_name": "Short Ext", - "version": "0.0.2", + "version": "0.0.3", "description": "Short allows you to visit short links using prefix s/ instead of https://s.time4hacks.com/r/", "manifest_version": 2, "background": { @@ -15,9 +15,12 @@ "webRequest", "webRequestBlocking" ], + "omnibox": { + "keyword" : "short" + }, "icons": { "16": "icons/logo-16.png", "48": "icons/logo-48.png", "128": "icons/logo-128.png" } -} \ No newline at end of file +} diff --git a/src/background.ts b/src/background.ts index efa4c3a..043d121 100644 --- a/src/background.ts +++ b/src/background.ts @@ -23,8 +23,33 @@ function extractAlias(shortLink: string): string { return shortLink.substring(aliasStartIdx); } +function openTab(url: string) { + chrome + .tabs + .create({ + url: url + }); +} + class ShortExt { constructor(private apiBaseUrl: string, private webUi: string) { + this.setupOmnibox(); + } + + fullURL = (alias: string) => { + // Escape user input for special characters , / ? : @ & = + $ # + let escapedAlias = encodeURIComponent(alias); + return `${this.apiBaseUrl}${escapedAlias}`; + } + + setupOmnibox = () => { + chrome + .omnibox + .onInputEntered + .addListener((alias: string) => { + let url = this.fullURL(alias); + openTab(url); + }); } redirect = (details: Details): BlockingResponse => { @@ -72,4 +97,5 @@ const webUi = 'https://s.time4hacks.com'; const apiBaseUrl = `${webUi}/r/`; const ext = new ShortExt(apiBaseUrl, webUi); -ext.launch(); \ No newline at end of file + +ext.launch();