Skip to content

Commit

Permalink
Improve Filter Editing (#57)
Browse files Browse the repository at this point in the history
* Update Editing and Logging

* Update Toast
  • Loading branch information
smashedr authored Dec 30, 2023
1 parent 6ac105c commit 3822e9a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 63 deletions.
6 changes: 3 additions & 3 deletions src/js/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function injectTab({

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

// Create URL to links.html
const url = new URL(chrome.runtime.getURL('../html/links.html'))
Expand All @@ -40,7 +40,7 @@ export async function injectTab({
})

// Open Tab to links.html with desired params
console.log(`url: ${url.toString()}`)
console.debug(`url: ${url.toString()}`)
await chrome.tabs.create({ active: true, url: url.toString() })
}

Expand All @@ -51,7 +51,7 @@ export async function injectTab({
*/
export function updateOptions(options) {
for (const [key, value] of Object.entries(options)) {
// console.log(`${key}: ${value}`)
// console.debug(`${key}: ${value}`)
const el = document.getElementById(key)
if (el) {
if (typeof value === 'boolean') {
Expand Down
12 changes: 6 additions & 6 deletions src/js/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (!window.injected) {
* @param {Function} sendResponse
*/
function onMessage(message, sender, sendResponse) {
console.log(`onMessage: message: ${message}`)
console.debug(`onMessage: message: ${message}`)
if (message === 'all') {
sendResponse(extractAllLinks())
} else if (message === 'selection') {
Expand All @@ -30,14 +30,14 @@ function onMessage(message, sender, sendResponse) {
* @return {Array}
*/
function extractAllLinks() {
console.log('extractAllLinks')
console.debug('extractAllLinks')
const links = []
for (const element of document.links) {
if (element.href) {
pushElement(links, element)
}
}
console.log('links:', links)
console.debug('links:', links)
return links
}

Expand All @@ -47,10 +47,10 @@ function extractAllLinks() {
* @return {Array}
*/
function extractSelection() {
console.log('extractSelection')
console.debug('extractSelection')
const links = []
const selection = window.getSelection()
console.log('selection:', selection)
console.debug('selection:', selection)
if (selection?.type !== 'Range') {
console.log('No selection or wrong selection.type')
return links
Expand All @@ -66,7 +66,7 @@ function extractSelection() {
}
})
}
console.log('links:', links)
console.debug('links:', links)
return links
}

Expand Down
26 changes: 13 additions & 13 deletions src/js/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function initLinks() {
try {
const tabId = parseInt(urlParams.get('tab'))
const selection = urlParams.has('selection')
console.log(`tabId: ${tabId}, selection: ${selection}`)
console.debug(`tabId: ${tabId}, selection: ${selection}`)

if (tabId) {
const action = selection ? 'selection' : 'all'
Expand All @@ -63,13 +63,13 @@ async function initLinks() {
* @param {Array} links
*/
async function processLinks(links) {
console.log('processLinks:', links)
console.debug('processLinks:', links)
const urlFilter = urlParams.get('filter')
const onlyDomains = urlParams.has('domains')
console.log(`urlFilter: ${urlFilter}`)
console.log(`onlyDomains: ${onlyDomains}`)
console.debug(`urlFilter: ${urlFilter}`)
console.debug(`onlyDomains: ${onlyDomains}`)
const { options } = await chrome.storage.sync.get(['options'])
console.log('options:', options)
console.debug('options:', options)

// Filter links by :// if not disabled by user
if (options.defaultFilter) {
Expand All @@ -86,7 +86,7 @@ async function processLinks(links) {
// Filter links based on pattern
if (urlFilter) {
const re = new RegExp(urlFilter, options.flags)
console.log(`Filtering Links with re: ${re}`)
console.debug(`Filtering Links with re: ${re}`)
items = items.filter((item) => item.match(re))
}

Expand Down Expand Up @@ -153,7 +153,7 @@ function getBaseURL(link) {
* @param {String} selector
*/
function updateTable(links, selector) {
console.log(`updateTable: ${selector}`)
console.debug(`updateTable: ${selector}`)
const tbody = document.querySelector(`${selector} tbody`)
links.forEach(function (url) {
const link = document.createElement('a')
Expand All @@ -171,11 +171,11 @@ function updateTable(links, selector) {
* @param {MouseEvent} event
*/
function openLinksClick(event) {
console.log('openLinksClick:', event)
console.debug('openLinksClick:', event)
const closest = event.target?.closest('a')
const target = document.querySelector(closest?.dataset?.target)
let links = target?.innerText?.trim()
console.log('links:', links)
console.debug('links:', links)
if (links) {
links.split('\n').forEach(function (url) {
chrome.tabs.create({ active: false, url }).then()
Expand All @@ -191,7 +191,7 @@ function openLinksClick(event) {
* @param {MouseEvent} event
*/
function downloadFileClick(event) {
console.log('downloadFileClick:', event)
console.debug('downloadFileClick:', event)
const closest = event.target?.closest('a')
const target = document.querySelector(closest?.dataset?.target)
let links = target?.innerText?.trim()
Expand All @@ -210,7 +210,7 @@ function downloadFileClick(event) {
* @param {String} text
*/
function download(filename, text) {
console.log(`download: ${filename}`)
console.debug(`download: ${filename}`)
const element = document.createElement('a')
element.setAttribute(
'href',
Expand All @@ -229,7 +229,7 @@ function download(filename, text) {
* @param {KeyboardEvent} e
*/
function handleKeyboard(e) {
// console.log('handleKeyboard:', e)
// console.debug('handleKeyboard:', e)
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.repeat) {
return
}
Expand All @@ -248,7 +248,7 @@ function handleKeyboard(e) {
}

function dtDraw(event) {
console.log('dtDraw:', event)
console.debug('dtDraw:', event)
const tbody = event.target.querySelector('tbody')
let length = tbody.rows.length
if (tbody.rows.length === 1) {
Expand Down
8 changes: 4 additions & 4 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ if (backToTop) {
if (typeof ClipboardJS !== 'undefined') {
const clipboard = new ClipboardJS('.clip')
clipboard.on('success', function (event) {
// console.info('clipboard.success:', event)
// console.debug('clipboard.success:', event)
const text = event.text.trim()
console.log(`text: "${text}"`)
console.debug(`text: "${text}"`)
if (event.trigger.dataset.toast) {
showToast(event.trigger.dataset.toast)
} else {
showToast('Copied to Clipboard')
}
})
clipboard.on('error', function (event) {
// console.log('clipboard.error:', event)
// console.debug('clipboard.error:', event)
showToast('Clipboard Copy Failed', 'warning')
})
}
Expand Down Expand Up @@ -55,7 +55,7 @@ function onScroll() {
function showToast(message, type = 'success') {
console.log(`showToast: ${type}:`, message)
const element = document.querySelector('.d-none .toast').cloneNode(true)
element.addEventListener('mouseover', () => toast.hide())
element.addEventListener('mousemove', () => toast.hide())
element.classList.add(`text-bg-${type}`)
element.querySelector('.toast-body').innerHTML = message
document.getElementById('toast-container').appendChild(element)
Expand Down
43 changes: 24 additions & 19 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ optionsForm
* @function initOptions
*/
async function initOptions() {
// console.log('initOptions')
// console.debug('initOptions')
const { options, patterns } = await chrome.storage.sync.get([
'options',
'patterns',
])
console.log('options, patterns:', options, patterns)
console.debug('options, patterns:', options, patterns)
updateOptions(options)
updateTable(patterns)

Expand All @@ -52,7 +52,7 @@ async function initOptions() {
* @param {String} namespace
*/
function onChanged(changes, namespace) {
// console.log('onChanged:', changes, namespace)
// console.debug('onChanged:', changes, namespace)
for (let [key, { newValue }] of Object.entries(changes)) {
if (namespace === 'sync' && key === 'options') {
updateOptions(newValue)
Expand All @@ -69,7 +69,7 @@ function onChanged(changes, namespace) {
* @param {SubmitEvent} event
*/
async function addFilter(event) {
// console.log('addFilter:', event)
// console.debug('addFilter:', event)
event.preventDefault()
const element = document.querySelector('#filters-form input')
const filter = element.value
Expand All @@ -78,7 +78,7 @@ async function addFilter(event) {
const { patterns } = await chrome.storage.sync.get(['patterns'])
if (!patterns.includes(filter)) {
patterns.push(filter)
console.log('patterns:', patterns)
console.debug('patterns:', patterns)
await chrome.storage.sync.set({ patterns })
updateTable(patterns)
}
Expand Down Expand Up @@ -184,27 +184,33 @@ async function saveEditing(event, idx) {
console.debug(`saveEditInput: ${idx}`, event)
const td = document.getElementById(`td-filter-${idx}`)
console.debug('td:', td)

if (!td) {
return console.warn(`TD Not Found: #td-filter-${idx}`)
console.warn(`TD Not Found: #td-filter-${idx}`)
return false
}

const input = td.querySelector('input')
console.log('input:', input)
const value = input.value
let value = input.value
console.log('value:', value)
if (!value) {
await deleteFilter(event, idx)
return true
}

const { patterns } = await chrome.storage.sync.get(['patterns'])
if (value !== patterns[idx]) {
console.log(`chrome.storage.sync.set: patterns[${idx}]: ${value}`)
console.debug('patterns:', patterns)
if (value === patterns[idx]) {
console.info('Value Unchanged!')
} else if (patterns.includes(value)) {
showToast('Filter Already Exists!', 'warning')
console.info('Value Already Exists!')
value = patterns[idx]
} else {
console.info(
`Updated idx: ${idx} from "${patterns[idx]}" to "${value}"`
)
patterns[idx] = value
await chrome.storage.sync.set({ patterns })
} else {
console.info('Value Unchanged!')
}

const link = genFilterLink(idx, value)
Expand All @@ -222,7 +228,6 @@ function beginEditing(event, idx) {
console.debug(`addEditInput: ${idx}`, event)
const td = document.getElementById(`td-filter-${idx}`)
console.debug('td:', td)

if (!td) {
return console.warn(`TD Not Found: #td-filter-${idx}`)
}
Expand Down Expand Up @@ -252,11 +257,11 @@ async function deleteFilter(event, index = undefined) {
console.log('deleteFilter:', event)
event.preventDefault()
const { patterns } = await chrome.storage.sync.get(['patterns'])
// console.debug('patterns:', patterns)
if (!index) {
const anchor = event.target.closest('a')
const filter = anchor?.dataset?.value
console.log(`filter: ${filter}`)
// console.log('patterns:', patterns)
if (filter && patterns.includes(filter)) {
index = patterns.indexOf(filter)
}
Expand All @@ -265,7 +270,7 @@ async function deleteFilter(event, index = undefined) {
if (index !== undefined) {
patterns.splice(index, 1)
await chrome.storage.sync.set({ patterns })
console.log('patterns:', patterns)
console.debug('patterns:', patterns)
updateTable(patterns)
document.getElementById('add-filter').focus()
}
Expand Down Expand Up @@ -293,7 +298,7 @@ async function resetForm(event) {
* @param {InputEvent} event
*/
async function saveOptions(event) {
console.log('saveOptions:', event)
console.debug('saveOptions:', event)
const { options } = await chrome.storage.sync.get(['options'])
let key = event.target?.id
let value
Expand All @@ -303,7 +308,7 @@ async function saveOptions(event) {
let flags = element.value.toLowerCase().replace(/\s+/gm, '').split('')
flags = new Set(flags)
flags = [...flags].join('')
console.log(`flags: ${flags}`)
console.debug(`flags: ${flags}`)
for (const flag of flags) {
if (!'dgimsuvy'.includes(flag)) {
element.classList.add('is-invalid')
Expand All @@ -321,7 +326,7 @@ async function saveOptions(event) {
}
if (value !== undefined) {
options[key] = value
console.log(`Set: ${key}:`, value)
console.info(`Set: ${key}:`, value)
await chrome.storage.sync.set({ options })
}
}
Loading

0 comments on commit 3822e9a

Please sign in to comment.