Skip to content

Commit

Permalink
Add Omnibox (#15)
Browse files Browse the repository at this point in the history
* Add Omnibox

* Bump Version

* Add Test Audio Link
  • Loading branch information
smashedr authored May 14, 2024
1 parent e9cffcf commit 8b33eb7
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 7 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 Safety Network Plus - Additional Features, Display and Search Options.",
"homepage_url": "https://asn-plus.cssnr.com/",
"author": "Shane",
"version": "0.1.5",
"version": "0.1.6",
"manifest_version": 3,
"commands": {
"_execute_action": {
Expand All @@ -19,6 +19,9 @@
"description": "Open ASN Home Page"
}
},
"omnibox": {
"keyword": "asn"
},
"permissions": ["contextMenus", "scripting", "storage"],
"host_permissions": ["*://aviation-safety.net/*"],
"content_scripts": [
Expand Down
2 changes: 1 addition & 1 deletion src/html/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ <h1>ASN Plus</h1>
<option value="">Browser Default</option>
</select>
<div class="form-text" id="speechVoiceHelp">
Voice for Speech Playback.
Voice for Speech Playback, <a id="test-voice" class="align-bottom" role="button">Test</a>.
</div>
</div>
<div class="col-sm-4 col-12 mb-2">
Expand Down
4 changes: 2 additions & 2 deletions src/js/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { countryList } from './vars.js'
/**
* Request Host Permissions
* @function getSearchURL
* @param {String} value
* @param {String} type
* @param {String} value
* @return {String}
*/
export function getSearchURL(value, type) {
export function getSearchURL(type, value) {
value = value.trim()
if (type === 'registration') {
return `https://aviation-safety.net/wikibase/dblist2.php?yr=&at=&re=${value}&pc=&op=&lo=&co=&ph=&na=&submit=Submit`
Expand Down
37 changes: 37 additions & 0 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ chrome.permissions.onAdded.addListener(onAdded)
document.addEventListener('DOMContentLoaded', initOptions)
document.getElementById('grant-perms').addEventListener('click', grantPerms)
document.getElementById('reset-country').addEventListener('click', resetCountry)
document.getElementById('test-voice').addEventListener('click', testVoice)
document
.querySelectorAll('#options-form input,select')
.forEach((el) => el.addEventListener('change', saveOptions))
Expand Down Expand Up @@ -103,6 +104,42 @@ async function resetCountry(event) {
showToast('Country Display and Code Reset.')
}

/**
* Test Voice Click Callback
* @function testVoice
* @param {InputEvent} event
*/
async function testVoice(event) {
console.log('testVoice:', event)
event.preventDefault()
const { options } = await chrome.storage.sync.get(['options'])
const utterance = getUtterance('Voice for Speech Playback.', options)
speechSynthesis.speak(utterance)
}

/**
* Get Utterance
* @param {String} text
* @param {Object} options
* @return {SpeechSynthesisUtterance}
*/
function getUtterance(text, options) {
const utterance = new SpeechSynthesisUtterance(text)
if (options.speechRate) {
utterance.rate = options.speechRate
}
if (options.speechVoice) {
speechSynthesis.getVoices().forEach((voice) => {
// console.log('voice:', voice)
if (voice.name === options.speechVoice) {
// console.debug('voice set:', voice)
utterance.voice = voice
}
})
}
return utterance
}

/**
* On Changed Callback
* @function onChanged
Expand Down
2 changes: 1 addition & 1 deletion src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ async function searchFormSubmit(event) {
if (!value) {
return searchForm.elements.searchTerm.focus()
}
const url = getSearchURL(value, searchType)
const url = getSearchURL(searchType, value)
console.log(`url: ${url}`)
await chrome.tabs.create({ active: true, url })
window.close()
Expand Down
101 changes: 99 additions & 2 deletions src/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ chrome.runtime.onMessage.addListener(onMessage)
chrome.contextMenus.onClicked.addListener(onClicked)
chrome.commands.onCommand.addListener(onCommand)
chrome.storage.onChanged.addListener(onChanged)
chrome.omnibox.onInputChanged.addListener(onInputChanged)
chrome.omnibox.onInputCancelled.addListener(onInputCancelled)
chrome.omnibox.onInputEntered.addListener(onInputEntered)

const omniboxDefault = 'ASN - registration OR operator Search'
const asnHomePageURL = 'https://aviation-safety.net/'
const uninstallURL = 'https://asn-plus.cssnr.com/uninstall/'

Expand Down Expand Up @@ -135,7 +139,7 @@ async function onClicked(ctx, tab) {
await activateOrOpen(asnHomePageURL)
} else if (['registration', 'operator'].includes(ctx.menuItemId)) {
console.debug(`${ctx.menuItemId}: ${ctx.selectionText}`)
const url = getSearchURL(ctx.selectionText, ctx.menuItemId)
const url = getSearchURL(ctx.menuItemId, ctx.selectionText)
console.log('url:', url)
await chrome.tabs.create({ active: true, url })
} else {
Expand Down Expand Up @@ -191,6 +195,99 @@ 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('o') && 'operator'.includes(command)) {
return ['operator', 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: 'ASN - Registration Search',
})
} else if (command.startsWith('o') && 'operator'.includes(command)) {
chrome.omnibox.setDefaultSuggestion({
description: 'ASN - Operator 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)
let url
if (!search) {
url = 'https://aviation-safety.net/wikibase/wikisearch.php'
} else {
url = getSearchURL(type, search)
}
console.log('url:', url)
await chrome.tabs.create({ active: true, url })
}

/**
* Register Dark Mode Content Script, yea its that hard
* @function registerDarkMode
Expand Down Expand Up @@ -224,8 +321,8 @@ function createContextMenus() {
console.debug('createContextMenus')
chrome.contextMenus.removeAll()
const contexts = [
[['selection'], 'operator', 'normal', 'Operator Search'],
[['selection'], 'registration', 'normal', 'Registration Search'],
[['selection'], 'operator', 'normal', 'Operator Search'],
[['selection'], 'separator-1', 'separator', 'separator'],
[['selection'], 'openHome', 'normal', 'ASN Home Page'],
[['selection'], 'options', 'normal', 'Open Options'],
Expand Down

0 comments on commit 8b33eb7

Please sign in to comment.