Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extension/src/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ if (Activator.isJsonContentType()) {
DomEvents.afterHeadAvailable(Loader.setupResources);
DomEvents.afterDocumentLoaded(Loader.loadViewer);
} else {
Activator.checkSettings()
.then((forceActivation) => {
Activator.checkActivationSetting()
.then((forceActivation: boolean) => {
if (forceActivation) {
DomEvents.afterDocumentLoaded(Loader.forceSetupAndLoadViewer);
}
Expand Down
11 changes: 4 additions & 7 deletions extension/src/content/Activator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ export function isJsonContentType(): boolean {
);
}

export async function checkSettings(): Promise<boolean> {
export async function checkActivationSetting(): Promise<boolean> {
const settings = await STORAGE.get<Settings>(SETTINGS_KEY);
const activationUrlRegex = settings?.activationUrlRegex || null;

if (activationUrlRegex === null) {
return false;
}

if (activationUrlRegex === ".*") {
return true;
}

const regex = new RegExp(activationUrlRegex);
return regex.test(location.href);
const activationRegex = new RegExp(activationUrlRegex);
const url = location.origin + location.pathname;
return activationRegex.test(url);
}
2 changes: 1 addition & 1 deletion extension/src/content/Loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function forceSetupAndLoadViewer() {
const jsonText = tryFindJsonText();
if (!jsonText) {
console.warn(
"Virtual Json Viewer forceful activation failed: JSON not found",
"Virtual Json Viewer activation failed: JSON expected but not found",
);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion extension/src/options/components/ForceActivationSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useState,
} from "react";
import { Icon, IconLabel, Select } from "viewer/components";
import { DefaultSettings } from "viewer/state";

export type ForceActivationSelectProps = Props<{
urlRegex: Nullable<string>;
Expand Down Expand Up @@ -54,7 +55,7 @@ function regexToOption(urlRegex: Nullable<string>): Option {
}

function optionToDefaultRegex(option: Option): Nullable<string> {
return option === "custom" ? ".*" : null;
return option === "custom" ? DefaultSettings.activationUrlRegex : null;
}

type UrlRegexInputProps = Props<{
Expand Down
8 changes: 4 additions & 4 deletions extension/src/viewer/commons/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export interface Storage {
addListener<T>(key: string, onChange: OnItemChange<T>): RemoveListener;
}

class LocalStorage implements Storage {
class SessionStorage implements Storage {
get<T>(key: string): Promise<Nullable<T>> {
return new Promise((resolve, reject) => {
try {
const value = localStorage.getItem(key);
const value = sessionStorage.getItem(key);
resolve(value !== null ? JSON.parse(value) : null);
} catch (e) {
reject(e);
Expand All @@ -24,7 +24,7 @@ class LocalStorage implements Storage {
set<T>(key: string, item: T): Promise<void> {
return new Promise((resolve, reject) => {
try {
localStorage.setItem(key, JSON.stringify(item));
sessionStorage.setItem(key, JSON.stringify(item));
resolve();
} catch (e) {
reject(e);
Expand Down Expand Up @@ -79,4 +79,4 @@ class SyncStorage implements Storage {
}

export const STORAGE =
RUNTIME === Runtime.Extension ? new SyncStorage() : new LocalStorage();
RUNTIME === Runtime.Extension ? new SyncStorage() : new SessionStorage();
39 changes: 24 additions & 15 deletions extension/src/viewer/hooks/UseSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useSettings(): [Settings, DispatchSettings] {
);

const upgraded = maybeUpgrade(settings);
if (upgraded) {
if (!Object.is(settings, upgraded)) {
setSettings(upgraded);
}

Expand All @@ -25,22 +25,31 @@ export function useSettings(): [Settings, DispatchSettings] {
return [settings, updateSettings];
}

function maybeUpgrade(settings: Settings): Nullable<Settings> {
// changes in the same version are backward compatible
if (isLatestVersion(settings) && hasMissingKeys(settings)) {
return { ...DefaultSettings, ...settings };
}

// ...handle breaking changes

// nothing to do
return null;
// returns a different Settings object in case of upgrade
function maybeUpgrade(settings: Settings): Settings {
settings = addMissingKeys(settings);
settings = upgradeV1toV2(settings);
return settings;
}

function isLatestVersion(settings: Settings): boolean {
return settings.version == DefaultSettings.version;
// adding keys is always backward compatible
function addMissingKeys(settings: Settings): Settings {
const hasMissingKeys = Object.keys(DefaultSettings).some(
(key: string) => !(key in settings),
);
return hasMissingKeys ? { ...DefaultSettings, ...settings } : settings;
}

function hasMissingKeys(settings: Settings): boolean {
return Object.keys(DefaultSettings).some((key: string) => !(key in settings));
// V2: changed default value for activationUrlRegex
export function upgradeV1toV2(settings: Settings): Settings {
if (settings.version > 1) {
return settings;
}

settings = { ...settings, version: 2 };

// don't overwrite regex if user already set a custom one
settings.activationUrlRegex ??= "^.*(\\.jsonl?)$";

return settings;
}
5 changes: 3 additions & 2 deletions extension/src/viewer/state/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ const textSizeClasses: Record<TextSize, string> = {
/* Context */

export const DefaultSettings: Settings = {
version: 1,
version: 2,

activationUrlRegex: null,
// all URL paths ending with a json file extension
activationUrlRegex: "^.*(\\.jsonl?)$",
enableJQ: true,
expandNodes: false,
indentation: 4,
Expand Down