Skip to content

Commit bdacf1d

Browse files
authored
Add Download Support to CTX Menu (#12)
* Add Download Support to CTX Menu * Update Service Worker Functions * Update .prettierrc.json
1 parent e1a6d21 commit bdacf1d

File tree

7 files changed

+45
-43
lines changed

7 files changed

+45
-43
lines changed

.github/workflows/build.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: 'Build'
1+
name: "Build"
22

33
on:
44
workflow_dispatch:
@@ -7,15 +7,15 @@ on:
77

88
jobs:
99
build:
10-
name: 'Build'
10+
name: "Build"
1111
runs-on: ubuntu-latest
1212
timeout-minutes: 5
1313

1414
steps:
15-
- name: 'Checkout'
15+
- name: "Checkout"
1616
uses: actions/checkout@v3
1717

18-
- name: 'Release Version Check'
18+
- name: "Release Version Check"
1919
if: ${{ github.event_name == 'release' }}
2020
run: |
2121
export version="$(jq '.version' < manifest.json | tr -d '"')"
@@ -24,23 +24,23 @@ jobs:
2424
exit 1
2525
fi
2626
27-
- name: 'Setup Node'
27+
- name: "Setup Node"
2828
uses: actions/setup-node@v3
2929
with:
3030
node-version: 18
3131

32-
- name: 'Build All'
32+
- name: "Build All"
3333
run: |
3434
npm install
3535
npm run build
3636
37-
- name: 'Upload to Actions'
37+
- name: "Upload to Actions"
3838
uses: actions/upload-artifact@v3
3939
with:
4040
name: artifacts
4141
path: web-ext-artifacts/
4242

43-
- name: 'Upload to Release'
43+
- name: "Upload to Release"
4444
uses: svenstaro/upload-release-action@v2
4545
with:
4646
repo_token: ${{ secrets.GITHUB_TOKEN }}

.prettierrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"singleQuote": true,
66
"overrides": [
77
{
8-
"files": ["**/*.html"],
8+
"files": ["**/*.html", "**/*.yaml"],
99
"options": {
1010
"singleQuote": false
1111
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "SMWC.world Web Extension and Browser Addon to Easily Patch and Play SMW ROMs.",
44
"homepage_url": "https://smwc.world",
55
"author": "Shane",
6-
"version": "0.1.1",
6+
"version": "0.1.2",
77
"manifest_version": 3,
88
"commands": {
99
"_execute_action": {

src/html/options.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ <h1>SMWC Web Extension</h1>
7575

7676
<div aria-live="polite" aria-atomic="true" class="">
7777
<div id="toast-container" class="toast-container position-fixed bottom-0 end-0 p-3"></div>
78-
</div>
78+
</div> <!-- toast -->
7979

8080
<div class="d-none">
8181
<div class="toast align-items-center border-0" role="alert" aria-live="assertive" aria-atomic="true" data-bs-delay="5000">
@@ -84,7 +84,7 @@ <h1>SMWC Web Extension</h1>
8484
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" id="bi-dot" viewBox="0 0 16 16">
8585
<path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3"/>
8686
</svg>
87-
</div>
87+
</div> <!-- d-none -->
8888

8989
<script type="text/javascript" src="../dist/jquery/jquery.min.js"></script>
9090
<script type="text/javascript" src="../dist/bootstrap/bootstrap.bundle.min.js"></script>

src/html/popup.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@
7878
<a class="link-offset-2 link-underline link-underline-opacity-0 link-underline-opacity-75-hover mx-2" type="button" rel="noopener"
7979
href="homepage_url">SMWC Web Extension</a> v<span class="version"></span>
8080
</div>
81-
</div>
81+
</div> <!-- container -->
8282

8383
<div aria-live="polite" aria-atomic="true" class="">
8484
<div id="toast-container" class="toast-container bottom-0 end-0 p-3"></div>
85-
</div>
85+
</div> <!-- toast -->
8686

8787
<div class="d-none">
8888
<div class="toast align-items-center border-0" role="alert" aria-live="assertive" aria-atomic="true" data-bs-delay="5000">

src/js/popup.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,27 @@ async function popupLinks(event) {
5151
console.debug('popupLinks:', event)
5252
event.preventDefault()
5353
const anchor = event.target.closest('a')
54-
console.debug(`anchor.href: ${anchor.href}`, anchor)
54+
const href = anchor.getAttribute('href').replace(/^\.+/g, '')
55+
console.debug('href:', href)
5556
let url
5657
if (anchor.href.endsWith('html/options.html')) {
5758
chrome.runtime.openOptionsPage()
5859
return window.close()
59-
} else if (
60-
anchor.href.startsWith('http') ||
61-
anchor.href.startsWith('chrome-extension')
62-
) {
63-
// console.debug(`http or chrome-extension`)
64-
url = anchor.href
60+
} else if (href.startsWith('http')) {
61+
url = href
6562
} else {
66-
// console.debug(`else chrome.runtime.getURL`)
67-
url = chrome.runtime.getURL(anchor.href)
63+
url = chrome.runtime.getURL(href)
6864
}
69-
console.log('url:', url)
65+
console.debug('url:', url)
66+
// const tabs = await chrome.tabs.query({ currentWindow: true })
67+
// console.log(tabs)
68+
// for (const tab of tabs) {
69+
// if (tab.url === url) {
70+
// console.debug('tab:', tab)
71+
// await chrome.tabs.update(tab.id, { active: true })
72+
// return window.close()
73+
// }
74+
// }
7075
await chrome.tabs.create({ active: true, url })
7176
return window.close()
7277
}

src/js/service-worker.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { patchRom } from './exports.js'
44

55
chrome.runtime.onStartup.addListener(onStartup)
66
chrome.runtime.onInstalled.addListener(onInstalled)
7-
chrome.contextMenus.onClicked.addListener(contextMenusClicked)
7+
chrome.contextMenus.onClicked.addListener(onClicked)
88
chrome.notifications.onClicked.addListener(notificationsClicked)
99
chrome.storage.onChanged.addListener(onChanged)
1010

@@ -39,7 +39,7 @@ async function onInstalled(details) {
3939
showUpdate: false,
4040
})
4141
)
42-
console.log('options:', options)
42+
console.debug('options:', options)
4343
if (options.contextMenu) {
4444
createContextMenus()
4545
}
@@ -50,7 +50,6 @@ async function onInstalled(details) {
5050
const manifest = chrome.runtime.getManifest()
5151
if (manifest.version !== details.previousVersion) {
5252
const url = `${githubURL}/releases/tag/${manifest.version}`
53-
console.log(`url: ${url}`)
5453
await chrome.tabs.create({ active: false, url })
5554
}
5655
}
@@ -60,16 +59,20 @@ async function onInstalled(details) {
6059

6160
/**
6261
* On Clicked Callback
63-
* @function contextMenusClicked
62+
* @function onClicked
6463
* @param {OnClickData} ctx
6564
* @param {chrome.tabs.Tab} tab
6665
*/
67-
async function contextMenusClicked(ctx, tab) {
68-
console.log('contextMenusClicked:', ctx, tab)
66+
async function onClicked(ctx, tab) {
67+
console.debug('onClicked:', ctx, tab)
6968
const callback = (result, key) => {
7069
console.log('service-worker callback:', result)
7170
if (result[key]) {
72-
chrome.tabs.create({ active: true, url: result[key] }).then()
71+
if (key === 'download') {
72+
chrome.downloads.download({ url: result[key] })
73+
} else {
74+
chrome.tabs.create({ active: true, url: result[key] }).then()
75+
}
7376
} else if (result.error?.__all__) {
7477
console.warn(result.error.__all__[0])
7578
sendNotification('Error', result.error.__all__[0])
@@ -101,15 +104,15 @@ function notificationsClicked(notificationId) {
101104
* @param {String} namespace
102105
*/
103106
function onChanged(changes, namespace) {
104-
// console.log('onChanged:', changes, namespace)
107+
// console.debug('onChanged:', changes, namespace)
105108
for (const [key, { oldValue, newValue }] of Object.entries(changes)) {
106109
if (namespace === 'sync' && key === 'options' && oldValue && newValue) {
107110
if (oldValue.contextMenu !== newValue.contextMenu) {
108111
if (newValue?.contextMenu) {
109-
console.log('Enabled contextMenu...')
112+
console.info('Enabled contextMenu...')
110113
createContextMenus()
111114
} else {
112-
console.log('Disabled contextMenu...')
115+
console.info('Disabled contextMenu...')
113116
chrome.contextMenus.removeAll()
114117
}
115118
}
@@ -148,10 +151,9 @@ function createContextMenus() {
148151
* @return {Object}
149152
*/
150153
async function setDefaultOptions(defaultOptions) {
151-
console.log('setDefaultOptions')
152-
let { options, popup } = await chrome.storage.sync.get(['options', 'popup'])
154+
console.log('setDefaultOptions', defaultOptions)
155+
let { options } = await chrome.storage.sync.get(['options'])
153156
options = options || {}
154-
console.log(options)
155157
let changed = false
156158
for (const [key, value] of Object.entries(defaultOptions)) {
157159
// console.log(`${key}: default: ${value} current: ${options[key]}`)
@@ -163,12 +165,7 @@ async function setDefaultOptions(defaultOptions) {
163165
}
164166
if (changed) {
165167
await chrome.storage.sync.set({ options })
166-
console.log(options)
167-
}
168-
// TODO: Handle popup default(s) differently?
169-
if (popup?.patchType === undefined) {
170-
popup = { patchType: 'doPatch' }
171-
await chrome.storage.sync.set({ popup })
168+
console.log('changed:', options)
172169
}
173170
return options
174171
}

0 commit comments

Comments
 (0)