Skip to content

Commit

Permalink
Updates (#10)
Browse files Browse the repository at this point in the history
* Set Active False for All Bookmarks

* active first tab only

* Fix Tabs

* Add Omnibox

* Add Omnibox
  • Loading branch information
smashedr authored May 13, 2024
1 parent ed7b2bd commit 4ddd808
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 53 deletions.
5 changes: 4 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Aviation Quick Search and Lookup Tools.",
"homepage_url": "https://github.com/cssnr/aviation-tools",
"author": "Shane",
"version": "0.1.2",
"version": "0.1.3",
"manifest_version": 3,
"commands": {
"_execute_action": {
Expand All @@ -13,6 +13,9 @@
"description": "Show Main Popup Action"
}
},
"omnibox": {
"keyword": "av"
},
"permissions": ["clipboardWrite", "contextMenus", "storage"],
"background": {
"type": "module"
Expand Down
11 changes: 7 additions & 4 deletions src/html/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<div class="container-fluid p-3">

<div class="btn-group w-100 mb-2" role="group" aria-label="Search Dropdown">
<button id="all-bookmarks" class="btn btn-success" type="button">Open All Bookmarks</button>
<button id="all-bookmarks" class="btn btn-success" type="button">
<i class="fa-solid fa-up-right-from-square me-2"></i> Open All Bookmarks</button>
<div class="btn-group btn-group-sm" role="group">
<button type="button" class="btn btn-outline-success dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Select</button>
<ul id="bookmarks" class="dropdown-menu">
Expand All @@ -24,6 +25,8 @@
</div>
</div>

<button type="submit" form="search-form" class="visually-hidden"></button>

<div class="btn-group w-100 mb-2 search-buttons" role="group" aria-label="Search Buttons">
<button type="submit" form="search-form" class="btn btn-outline-info" data-search="registration">Reg.</button>
<div class="btn-group btn-group-sm" role="group">
Expand All @@ -47,10 +50,9 @@
<div class="row mb-2">
<div class="col">
<label for="searchTerm" class="visually-hidden"></label>
<input id="searchTerm" class="form-control" type="text" placeholder="">
<input id="searchTerm" class="form-control" type="text" placeholder="" required>
</div>
</div>
<button type="submit" form="search-form" class="visually-hidden"></button>

<div class="row mb-2">
<div class="col text-center">
Expand Down Expand Up @@ -94,7 +96,8 @@
</div>
</form>

<a class="btn btn-primary w-100 mb-2" role="button" href="../html/options.html">Open Options</a>
<a class="btn btn-primary w-100 mb-2" role="button" href="../html/options.html">
<i class="fa-solid fa-sliders me-2"></i> Open Options</a>

<div class="d-flex justify-content-center align-items-center text-center small">
<img src="../images/logo16.png" width="16" height="16" alt="ASN Plus" title="ASN Plus">
Expand Down
16 changes: 8 additions & 8 deletions src/js/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ export async function openOptionsFor(category, searchTerm) {
} else if (category === 'registration') {
searchTerm = searchTerm.toUpperCase()
}
let resp = null
const { options } = await chrome.storage.sync.get(['options'])
if (!options) {
return null
}
let count = 0
for (const [key, value] of Object.entries(options[category])) {
console.log(`${key}: ${value}`)
// console.log(`${key}: ${value}`)
if (value) {
count += 1
const url = getLinkUrl(category, key, searchTerm)
console.log(`url: ${url}`)
await chrome.tabs.create({ active: true, url })
resp = searchTerm
}
}
return resp
if (!count) {
chrome.runtime.openOptionsPage()
}
return searchTerm
}

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function openAllBookmarks() {
}
for (const url of bookmarks) {
console.debug(`url: ${url}`)
await chrome.tabs.create({ active: true, url })
chrome.tabs.create({ active: true, url }).then()
}
window.close()
}
Expand Down
49 changes: 14 additions & 35 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ async function initPopup() {
console.debug('options, bookmarks:', options, bookmarks)
updateOptions(options)

console.debug(`options.searchType:`, options.searchType)
searchTerm.placeholder = options.searchType
document.querySelector(
`input[name="searchType"][value="${options.searchType}"]`
Expand All @@ -64,7 +63,6 @@ async function initPopup() {
}
}

console.debug('bookmarks:', bookmarks)
if (bookmarks?.length) {
document.getElementById('no-bookmarks').remove()
const ul = document.getElementById('bookmarks')
Expand Down Expand Up @@ -124,20 +122,16 @@ async function popupLinks(event) {
console.debug('popupLinks:', event)
event.preventDefault()
const anchor = event.target.closest('a')
console.debug(`anchor.href: ${anchor.href}`, anchor)
const href = anchor.getAttribute('href').replace(/^\.+/g, '')
console.debug('href:', href)
let url
if (anchor.href.endsWith('html/options.html')) {
if (href.endsWith('html/options.html')) {
chrome.runtime.openOptionsPage()
return window.close()
} else if (
anchor.href.startsWith('http') ||
anchor.href.startsWith('chrome-extension')
) {
// console.debug(`http or chrome-extension`)
url = anchor.href
} else if (href.startsWith('http')) {
url = href
} else {
// console.debug(`else chrome.runtime.getURL`)
url = chrome.runtime.getURL(anchor.href)
url = chrome.runtime.getURL(href)
}
console.log('url:', url)
await chrome.tabs.create({ active: true, url })
Expand Down Expand Up @@ -167,37 +161,22 @@ async function updateSearchType(event) {
async function searchFormSubmit(event) {
console.debug('searchFormSubmit:', event)
event.preventDefault()
// const searchTerm = document.getElementById('searchTerm')
console.debug(`searchTerm.value: ${searchTerm.value}`)
const { options } = await chrome.storage.sync.get(['options'])
let search
if (!searchTerm.value) {
console.debug('no searchTerm.value')
return searchTerm.focus()
}
if (event.target.classList.contains('dropdown-item')) {
let category = event.target.parentNode.parentNode.id
console.debug(`category: ${category}`)
let key = event.target.textContent
console.debug(`key: ${key}`)
const url = getLinkUrl(category, key, searchTerm.value)
console.debug(`url: ${url}`)
await chrome.tabs.create({ active: true, url })
return
} else if (event.submitter?.dataset?.search) {
search = event.submitter.dataset.search
const category = event.submitter.dataset.search
await openOptionsFor(category, searchTerm.value)
} else {
search = document.querySelector(
const category = document.querySelector(
'input[name="searchType"]:checked'
).value
await openOptionsFor(category, searchTerm.value)
}
console.debug(`search: ${search}`, options[search])
if (!searchTerm.value) {
console.debug('no searchTerm.value')
searchTerm.focus()
return
}
const resp = await openOptionsFor(search, searchTerm.value)
console.debug(`resp: ${resp}`)
if (!resp) {
console.debug(`no options set for: ${search}`)
chrome.runtime.openOptionsPage()
}
window.close()
}
106 changes: 101 additions & 5 deletions src/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ chrome.runtime.onStartup.addListener(onStartup)
chrome.runtime.onInstalled.addListener(onInstalled)
chrome.contextMenus.onClicked.addListener(onClicked)
chrome.storage.onChanged.addListener(onChanged)
chrome.omnibox.onInputChanged.addListener(onInputChanged)
chrome.omnibox.onInputCancelled.addListener(onInputCancelled)
chrome.omnibox.onInputEntered.addListener(onInputEntered)

const omniboxDefault = 'Aviation Tools - airport, flight, registration'

/**
* On Startup Callback
Expand Down Expand Up @@ -64,6 +69,9 @@ async function onInstalled(details) {
}
}
await chrome.runtime.setUninstallURL(`${githubURL}/issues`)
chrome.omnibox.setDefaultSuggestion({
description: omniboxDefault,
})
}

/**
Expand Down Expand Up @@ -95,9 +103,6 @@ async function onClicked(ctx, tab) {
} else {
console.debug('openOptionsFor')
const term = await openOptionsFor(ctx.menuItemId, ctx.selectionText)
if (!term) {
chrome.runtime.openOptionsPage()
}
await clipboardWrite(term)
}
}
Expand Down Expand Up @@ -134,6 +139,98 @@ async function onChanged(changes, namespace) {
}
}

async function parseInput(text) {
console.debug('parseInput:', text)
text = text.trim()
const split = text.split(' ')
const length = split.length
let command = split.shift().toLowerCase()
let search = split.join('')
if (length === 1) {
command = ''
search = text
}
console.debug('command:', command)
if (command.startsWith('r') && 'registration'.includes(command)) {
return ['registration', search]
} else if (command.startsWith('f') && 'flight'.includes(command)) {
return ['flight', search]
} else if (command.startsWith('a') && 'airport'.includes(command)) {
return ['airport', search]
} else {
search = text.replace(/ /g, '')
let { options } = await chrome.storage.sync.get(['options'])
return [options.searchType, search]
}
}

/**
* Omnibox Input Changed Callback
* @function onInputChanged
* @param {String} text
* @param {Function} suggest
*/
async function onInputChanged(text, suggest) {
console.debug('onInputChanged:', text, suggest)
text = text.trim()
const split = text.split(' ')
// console.debug('split:', split)
if (split.length) {
let command = split.shift().toLowerCase()
// console.debug('command:', command)
let search = split.join('')
console.debug('search:', search)
if (command.startsWith('r') && 'registration'.includes(command)) {
chrome.omnibox.setDefaultSuggestion({
description: 'Aviation Tools - Registration Search',
})
} else if (command.startsWith('f') && 'flight'.includes(command)) {
chrome.omnibox.setDefaultSuggestion({
description: 'Aviation Tools - Flight Search',
})
} else if (command.startsWith('a') && 'airport'.includes(command)) {
chrome.omnibox.setDefaultSuggestion({
description: 'Aviation Tools - Airport Search',
})
} else {
let { options } = await chrome.storage.sync.get(['options'])
// search = text.replace(/\s/g, '')
const type =
options.searchType.charAt(0).toUpperCase() +
options.searchType.slice(1)
chrome.omnibox.setDefaultSuggestion({
description: `Aviation Tools - ${type} Search`,
})
}
}
}

/**
* Omnibox Input Cancelled Callback
* @function onInputCancelled
*/
async function onInputCancelled() {
console.debug('onInputCancelled')
chrome.omnibox.setDefaultSuggestion({
description: omniboxDefault,
})
}

/**
* Omnibox Input Entered Callback
* @function onInputEntered
* @param {String} text
*/
async function onInputEntered(text) {
console.debug('onInputEntered:', text)
text = text.trim()
// console.debug('text:', text)
let [type, search] = await parseInput(text)
console.debug('type:', type)
console.debug('search:', search)
openOptionsFor(type, search)
}

/**
* Create Context Menus
* @function createContextMenus
Expand All @@ -160,7 +257,7 @@ export function createContextMenus(bookmarks) {
type: context[2],
})
})
if (bookmarks) {
if (bookmarks.length) {
chrome.contextMenus.create({
contexts: ctx,
id: `bookmark-all`,
Expand All @@ -171,7 +268,6 @@ export function createContextMenus(bookmarks) {
contexts: ctx,
id: `bookmark-sep`,
parentId: 'bookmarks',
title: 'Open All Bookmarks',
type: 'separator',
})
bookmarks.forEach((url, i) => {
Expand Down

0 comments on commit 4ddd808

Please sign in to comment.