From 1fab031b14f02037193e1b40dc92f3cf3d2d5c04 Mon Sep 17 00:00:00 2001 From: Vijay Raghavan Aravamudhan Date: Wed, 8 Sep 2021 19:49:12 +0530 Subject: refactor: expose some more methods for session handling on the main repo (#1887) --- src/webview/recipe.js | 7 +++++++ src/webview/sessionHandler.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/webview/sessionHandler.ts diff --git a/src/webview/recipe.js b/src/webview/recipe.js index b2cc55ad3..a3ae4513f 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js @@ -23,6 +23,7 @@ import RecipeWebview from './lib/RecipeWebview'; import Userscript from './lib/Userscript'; import { BadgeHandler } from './badge'; +import { SessionHandler } from './sessionHandler'; import contextMenu from './contextMenu'; import { injectDarkModeStyle, @@ -50,6 +51,8 @@ const debug = require('debug')('Ferdi:Plugin'); const badgeHandler = new BadgeHandler(); +const sessionHandler = new SessionHandler(); + const notificationsHandler = new NotificationsHandler(); // Patching window.open @@ -107,6 +110,10 @@ contextBridge.exposeInMainWorld('ferdi', { badgeHandler.safeParseInt(text), displayNotification: (title, options) => notificationsHandler.displayNotification(title, options), + clearStorageData: (storageLocations) => + sessionHandler.clearStorageData(storageLocations), + releaseServiceWorkers: () => + sessionHandler.releaseServiceWorkers(), getDisplayMediaSelector, getCurrentWebContents, BrowserWindow, diff --git a/src/webview/sessionHandler.ts b/src/webview/sessionHandler.ts new file mode 100644 index 000000000..6a7e62ac5 --- /dev/null +++ b/src/webview/sessionHandler.ts @@ -0,0 +1,28 @@ +import { getCurrentWebContents } from '@electron/remote'; + +const debug = require('debug')('Ferdi:Plugin:SessionHandler'); + +export class SessionHandler { + clearStorageData(storageLocations: string[]) { + try { + debug('Clearing storageLocations:', storageLocations); + const { session } = getCurrentWebContents(); + session.flushStorageData(); + session.clearStorageData({ storages: storageLocations }); + } catch (err) { + debug(err); + } + } + + async releaseServiceWorkers() { + try { + const registrations = await window.navigator.serviceWorker.getRegistrations(); + registrations.forEach(r => { + r.unregister(); + debug('ServiceWorker unregistered'); + }); + } catch (err) { + debug(err); + } + } +} -- cgit v1.2.3-70-g09d2