Skip to content

Commit 52f9be0

Browse files
authored
Fix Default Local Options and Android (#65)
- Fixes for Android and Local Defaults
1 parent 8e65330 commit 52f9be0

File tree

6 files changed

+110
-46
lines changed

6 files changed

+110
-46
lines changed

src/html/options.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ <h2>Django Files Options</h2>
7676
<input id="recentFiles" aria-describedby="recentFilesHelp" type="number" class="form-control" autocomplete="off">
7777
<div class="form-text" id="recentFilesHelp">Popup Recent Files. <span class="d-inline-block">0 to Disable.</span></div>
7878
</div>
79-
<div class="col-sm-4 col-12 mb-2 non-mobile">
79+
<div class="col-sm-4 col-12 mb-2" data-mobile-add="d-none">
8080
<label for="popupWidth" class="form-label"><i class="fa-solid fa-arrows-left-right-to-line me-2"></i> Popup Width</label>
8181
<input id="popupWidth" aria-describedby="popupWidthHelp" type="number" class="form-control" autocomplete="off" step="10">
8282
<div class="form-text" id="popupWidthHelp">Popup Width in Pixels. <span class="d-inline-block">320-600.</span></div>
@@ -141,7 +141,7 @@ <h2>Django Files Options</h2>
141141
</label>
142142
</div>
143143

144-
<div class="form-check form-switch mb-2 non-mobile">
144+
<div class="form-check form-switch mb-2" data-mobile-add="d-none">
145145
<input class="form-check-input" id="popupSidePanel" type="checkbox" role="switch">
146146
<label class="form-check-label" for="popupSidePanel" aria-describedby="popupSidePanelHelp">
147147
Show SidePanel Button in Popup
@@ -171,7 +171,7 @@ <h2>Django Files Options</h2>
171171
</label>
172172
</div>
173173

174-
<div class="form-check form-switch mb-2 non-mobile">
174+
<div class="form-check form-switch mb-2" data-mobile-add="d-none">
175175
<input class="form-check-input" id="contextMenu" type="checkbox" role="switch" data-related="reloadMenu">
176176
<label class="form-check-label" for="contextMenu" aria-describedby="contextMenuHelp">
177177
Enable Context Menu
@@ -180,7 +180,7 @@ <h2>Django Files Options</h2>
180180
</span>
181181
</label>
182182
</div><!-- data-related #reloadMenu -->
183-
<div id="reloadMenu" class="ms-5 mb-2 non-mobile" style="display: none;">
183+
<div id="reloadMenu" class="ms-5 mb-2" style="display: none;" data-mobile-add="d-none">
184184
<div class="form-check form-switch mb-2">
185185
<input class="form-check-input" id="ctxSidePanel" type="checkbox" role="switch">
186186
<label class="form-check-label" for="ctxSidePanel" aria-describedby="ctxSidePanelHelp">

src/html/popup.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
<i class="fa-solid fa-key"></i></a>
4646
<a id="pop-in" class="btn btn-sm btn-outline-body d-inline-block d-none" role="button" title="Pop In">
4747
<i class="fa-regular fa-window-restore"></i></a>
48-
<a id="pop-out" class="btn btn-sm btn-outline-body d-inline-block" role="button" title="Pop Out">
48+
<a id="pop-out" class="btn btn-sm btn-outline-body d-inline-block" role="button" title="Pop Out" data-mobile-add="d-none">
4949
<i class="fa-solid fa-arrow-up-right-from-square"></i></a>
50-
<button id="side-panel" class="btn btn-sm btn-outline-info d-none" type="button" title="Side Panel">
50+
<button id="side-panel" class="btn btn-sm btn-outline-info d-none" type="button" title="Side Panel" data-mobile-add="d-none">
5151
<i class="fa-solid fa-table-columns"></i></button>
5252
<a class="btn btn-sm btn-outline-primary" role="button" href="../html/options.html" title="Options">
5353
<i class="fa-solid fa-gears"></i></a>

src/js/exports.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,48 @@ export function debounce(fn, timeout = 250) {
124124
}
125125
}
126126

127+
/**
128+
* @function updatePlatform
129+
* @return {Promise<any>}
130+
*/
131+
export async function updatePlatform() {
132+
const platform = await chrome.runtime.getPlatformInfo()
133+
console.debug('updatePlatform:', platform)
134+
const splitCls = (cls) => cls.split(' ').filter(Boolean)
135+
if (platform.os === 'android' && typeof document !== 'undefined') {
136+
// document.querySelectorAll('[class*="mobile-"]').forEach((el) => {
137+
document
138+
.querySelectorAll(
139+
'[data-mobile-add],[data-mobile-remove],[data-mobile-replace]'
140+
)
141+
.forEach((el) => {
142+
if (el.dataset.mobileAdd) {
143+
for (const cls of splitCls(el.dataset.mobileAdd)) {
144+
// console.debug('mobileAdd:', cls)
145+
el.classList.add(cls)
146+
}
147+
}
148+
if (el.dataset.mobileRemove) {
149+
for (const cls of splitCls(el.dataset.mobileRemove)) {
150+
// console.debug('mobileAdd:', cls)
151+
el.classList.remove(cls)
152+
}
153+
}
154+
if (el.dataset.mobileReplace) {
155+
const split = splitCls(el.dataset.mobileReplace)
156+
// console.debug('mobileReplace:', split)
157+
for (let i = 0; i < split.length; i += 2) {
158+
const one = split[i]
159+
const two = split[i + 1]
160+
// console.debug(`replace: ${one} >> ${two}`)
161+
el.classList.replace(one, two)
162+
}
163+
}
164+
})
165+
}
166+
return platform
167+
}
168+
127169
// /**
128170
// *
129171
// * @param {String} type

src/js/options.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// JS for options.html
22

3-
import { showToast } from './exports.js'
3+
import { showToast, updatePlatform } from './exports.js'
44

55
chrome.storage.onChanged.addListener(onChanged)
66
document.addEventListener('DOMContentLoaded', initOptions)
@@ -50,13 +50,8 @@ async function initOptions() {
5050
siteUrl.focus()
5151
}
5252

53-
const platform = await chrome.runtime.getPlatformInfo()
54-
if (platform.os === 'android') {
55-
document.querySelectorAll('.non-mobile').forEach((el) => {
56-
console.log('non-mobile el:', el)
57-
el.classList.add('d-none')
58-
})
59-
}
53+
const platform = await updatePlatform()
54+
console.debug('platform:', platform)
6055
}
6156

6257
/**

src/js/popup.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
openPopup,
77
openSidePanel,
88
showToast,
9+
updatePlatform,
910
} from './exports.js'
1011

1112
import {
@@ -123,7 +124,9 @@ async function initPopup(event) /* NOSONAR */ {
123124
const { options } = await chrome.storage.sync.get(['options'])
124125
console.debug('options:', options)
125126
document.getElementById('popupPreview').checked = options.popupPreview
126-
const platform = await chrome.runtime.getPlatformInfo()
127+
const platform = await updatePlatform()
128+
console.debug('platform:', platform)
129+
127130
if (platform.os !== 'android' && popupView === 'popup') {
128131
document.body.style.width = `${options.popupWidth}px`
129132
console.debug(`%c SET: width: ${options.popupWidth}`, 'color: Yellow')
@@ -132,11 +135,11 @@ async function initPopup(event) /* NOSONAR */ {
132135
}
133136
}
134137
if (platform.os === 'android') {
138+
console.debug('%c SET: fontSize: 1.3rem + px-1', 'color: Orange')
135139
document.documentElement.style.fontSize = '1.3rem'
136140
document
137141
.querySelectorAll('.hover-menu > a')
138142
.forEach((el) => el.classList.add('px-1'))
139-
console.debug('%c SET: fontSize: 1.3rem + px-1', 'color: Orange')
140143
}
141144

142145
// Manifest
@@ -1107,6 +1110,10 @@ function onMouseLeave() {
11071110
*/
11081111
async function popOutClick(event, close = true) {
11091112
console.debug('popOutClick:', event)
1113+
const platform = await chrome.runtime.getPlatformInfo()
1114+
if (platform.os === 'android') {
1115+
return console.warn('Blocking Popout on Android.')
1116+
}
11101117
await chrome.storage.local.set({ popupView: 'panel' })
11111118
await chrome.action.setPopup({ popup: '' })
11121119
await openExtPanel()

src/js/service-worker.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,37 @@ async function actionOnClicked(event) {
2020
/**
2121
* On Installed Callback
2222
* @function onInstalled
23-
* @param {InstalledDetails} details
23+
* @param {chrome.runtime.InstalledDetails} details
2424
*/
2525
async function onInstalled(details) {
2626
console.log('onInstalled:', details)
2727
const githubURL = 'https://github.com/django-files/web-extension'
2828
const installURL = 'https://django-files.github.io/extension/#configure'
29-
const options = await Promise.resolve(
30-
setDefaultOptions({
31-
siteUrl: '',
32-
authToken: '',
33-
recentFiles: 14,
34-
popupWidth: 380,
35-
popupTimeout: 10,
36-
popupPreview: true,
37-
popupIcons: true,
38-
iconPrivate: true,
39-
iconPassword: true,
40-
iconExpire: false,
41-
popupLinks: true,
42-
popupSidePanel: true,
43-
checkAuth: true,
44-
deleteConfirm: true,
45-
ctxSidePanel: true,
46-
contextMenu: true,
47-
showUpdate: false,
48-
radioBackground: 'bgPicture',
49-
pictureURL: 'https://picsum.photos/1920/1080',
50-
videoURL: '',
51-
})
52-
)
29+
const options = await setDefaultOptions({
30+
siteUrl: '',
31+
authToken: '',
32+
recentFiles: 14,
33+
popupWidth: 380,
34+
popupTimeout: 10,
35+
popupPreview: true,
36+
popupIcons: true,
37+
iconPrivate: true,
38+
iconPassword: true,
39+
iconExpire: false,
40+
popupLinks: true,
41+
popupSidePanel: true,
42+
checkAuth: true,
43+
deleteConfirm: true,
44+
ctxSidePanel: true,
45+
contextMenu: true,
46+
showUpdate: false,
47+
radioBackground: 'bgPicture',
48+
pictureURL: 'https://picsum.photos/1920/1080',
49+
videoURL: '',
50+
})
5351
console.log('options:', options)
52+
await setStorageDefaults(chrome.storage.local, { popupView: 'popup' })
53+
5454
if (options.contextMenu) {
5555
// noinspection ES6MissingAwait
5656
createContextMenus(options)
@@ -60,7 +60,6 @@ async function onInstalled(details) {
6060
// noinspection ES6MissingAwait
6161
chrome.runtime.openOptionsPage()
6262
await chrome.tabs.create({ active: false, url: installURL })
63-
await chrome.storage.local.set({ popupView: 'popup' })
6463
} else if (details.reason === 'update' && options.showUpdate) {
6564
if (manifest.version !== details.previousVersion) {
6665
let { internal } = await chrome.storage.sync.get(['internal'])
@@ -149,7 +148,7 @@ async function onCommand(command) {
149148
/**
150149
* Context Menus On Clicked Callback
151150
* @function contextMenusClicked
152-
* @param {OnClickData} ctx
151+
* @param {chrome.contextMenus.OnClickData} ctx
153152
*/
154153
async function contextMenusClicked(ctx) {
155154
console.debug('contextMenusClicked:', ctx)
@@ -230,7 +229,7 @@ function onChanged(changes, namespace) {
230229
* On Message Callback
231230
* @function onMessage
232231
* @param {Object} message
233-
* @param {MessageSender} sender
232+
* @param {chrome.runtime.MessageSender} sender
234233
*/
235234
function onMessage(message, sender) {
236235
console.debug('onMessage: message, sender:', message, sender)
@@ -519,7 +518,7 @@ async function clipboardWrite(value) {
519518
* Set Default Options
520519
* @function setDefaultOptions
521520
* @param {Object} defaultOptions
522-
* @return {Object}
521+
* @return {Promise<Object>}
523522
*/
524523
async function setDefaultOptions(defaultOptions) {
525524
console.log('setDefaultOptions')
@@ -536,7 +535,28 @@ async function setDefaultOptions(defaultOptions) {
536535
}
537536
if (changed) {
538537
await chrome.storage.sync.set({ options })
539-
console.log('options:', options)
538+
console.log('changed options:', options)
540539
}
541540
return options
542541
}
542+
543+
/**
544+
* @function setStorageDefaults
545+
* @param {chrome.storage.LocalStorageArea|chrome.storage.SyncStorageArea} storageArea
546+
* @param {Object} defaultOptions
547+
* @return {Promise<void>}
548+
*/
549+
async function setStorageDefaults(storageArea, defaultOptions) {
550+
console.log('%c setStorageDefaults:', 'color: Lime', defaultOptions)
551+
const current = await storageArea.get()
552+
const data = {}
553+
for (const [key, value] of Object.entries(defaultOptions)) {
554+
if (current[key] === undefined) {
555+
data[key] = value
556+
}
557+
}
558+
if (Object.keys(data).length > 0) {
559+
console.log('%c Set data:', 'color: Yellow', data)
560+
await storageArea.set(data)
561+
}
562+
}

0 commit comments

Comments
 (0)