Skip to content

Commit

Permalink
fix edge case in migration; update manifest to version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ankit committed Jul 26, 2020
1 parent 7b32d58 commit 6aee993
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
22 changes: 21 additions & 1 deletion src/background/migrate/__tests__/migrate-old-format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('MigrateOldFormat', () => {
expect(mockSet).toHaveBeenCalledTimes(0);
});

it('returns styles as is when they are already in the new format', async () => {
it('returns styles as is when they are in the new format', async () => {
const mockSet = jest.fn((_, callback) => callback());
const mockGet = (_, callback) => {
callback({ styles: newFormat });
Expand All @@ -43,6 +43,26 @@ describe('MigrateOldFormat', () => {
expect(mockSet).toHaveBeenCalledTimes(0);
});

it('returns styles as is when they are in the new format (with empty css)', async () => {
const newFormatWithEmptyCss = {
'www.google.com': {
css: '',
enabled: false,
readability: false,
},
};

const mockSet = jest.fn((_, callback) => callback());
const mockGet = (_, callback) => {
callback({ styles: newFormatWithEmptyCss });
};
mockChromeAPI(mockGet, mockSet);

const styles = await getMigratedStyles();
expect(styles).toEqual(newFormatWithEmptyCss);
expect(mockSet).toHaveBeenCalledTimes(0);
});

it('backs up v2 styles to "backup_v2_styles"', async () => {
const mockSet = jest.fn((_, callback) => callback());
const mockGet = (_, callback) => {
Expand Down
24 changes: 12 additions & 12 deletions src/background/migrate/migrate-old-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Style, StyleMap, StylebotOptions } from '@stylebot/types';
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const isNewFormat = (styles: any) => {
const urls: Array<string> = Object.keys(styles);
return !!urls.find(url => !!styles[url].css);
return !!urls.find(url => styles[url].css !== undefined);
};

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
Expand All @@ -20,28 +20,28 @@ const backupV2Styles = (styles: any) => {
export const getMigratedStyles = (): Promise<StyleMap> => {
return new Promise(resolve => {
chrome.storage.local.get('styles', async items => {
const oldStyles = items['styles'];
const styles = items['styles'];

if (!oldStyles) {
if (!styles) {
resolve({});
return;
}

// test to ensure styles are not already in the new format
if (isNewFormat(oldStyles)) {
resolve(oldStyles);
// check if styles are in the new format
if (isNewFormat(styles)) {
resolve(styles);
return;
}

// backup old styles, in case we run into a bug
await backupV2Styles(oldStyles);
await backupV2Styles(styles);

const formatter = new LegacyCssFormatter();
const urls: Array<string> = Object.keys(oldStyles);
const urls: Array<string> = Object.keys(styles);

const results: Array<Promise<Style>> = urls.map(
async (url): Promise<Style> => {
const style = oldStyles[url];
const style = styles[url];

return new Promise(resolveStyle => {
try {
Expand All @@ -67,13 +67,13 @@ export const getMigratedStyles = (): Promise<StyleMap> => {
);

Promise.all(results).then(formattedStyles => {
const styles: StyleMap = {};
const newStyles: StyleMap = {};

formattedStyles.forEach(({ url, css, enabled, readability }) => {
styles[url] = { css, enabled, readability };
newStyles[url] = { css, enabled, readability };
});

resolve(styles);
resolve(newStyles);
});
});
});
Expand Down
1 change: 1 addition & 0 deletions src/editor/components/header/TheDeleteStyleDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default Vue.extend({
height: 100%;
overflow: hidden;
outline: 0;
line-height: 24px;
background: #000000b3;
}
Expand Down
4 changes: 2 additions & 2 deletions src/extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"manifest_version": 2,
"name": "Stylebot",
"version": "2.4.1",
"description": "Change the appearance of websites instantly.",
"version": "3",
"description": "Change the appearance of the web instantly",

"background": {
"scripts": ["background/index.js"]
Expand Down

0 comments on commit 6aee993

Please sign in to comment.