Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
smashedr committed Oct 25, 2024
1 parent 958ccd2 commit ba3386c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 54 deletions.
17 changes: 9 additions & 8 deletions src/js/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function getLinkUrl(subkey, key, value) {
// if (subkey === 'flight') {
// value = value.toLowerCase().replace(/[\s-]+/g, '')
// }
console.log(`${subkey}: ${key}: ${value}`)
console.log(`getLinkUrl: ${subkey}: ${key}: ${value}`)
const link = searchLinks[subkey][key] + value.trim()
console.log(`link: ${link}`)
return link
Expand All @@ -88,16 +88,17 @@ export function getLinkUrl(subkey, key, value) {
* @function openAllBookmarks
*/
export async function openAllBookmarks() {
console.debug('openAllBookmarks')
// console.debug('openAllBookmarks')
const { bookmarks } = await chrome.storage.sync.get(['bookmarks'])
console.debug(bookmarks)
// console.debug(bookmarks)
if (!bookmarks?.length) {
chrome.runtime.openOptionsPage()
}
for (const url of bookmarks) {
try {
console.debug(`url: ${url}`)
chrome.tabs.create({ active: true, url }).then()
console.debug(`tabs.create: url: ${url}`)
// noinspection ES6MissingAwait
chrome.tabs.create({ active: true, url })
} catch (e) {
console.error(e)
}
Expand Down Expand Up @@ -139,7 +140,7 @@ export async function saveOptions(event) {
if (key.includes('-')) {
const subkey = key.split('-')[1]
key = key.split('-')[0]
console.log(`%cSet: ${key}.${subkey}:`, 'color: DeepSkyBlue', value)
console.log(`%c Set: ${key}.${subkey}:`, 'color: Khaki', value)
options[key][subkey] = value
} else if (value !== undefined) {
console.log(`Set %c${key}:`, 'color: Khaki', value)
Expand All @@ -166,7 +167,7 @@ export function updateOptions(options) {
key = value // NOSONAR
value = true // NOSONAR
}
console.debug(`${key}:`, value)
// console.debug(`key: ${key}:`, value)
const el = document.getElementById(key)
// Handle Object Subkeys
if (typeof value === 'object') {
Expand All @@ -180,7 +181,7 @@ export function updateOptions(options) {
continue
}
if (!el) {
console.debug('element not found for key:', key)
// console.debug('element not found for key:', key)
continue
}
if (!['INPUT', 'SELECT'].includes(el.tagName)) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (typeof ClipboardJS !== 'undefined') {
}
})
clipboard.on('error', function (event) {
console.debug('clipboard.error:', event)
console.warn('clipboard.error:', event)
showToast('Clipboard Copy Failed', 'warning')
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/js/metar.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ function processMetar(event) {
}
console.log('metar:', metar)
if (!metar || typeof metar !== 'object') {
return console.info('no metar')
return console.log('%c No metar', 'color: Yellow')
}
for (const [key, value] of Object.entries(metar)) {
console.debug(`${key}:`, value)
console.debug(`key: ${key}:`, value)
if (key === 'clouds') {
processClouds(value)
} else if (typeof value === 'object') {
Expand Down
38 changes: 15 additions & 23 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bookmarksInput.addEventListener('change', inputBookmarks)
* @function initOptions
*/
async function initOptions() {
console.log('initOptions')
console.debug('initOptions')
// noinspection ES6MissingAwait
hideSections()
// noinspection ES6MissingAwait
Expand All @@ -68,16 +68,13 @@ async function initOptions() {
}

async function hideSections() {
console.debug('hideSections')
// const storage = localStorage.getItem('sections') || '[]'
// console.debug('storage:', storage)
// console.debug('hideSections')
const sections = JSON.parse(localStorage.getItem('sections') || '[]')
console.debug('sections:', sections)
// console.debug('sections:', sections)
for (const section of sections) {
console.debug('section:', section)
// console.debug('section:', section)
const el = document.getElementById(section)
// console.debug('el:', el)
// el.classList.add('d-none')
el.style.display = 'none'
document.querySelector(`[data-section="${section}"]`).textContent =
'show'
Expand All @@ -91,17 +88,15 @@ function hideShowAll(event) {
const sections = document.querySelectorAll('section')
const storage = []
for (const section of sections) {
console.debug('section:', section)
// console.debug('section:', section)
if (action === 'expand') {
console.debug('%c SHOW Section', 'color: Lime')
// section.style.display = ''
// console.debug('%c SHOW Section', 'color: Lime')
$(section).show('fast')
document.querySelector(
`[data-section="${section.id}"]`
).textContent = 'hide'
} else {
console.debug('%c HIDE Section', 'color: OrangeRed')
// section.style.display = 'none'
// console.debug('%c HIDE Section', 'color: OrangeRed')
$(section).hide('fast')
storage.push(section.id)
document.querySelector(
Expand All @@ -110,10 +105,10 @@ function hideShowAll(event) {
}
}
if (action === 'expand') {
console.debug('storage:', '[]')
// console.debug('storage:', '[]')
localStorage.setItem('sections', '[]')
} else {
console.debug('storage:', storage)
// console.debug('storage:', storage)
localStorage.setItem('sections', JSON.stringify(storage))
}
}
Expand All @@ -125,26 +120,24 @@ function hideShowSection(event) {
const el = document.getElementById(section)
// console.debug('el:', el)
const sections = JSON.parse(localStorage.getItem('sections') || '[]')
console.debug('sections:', sections)
// console.debug('sections:', sections)
const shown = !sections.includes(section)
// console.debug('shown:', shown)
if (shown) {
console.debug('%c HIDE Section', 'color: OrangeRed')
// el.classList.add('d-none')
$(el).hide('fast')
sections.push(section)
document.querySelector(`[data-section="${section}"]`).textContent =
'show'
} else {
console.debug('%c SHOW Section', 'color: Lime')
// el.classList.remove('d-none')
$(el).show('fast')
const idx = sections.indexOf(section)
sections.splice(idx, 1)
document.querySelector(`[data-section="${section}"]`).textContent =
'hide'
}
console.debug('sections:', sections)
// console.debug('sections:', sections)
localStorage.setItem('sections', JSON.stringify(sections))
}

Expand All @@ -156,7 +149,7 @@ async function checkInstall() {
history.pushState(null, '', location.href.split('?')[0])
const userSettings = await chrome.action.getUserSettings()
if (userSettings.isOnToolbar) {
return console.log('%cToolbar Icon Already Pinned!', 'color: Aqua')
return console.log('%c Toolbar Icon Already Pinned!', 'color: Aqua')
}
const pin = document.getElementById('pin-notice')
pin.addEventListener('click', pinClick)
Expand Down Expand Up @@ -267,10 +260,9 @@ async function addBookmark(event) {
try {
url = new URL(value)
} catch (e) {
console.debug(e)
showToast('You must provide a valid URL.', 'danger')
input.focus()
return
console.log(e)
showToast(`Error: ${e.message}`, 'danger')
return input.focus()
}
const { bookmarks } = await chrome.storage.sync.get(['bookmarks'])
if (!bookmarks.includes(url.href)) {
Expand Down
7 changes: 4 additions & 3 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function initPopup() {
updateManifest()

const [tab] = await chrome.tabs.query({ currentWindow: true, active: true })
console.debug(`tab: ${tab.id}`, tab)
// console.debug(`tab: ${tab.id}`, tab)
console.debug('tab.url:', tab.url)

chrome.storage.sync.get(['options']).then((items) => {
Expand Down Expand Up @@ -78,7 +78,7 @@ async function initPopup() {
// console.debug(`${key}: ${value}`)
const ul = document.getElementById(key)
if (!ul) {
console.debug('skipping key:', key)
// console.debug('skipping key:', key)
continue
}
for (const [name, url] of Object.entries(value)) {
Expand Down Expand Up @@ -237,7 +237,7 @@ async function bookmarkToggle(event) {
const { bookmarks } = await chrome.storage.sync.get(['bookmarks'])
console.debug('bookmarks:', bookmarks)
const [tab] = await chrome.tabs.query({ currentWindow: true, active: true })
console.debug(`tab: ${tab.id}`, tab)
// console.debug(`tab: ${tab.id}`, tab)
console.debug('tab.url:', tab.url)
if (!bookmarks.includes(tab.url)) {
bookmarks.push(tab.url)
Expand All @@ -254,6 +254,7 @@ async function bookmarkToggle(event) {
)
bookmarkCurrent.textContent = 'Add'
}
// console.debug('bookmarks:', bookmarks)
updateBookmarks(bookmarks)
await chrome.storage.sync.set({ bookmarks })
// initPopup()
Expand Down
46 changes: 29 additions & 17 deletions src/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
clipboardWrite,
openAllBookmarks,
openOptionsFor,
updateOptions,

Check failure on line 9 in src/js/service-worker.js

View workflow job for this annotation

GitHub Actions / Test

'updateOptions' is defined but never used
} from './exports.js'

chrome.runtime.onStartup.addListener(onStartup)
Expand Down Expand Up @@ -62,8 +63,8 @@ async function onInstalled(details) {
}
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
// chrome.runtime.openOptionsPage()
let url = chrome.runtime.getURL('/html/options.html') + '?install=new'
console.log(`url: ${url}`)
const url = chrome.runtime.getURL('/html/options.html') + '?install=new'
console.log(`INSTALL url: ${url}`)
await chrome.tabs.create({ active: true, url })
} else if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
if (options.showUpdate) {
Expand All @@ -88,7 +89,7 @@ async function onInstalled(details) {
*/
async function onClicked(ctx, tab) {
console.debug('onClicked:', ctx, tab)
console.log(`ctx.menuItemId: ${ctx.menuItemId}`)
console.log(`ctx.menuItemId: %c${ctx.menuItemId}`, 'color: HotPink')
if (ctx.menuItemId === 'options') {
console.debug('options')
chrome.runtime.openOptionsPage()
Expand Down Expand Up @@ -130,7 +131,6 @@ async function onClicked(ctx, tab) {
url.searchParams.append('metar', ctx.selectionText)
await chrome.tabs.create({ active: true, url: url.href })
} else {
console.debug('openOptionsFor')
const term = await openOptionsFor(ctx.menuItemId, ctx.selectionText)
await clipboardWrite(term)
}
Expand Down Expand Up @@ -160,20 +160,31 @@ async function onChanged(changes, namespace) {
// console.log('onChanged:', changes, namespace)
for (const [key, { oldValue, newValue }] of Object.entries(changes)) {
if (namespace === 'sync' && key === 'options' && oldValue && newValue) {
if (newValue?.contextMenu) {
console.info('Enabled contextMenu...')
const { bookmarks } = await chrome.storage.sync.get([
'bookmarks',
])
createContextMenus(newValue, bookmarks)
} else {
console.info('Disabled contextMenu...')
chrome.contextMenus.removeAll()
if (oldValue?.contextMenu !== newValue?.contextMenu) {
if (newValue?.contextMenu) {
console.log('%c Enabled contextMenu...', 'color: Lime')
chrome.storage.sync.get(['bookmarks']).then((items) => {
createContextMenus(newValue, items.bookmarks)
})
const { bookmarks } = await chrome.storage.sync.get([
'bookmarks',
])
createContextMenus(newValue, bookmarks)
} else {
console.log(
'%c Disabled contextMenu...',
'color: BlueViolet'
)
chrome.contextMenus.removeAll()
}
}
} else if (namespace === 'sync' && key === 'bookmarks') {
chrome.storage.sync.get(['options']).then((items) => {
createContextMenus(items.options, newValue)
})
const { options } = await chrome.storage.sync.get(['options'])
if (options?.contextMenu) {
console.log('Updating Context Menu Bookmarks...')
console.log('Updating CTX Menu Bookmarks...', 'color: Aqua')
createContextMenus(options, newValue)
}
}
Expand Down Expand Up @@ -211,8 +222,9 @@ async function parseInput(text) {
* @param {String} text
* @param {Function} suggest
*/
// noinspection JSUnusedLocalSymbols
async function onInputChanged(text, suggest) {

Check failure on line 226 in src/js/service-worker.js

View workflow job for this annotation

GitHub Actions / Test

'suggest' is defined but never used
console.debug('onInputChanged:', text, suggest)
console.debug('onInputChanged:', text)
text = text.trim()
const split = text.split(' ')
// console.debug('split:', split)
Expand Down Expand Up @@ -388,7 +400,7 @@ async function setDefaultOptions(defaultOptions) {
await chrome.storage.sync.set({ bookmarks: [] })
}
options = options || {}
console.debug('options', options)
console.debug('options:', options)
let changed = false
for (const [key, value] of Object.entries(defaultOptions)) {
// console.log(`${key}: default: ${value} current: ${options[key]}`)
Expand Down Expand Up @@ -419,7 +431,7 @@ function setNestedDefaults(options, defaults) {
console.log('setNestedDefaults:', options, defaults)
let changed = false
for (const [key, value] of Object.entries(defaults)) {
console.log(`Nested: ${key}`, value)
console.log(`Nested: %c${key}`, 'color: Khaki', value)
if (!options[key]) {
options[key] = {}
}
Expand Down

0 comments on commit ba3386c

Please sign in to comment.