Skip to content

Commit

Permalink
Don't use optional permission
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayquaza01 committed Jan 30, 2024
1 parent e6766e0 commit d5822e2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 55 deletions.
4 changes: 4 additions & 0 deletions src/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
@import url("./Photon/Dropdown.css");
@import url("./Photon/Checkbox.css");

html, body {
font-family: sans-serif;
}

h2 {
font-size: 17px;
font-weight: 500;
Expand Down
6 changes: 4 additions & 2 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ <h2 data-i18n="optionDeleteModeText" class="i18n"></h2>
<h2 data-i18n="optionNotificationsText" class="i18n"></h2>
<p data-i18n="optionNotificationsInfo" class="i18n"></p>
<span class="vertcenter">
<input type="checkbox" id="notifications" class="checkbox" disabled>
<input type="checkbox" id="notifications" class="checkbox">
<label for="notifications" class="i18n" data-i18n="optionNotificationsCheckbox"></label>
</span>
<br>
<button class="default" id="notification-permission-request" name="notificationPermissionRequest"></button>
<!-- <button class="default" id="notification-permission-request" name="notificationPermissionRequest"></button> -->

<h2 data-i18n="manualDelete" class="i18n"></h2>
<p data-i18n="manualDeleteInfo" class="i18n"></p>
Expand All @@ -57,6 +57,8 @@ <h2 data-i18n="manualDelete" class="i18n"></h2>
<h2 data-i18n="sync" class="i18n"></h2>
<button id="syncUp" data-i18n="syncUp" class="i18n default" name="syncUp"></button>
<button id="syncDown" data-i18n="syncDown" class="i18n default" name="syncDown"></button>

<input type="hidden" name="lastRun" value="0">
</form>
</body>
</html>
108 changes: 55 additions & 53 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import browser from "webextension-polyfill";
import { ToggleButton, ToggleButtonState } from "./ToggleButton";
// import { ToggleButton, ToggleButtonState } from "./ToggleButton";
import { Message, MessageState } from "./MessageInterface";
import { i18n } from "./i18n";
import { Options, OptionsInterface, FormElements } from "./OptionsInterface";
Expand All @@ -15,10 +15,10 @@ const uploadButton = document.querySelector("#syncUp") as HTMLButtonElement;
const downloadButton = document.querySelector("#syncDown") as HTMLButtonElement;

// permission toggle button
const notificationRequestButton: ToggleButton = new ToggleButton(
document.querySelector("#notification-permission-request") as HTMLButtonElement,
[browser.i18n.getMessage("notificationRequest"), browser.i18n.getMessage("notificationRevoke")]
);
// const notificationRequestButton: ToggleButton = new ToggleButton(
// document.querySelector("#notification-permission-request") as HTMLButtonElement,
// [browser.i18n.getMessage("notificationRequest"), browser.i18n.getMessage("notificationRevoke")]
// );

// manual delete button
const manualDeleteButton = document.querySelector("#manual-delete") as HTMLButtonElement;
Expand All @@ -39,37 +39,37 @@ function manualDelete(): void {
*
* Updates the button state afterwards
*/
function togglePermission(): void {
// if permission is not currently granted
if (notificationRequestButton.getState() === ToggleButtonState.NO_PERMISSION) {
// attempt to get permission
browser.permissions.request({ permissions: ["notifications"] })
.then((request: boolean) => {
// if user gives permission
// switch button state, enable option, send demo notification
if (request) {
notificationRequestButton.setState(ToggleButtonState.PERMISSION);
formElements.notifications.disabled = false;
}
// otherwise, keep button state same, turn off notifications, disable option
else {
notificationRequestButton.setState(ToggleButtonState.NO_PERMISSION);
formElements.notifications.checked = false;
formElements.notifications.disabled = true;
browser.storage.local.set({ notifications: false });
}
});
}
// if permission currently granted
// revoke permission, switch button state, disable notifications, and disable option
else if (notificationRequestButton.getState() === ToggleButtonState.PERMISSION) {
browser.permissions.remove({ permissions: ["notifications"] });
notificationRequestButton.setState(ToggleButtonState.NO_PERMISSION);
formElements.notifications.checked = false;
formElements.notifications.disabled = true;
browser.storage.local.set({ notifications: false });
}
}
// function togglePermission(): void {
// // if permission is not currently granted
// if (notificationRequestButton.getState() === ToggleButtonState.NO_PERMISSION) {
// // attempt to get permission
// browser.permissions.request({ permissions: ["notifications"] })
// .then((request: boolean) => {
// // if user gives permission
// // switch button state, enable option, send demo notification
// if (request) {
// notificationRequestButton.setState(ToggleButtonState.PERMISSION);
// formElements.notifications.disabled = false;
// }
// // otherwise, keep button state same, turn off notifications, disable option
// else {
// notificationRequestButton.setState(ToggleButtonState.NO_PERMISSION);
// formElements.notifications.checked = false;
// formElements.notifications.disabled = true;
// browser.storage.local.set({ notifications: false });
// }
// });
// }
// // if permission currently granted
// // revoke permission, switch button state, disable notifications, and disable option
// else if (notificationRequestButton.getState() === ToggleButtonState.PERMISSION) {
// browser.permissions.remove({ permissions: ["notifications"] });
// notificationRequestButton.setState(ToggleButtonState.NO_PERMISSION);
// formElements.notifications.checked = false;
// formElements.notifications.disabled = true;
// browser.storage.local.set({ notifications: false });
// }
// }

/**
* Upload current local storage to sync storage
Expand Down Expand Up @@ -100,9 +100,9 @@ async function download(): Promise<void> {
}

// disable notifications if permission not allowed
if (notificationRequestButton.getState() === ToggleButtonState.NO_PERMISSION) {
res.notifications = false;
}
// if (notificationRequestButton.getState() === ToggleButtonState.NO_PERMISSION) {
// res.notifications = false;
// }

await browser.storage.local.set(res);
location.reload();
Expand All @@ -124,7 +124,8 @@ function save(e: Event): void {
days: parseInt(formElements.days.value),
deleteMode: formElements.deleteMode.value,
idleLength: parseInt(formElements.idleLength.value),
notifications: formElements.notifications.checked
notifications: formElements.notifications.checked,
lastRun: parseInt(formElements.lastRun.value)
};

// if changing the setting will update idle / startup
Expand Down Expand Up @@ -167,24 +168,25 @@ async function load(): Promise<void> {
formElements.idleLength.value = res.idleLength.toString();
formElements.deleteMode.value = res.deleteMode;
formElements.notifications.checked = res.notifications;
formElements.lastRun.value = res.lastRun.toString();

// check permissions
const permissions = await browser.permissions.getAll();
// if notification permission
// enable notification option, set button to revoke
if (Array.isArray(permissions.permissions) && permissions.permissions.includes("notifications")) {
formElements.notifications.disabled = false;
notificationRequestButton.setState(ToggleButtonState.PERMISSION);
}
// otherise disable option, set button to enable
else {
formElements.notifications.disabled = true;
notificationRequestButton.setState(ToggleButtonState.NO_PERMISSION);
}
// const permissions = await browser.permissions.getAll();
// // if notification permission
// // enable notification option, set button to revoke
// if (Array.isArray(permissions.permissions) && permissions.permissions.includes("notifications")) {
// formElements.notifications.disabled = false;
// notificationRequestButton.setState(ToggleButtonState.PERMISSION);
// }
// // otherise disable option, set button to enable
// else {
// formElements.notifications.disabled = true;
// notificationRequestButton.setState(ToggleButtonState.NO_PERMISSION);
// }
}

document.addEventListener("DOMContentLoaded", load);
notificationRequestButton.getElement().addEventListener("click", togglePermission);
// notificationRequestButton.getElement().addEventListener("click", togglePermission);
form.addEventListener("input", save);
manualDeleteButton.addEventListener("click", manualDelete);

Expand Down

0 comments on commit d5822e2

Please sign in to comment.