@@ -63,7 +73,7 @@
diff --git a/src/js/export.js b/src/js/export.js
index 7d0cebe..d6b4dda 100644
--- a/src/js/export.js
+++ b/src/js/export.js
@@ -20,10 +20,6 @@ export async function toggleSite(tab) {
} else {
console.log(`Disabling Site: ${url.hostname}`)
sites.splice(sites.indexOf(url.hostname), 1)
- const { options } = await chrome.storage.sync.get(['options'])
- if (options.autoReload) {
- await reloadTab(tab)
- }
}
console.log('sites:', sites)
await chrome.storage.sync.set({ sites })
@@ -40,20 +36,6 @@ export async function enableSite(tab, color) {
})
}
-/**
- * Reload Tab
- * @function reloadTab
- * @param {chrome.tabs.Tab} tab
- */
-export async function reloadTab(tab) {
- await chrome.scripting.executeScript({
- target: { tabId: tab.id },
- func: function () {
- window.location.reload()
- },
- })
-}
-
/**
* Check Host Permissions
* @function checkPerms
diff --git a/src/js/options.js b/src/js/options.js
index 2c56ded..447d853 100644
--- a/src/js/options.js
+++ b/src/js/options.js
@@ -2,12 +2,9 @@
import { checkPerms, saveOptions, updateOptions } from './export.js'
-document.addEventListener('DOMContentLoaded', initOptions)
-
chrome.storage.onChanged.addListener(onChanged)
-
+document.addEventListener('DOMContentLoaded', initOptions)
document.getElementById('grant-perms').addEventListener('click', grantPerms)
-
document
.querySelectorAll('#options-form input')
.forEach((el) => el.addEventListener('change', saveOptions))
diff --git a/src/js/popup.js b/src/js/popup.js
index 8f8f9a8..0298dba 100644
--- a/src/js/popup.js
+++ b/src/js/popup.js
@@ -9,7 +9,9 @@ import {
} from './export.js'
document.addEventListener('DOMContentLoaded', initPopup)
-
+document.getElementById('grant-perms').onclick = grantPerms
+document.getElementById('toggle-site').onclick = toggleSiteClick
+document.getElementById('enable-temp').onclick = enableTempClick
document
.querySelectorAll('a[href]')
.forEach((el) => el.addEventListener('click', popupLinks))
@@ -20,10 +22,6 @@ document
.querySelectorAll('[data-bs-toggle="tooltip"]')
.forEach((el) => new bootstrap.Tooltip(el))
-document.getElementById('grant-perms').onclick = grantPerms
-document.getElementById('toggle-site').onclick = toggleSiteClick
-document.getElementById('enable-temp').onclick = enableTempClick
-
/**
* Initialize Popup
* TODO: Cleanup this function
diff --git a/src/js/service-worker.js b/src/js/service-worker.js
index 70b02c5..d60e636 100644
--- a/src/js/service-worker.js
+++ b/src/js/service-worker.js
@@ -18,10 +18,11 @@ async function onInstalled(details) {
const githubURL = 'https://github.com/cssnr/open-links-in-new-tab'
const options = await Promise.resolve(
setDefaultOptions({
- contextMenu: true,
- showUpdate: false,
autoReload: true,
onScroll: false,
+ updateAll: true,
+ contextMenu: true,
+ showUpdate: false,
})
)
if (options.contextMenu) {
@@ -57,10 +58,10 @@ async function onInstalled(details) {
* @param {chrome.tabs.Tab} tab
*/
async function onClicked(ctx, tab) {
- console.log('contextMenuClick:', ctx, tab)
+ console.debug('contextMenuClick:', ctx, tab)
console.log(`ctx.menuItemId: ${ctx.menuItemId}`)
if (ctx.menuItemId === 'toggle') {
- console.log(`toggle: ctx.pageUrl: ${ctx.pageUrl}`)
+ console.debug(`toggle: ctx.pageUrl: ${ctx.pageUrl}`)
chrome.permissions.request({
origins: ['https://*/*', 'http://*/*'],
})
@@ -71,7 +72,7 @@ async function onClicked(ctx, tab) {
await toggleSite(tab)
}
} else if (ctx.menuItemId === 'temp') {
- console.log(`temp: ctx.pageUrl: ${ctx.pageUrl}`)
+ console.debug(`temp: ctx.pageUrl: ${ctx.pageUrl}`)
await enableSite(tab, 'yellow')
} else if (ctx.menuItemId === 'options') {
chrome.runtime.openOptionsPage()
@@ -86,20 +87,20 @@ async function onClicked(ctx, tab) {
* @param {String} command
*/
async function onCommand(command) {
- console.log(`onCommand: ${command}`)
+ console.debug(`onCommand: ${command}`)
const [tab] = await chrome.tabs.query({ currentWindow: true, active: true })
if (command === 'toggle-site') {
- console.log('toggle-site')
+ console.debug('toggle-site')
const hasPerms = await chrome.permissions.contains({
origins: ['https://*/*', 'http://*/*'],
})
if (hasPerms) {
await toggleSite(tab)
} else {
- console.log('Missing Permissions. Use Popup First!')
+ console.info('Missing Permissions. Use Popup First!')
}
} else if (command === 'enable-temp') {
- console.log('enable-temp', tab)
+ console.debug('enable-temp', tab)
await enableSite(tab, 'yellow')
} else {
console.error(`Unknown command: ${command}`)
@@ -113,25 +114,23 @@ async function onCommand(command) {
* @param {MessageSender} sender
*/
async function onMessage(message, sender) {
- console.log('message, sender:', message, sender)
+ console.debug('message, sender:', message, sender)
const tabId = message.tabId || sender.tab.id
const text = message.badgeText
const color = message.badgeColor
- console.log(`tabId: ${tabId}, text: ${text}, color: ${color}`)
- const bgColor = await chrome.action.getBadgeBackgroundColor({
- tabId: tabId,
- })
- const bgJson = JSON.stringify(bgColor)
- if (bgJson !== JSON.stringify([0, 128, 0, 255])) {
+ console.debug(`tabId: ${tabId}, text: ${text}, color: ${color}`)
+ if (color) {
await chrome.action.setBadgeBackgroundColor({
tabId: tabId,
color: color,
})
}
- await chrome.action.setBadgeText({
- tabId: tabId,
- text: text,
- })
+ if (text) {
+ await chrome.action.setBadgeText({
+ tabId: tabId,
+ text: text,
+ })
+ }
}
/**
@@ -141,7 +140,7 @@ async function onMessage(message, sender) {
* @param {String} namespace
*/
function onChanged(changes, namespace) {
- // console.log('onChanged:', changes, namespace)
+ // console.debug('onChanged:', changes, namespace)
for (const [key, { oldValue, newValue }] of Object.entries(changes)) {
if (namespace === 'sync' && key === 'options' && oldValue && newValue) {
if (oldValue.contextMenu !== newValue.contextMenu) {
@@ -191,7 +190,7 @@ async function setDefaultOptions(defaultOptions) {
* @function createContextMenus
*/
export function createContextMenus() {
- console.log('createContextMenus')
+ console.debug('createContextMenus')
chrome.contextMenus.removeAll()
const ctx = ['all']
const contexts = [
diff --git a/src/js/tab.js b/src/js/tab.js
index 008faac..4e68745 100644
--- a/src/js/tab.js
+++ b/src/js/tab.js
@@ -1,12 +1,19 @@
// JS Content Script tab.js
;(async () => {
- const { sites } = await chrome.storage.sync.get(['sites'])
- // console.log(`sites: ${window.location.host}`, sites)
+ const { options, sites } = await chrome.storage.sync.get([
+ 'options',
+ 'sites',
+ ])
+ console.debug(`sites: ${window.location.host}`, sites)
if (sites?.includes(window.location.host)) {
console.log(`Enabled Host: ${window.location.host}`)
await activateTab('green')
}
+ if (options.updateAll && !chrome.storage.onChanged.hasListener(onChanged)) {
+ console.log('Adding onChanged Listener')
+ chrome.storage.onChanged.addListener(onChanged)
+ }
})()
let tabEnabled = false
@@ -14,19 +21,20 @@ let tabEnabled = false
/**
* Activate Tab
* @function activateTab
+ * @param {String} color
*/
async function activateTab(color) {
// await chrome.runtime.sendMessage({ badgeText: 'On' })
- console.log(`activateTab: color: ${color}`)
+ console.debug(`activateTab: color: ${color}`)
await chrome.runtime.sendMessage({
badgeText: 'On',
badgeColor: color,
})
if (tabEnabled) {
- return console.log('Tab Already Enabled...')
+ return console.info('Tab Already Enabled!')
}
+ console.info('Activating Tab...')
tabEnabled = true
- console.log('Activating Tab...')
updateLinks()
const observer = new MutationObserver(function () {
updateLinks()
@@ -38,7 +46,7 @@ async function activateTab(color) {
})
const { options } = await chrome.storage.sync.get(['options'])
if (options.onScroll) {
- console.log('onScroll Enabled')
+ console.debug('Enabling onScroll...')
const processChange = debounce(() => updateLinks())
document.addEventListener('scroll', processChange)
}
@@ -59,18 +67,50 @@ function updateLinks() {
}
}
+/**
+ * On Changed Callback
+ * @function onChanged
+ * @param {Object} changes
+ * @param {String} namespace
+ */
+async function onChanged(changes, namespace) {
+ console.debug('onChanged:', changes, namespace)
+ for (let [key, { newValue }] of Object.entries(changes)) {
+ if (namespace === 'sync' && key === 'sites') {
+ // console.debug('newValue:', newValue)
+ if (newValue?.includes(window.location.host)) {
+ if (!tabEnabled) {
+ console.log(`Enabling: ${window.location.host}`)
+ await activateTab('green')
+ } else {
+ await chrome.runtime.sendMessage({ badgeColor: 'green' })
+ }
+ } else if (tabEnabled) {
+ console.log(`Disabling: ${window.location.host}`)
+ const { options } = await chrome.storage.sync.get(['options'])
+ if (options.autoReload) {
+ window.location.reload()
+ } else {
+ await chrome.runtime.sendMessage({
+ badgeColor: 'red',
+ })
+ tabEnabled = false
+ }
+ }
+ }
+ }
+}
+
/**
* DeBounce Function
* @function debounce
- * @param {Function} func
+ * @param {Function} fn
* @param {Number} timeout
*/
-function debounce(func, timeout = 300) {
- let timer
+function debounce(fn, timeout = 300) {
+ let timeoutID
return (...args) => {
- clearTimeout(timer)
- timer = setTimeout(() => {
- func.apply(this, args)
- }, timeout)
+ clearTimeout(timeoutID)
+ timeoutID = setTimeout(() => fn(...args), timeout)
}
}