Skip to content

Commit

Permalink
Updates (#4)
Browse files Browse the repository at this point in the history
* Update uninstallURL, Bump Version

* Update homepage_url

* Simplify Host Pattern

* Update checkPerms, Add requestPerms, SW type module

* Added Permissions onAdded Callback

* Bump Version for Host Pattern Change
  • Loading branch information
smashedr authored Apr 29, 2024
1 parent aed4881 commit aa0ac5f
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ All **Chromium** Based Browsers can install the extension from the
* Add Additional Links to Entries
* Update Main Navigation Links
* Hide Site Header Image
* Search by Registration or Operator from the Popup
* Search Registration/Operator from Popup

### Upcoming Features and Ideas

Expand Down
2 changes: 1 addition & 1 deletion manifest-firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "109.0"
"strict_min_version": "112.0"
}
}
}
21 changes: 6 additions & 15 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "ASN Plus",
"description": "Aviation Safety Network Plus - Additional Features, Display and Search Options.",
"homepage_url": "https://github.com/cssnr/asn-plus",
"homepage_url": "https://asn-plus.cssnr.com/",
"author": "Shane",
"version": "0.0.3",
"version": "0.1.0",
"manifest_version": 3,
"commands": {
"_execute_action": {
Expand All @@ -20,30 +20,21 @@
}
},
"permissions": ["contextMenus", "scripting", "storage"],
"host_permissions": [
"http://aviation-safety.net/*",
"https://aviation-safety.net/*"
],
"host_permissions": ["*://aviation-safety.net/*"],
"content_scripts": [
{
"matches": [
"http://aviation-safety.net/*",
"https://aviation-safety.net/*"
],
"matches": ["*://aviation-safety.net/*"],
"run_at": "document_start",
"js": ["dist/jquery/jquery.min.js", "js/asn.js", "js/content-script.js"]
}
],
"web_accessible_resources": [
{
"resources": ["css/dark.css"],
"matches": [
"http://aviation-safety.net/*",
"https://aviation-safety.net/*"
]
"matches": ["*://aviation-safety.net/*"]
}
],
"background": {},
"background": { "type": "module" },
"options_ui": {
"page": "html/options.html",
"open_in_tab": true
Expand Down
24 changes: 18 additions & 6 deletions src/js/export.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
// JS Exports

/**
* Request Host Permissions
* @function requestPerms
* @return {chrome.permissions.request}
*/
export async function requestPerms() {
return await chrome.permissions.request({
origins: ['*://aviation-safety.net/*'],
})
}

/**
* Check Host Permissions
* @function checkPerms
* @return {Boolean}
*/
export async function checkPerms() {
const hasPermsEl = document.querySelectorAll('.has-perms')
const grantPermsEl = document.querySelectorAll('.grant-perms')
const hasPerms = await chrome.permissions.contains({
origins: [
'http://aviation-safety.net/*',
'https://aviation-safety.net/*',
],
origins: ['*://aviation-safety.net/*'],
})
console.debug('checkPerms:', hasPerms)
if (typeof document === 'undefined') {
console.debug('document undefined')
return hasPerms
}
const hasPermsEl = document.querySelectorAll('.has-perms')
const grantPermsEl = document.querySelectorAll('.grant-perms')
if (hasPerms) {
hasPermsEl.forEach((el) => el.classList.remove('d-none'))
grantPermsEl.forEach((el) => el.classList.add('d-none'))
Expand Down
21 changes: 17 additions & 4 deletions src/js/oninstall.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// JS for oninstall.html

import { checkPerms } from './export.js'
import { checkPerms, requestPerms } from './export.js'

chrome.permissions.onAdded.addListener(onAdded)

document.addEventListener('DOMContentLoaded', domContentLoaded)
document.getElementById('grant-perms').addEventListener('click', grantPerms)
Expand All @@ -22,9 +24,7 @@ async function domContentLoaded() {
*/
async function grantPerms(event) {
console.debug('grantPerms:', event)
await chrome.permissions.request({
origins: ['http://aviation-safety.net/*', 'https://aviation-safety.net/*'],
})
await requestPerms()
const hasPerms = await checkPerms()
if (hasPerms) {
chrome.runtime.openOptionsPage()
Expand All @@ -43,3 +43,16 @@ function openOptions(event) {
chrome.runtime.openOptionsPage()
window.close()
}

/**
* Permissions On Added Callback
* @param permissions
*/
async function onAdded(permissions) {
console.info('onAdded', permissions)
const hasPerms = await checkPerms()
if (hasPerms) {
chrome.runtime.openOptionsPage()
window.close()
}
}
25 changes: 18 additions & 7 deletions src/js/options.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// JS for options.html

import { checkPerms, saveOptions, updateOptions } from './export.js'
import {
checkPerms,
requestPerms,
saveOptions,
updateOptions,
} from './export.js'

chrome.storage.onChanged.addListener(onChanged)
chrome.permissions.onAdded.addListener(onAdded)

document.addEventListener('DOMContentLoaded', initOptions)
document.getElementById('grant-perms').addEventListener('click', grantPerms)
document
Expand Down Expand Up @@ -62,12 +69,7 @@ function onChanged(changes, namespace) {
*/
async function grantPerms(event) {
console.debug('grantPermsBtn:', event)
await chrome.permissions.request({
origins: [
'http://aviation-safety.net/*',
'https://aviation-safety.net/*',
],
})
await requestPerms()
await checkPerms()
}

Expand Down Expand Up @@ -102,3 +104,12 @@ async function setShortcuts(mapping) {
}
}
}

/**
* Permissions On Added Callback
* @param permissions
*/
async function onAdded(permissions) {
console.debug('onAdded', permissions)
await checkPerms()
}
17 changes: 9 additions & 8 deletions src/js/popup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// JS for popup.html

import { checkPerms, saveOptions, showToast, updateOptions } from './export.js'
import {
checkPerms,
requestPerms,
saveOptions,
showToast,
updateOptions,
} from './export.js'

document.addEventListener('DOMContentLoaded', initPopup)
document.getElementById('grant-perms').addEventListener('click', grantPerms)
Expand Down Expand Up @@ -126,14 +132,9 @@ async function popupLinks(event) {
* @function grantPerms
* @param {Event} event
*/
function grantPerms(event) {
async function grantPerms(event) {
console.debug('grantPerms:', event)
chrome.permissions.request({
origins: [
'http://aviation-safety.net/*',
'https://aviation-safety.net/*',
],
})
requestPerms()
window.close()
}

Expand Down
26 changes: 11 additions & 15 deletions src/js/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// JS Background Service Worker

import { checkPerms } from './export.js'

chrome.runtime.onStartup.addListener(onStartup)
chrome.runtime.onInstalled.addListener(onInstalled)
chrome.runtime.onMessage.addListener(onMessage)
Expand All @@ -8,13 +10,14 @@ chrome.commands.onCommand.addListener(onCommand)
chrome.storage.onChanged.addListener(onChanged)

const asnHomePageURL = 'https://aviation-safety.net/'
const uninstallURL = 'https://asn-plus.cssnr.com/uninstall/'

/**
* On Startup Callback
* @function onStartup
*/
async function onStartup(arg) {
console.log('onStartup', arg)
async function onStartup() {
console.log('onStartup')
if (typeof browser !== 'undefined') {
console.log('FireFox Startup - Fix for Bug')
const { options } = await chrome.storage.sync.get(['options'])
Expand Down Expand Up @@ -53,12 +56,8 @@ async function onInstalled(details) {
await registerDarkMode()
}
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
const hasPerms = await chrome.permissions.contains({
origins: [
'http://aviation-safety.net/*',
'https://aviation-safety.net/*',
],
})
const hasPerms = await checkPerms()
console.debug('hasPerms:', hasPerms)
if (hasPerms) {
chrome.runtime.openOptionsPage()
} else {
Expand All @@ -74,7 +73,7 @@ async function onInstalled(details) {
}
}
}
await chrome.runtime.setUninstallURL(`${githubURL}/issues`)
await chrome.runtime.setUninstallURL(uninstallURL)
}

/**
Expand All @@ -94,7 +93,7 @@ function onMessage(message, sender, sendResponse) {
tabId: sender.tab.id,
},
}
console.info(`SW: Dark Mode: ${message.dark}`, darkCss)
console.debug(`SW: Dark Mode: ${message.dark}`, darkCss)
if (message.dark === 'off') {
try {
chrome.scripting.removeCSS(darkCss)
Expand Down Expand Up @@ -138,7 +137,7 @@ async function onClicked(ctx, tab) {
* @param {String} command
*/
async function onCommand(command) {
console.error('onCommand:', command)
console.debug('onCommand:', command)
if (command === 'openHome') {
await chrome.tabs.create({ active: true, url: asnHomePageURL })
} else {
Expand Down Expand Up @@ -188,10 +187,7 @@ async function registerDarkMode() {
const asnDark = {
id: 'asn-dark',
css: ['css/dark.css'],
matches: [
'http://aviation-safety.net/*',
'https://aviation-safety.net/*',
],
matches: ['*://aviation-safety.net/*'],
runAt: 'document_start',
}
console.log('registerDarkMode', asnDark)
Expand Down

0 comments on commit aa0ac5f

Please sign in to comment.