Skip to content

Commit

Permalink
Fixed extension auto update from code. (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmesh-hemaram authored Jan 1, 2024
1 parent fe7dd46 commit d4f94e3
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 120 deletions.
33 changes: 0 additions & 33 deletions apps/acf-extension/src/background/check-blog.ts

This file was deleted.

32 changes: 6 additions & 26 deletions apps/acf-extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ import DiscordOauth2 from './discord-oauth2';
import GoogleSheets from './google-sheets';
import GoogleBackup from './google-backup';
import { TabsMessenger } from './tab';
import { Blog } from './check-blog';
import { ACTION_POPUP } from '../common/constant';
import { OPTIONS_PAGE_URL, UNINSTALL_URL } from '../common/environments';
import GoogleOauth2 from './google-oauth2';
import DiscordMessaging from './discord-messaging';
import { sentryInit } from '../common/sentry';

const EXTENSION_VERSION = 'extension-version';

try {
sentryInit('background');

Expand All @@ -30,8 +27,12 @@ try {
/**
* On initial install setup basic configuration
*/
chrome.runtime.onInstalled.addListener(() => {
TabsMessenger.optionsTab({ url: OPTIONS_PAGE_URL });
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'update') {
TabsMessenger.optionsTab({ url: `${OPTIONS_PAGE_URL}?version=${details.previousVersion}` });
} else if (details.reason === 'install') {
TabsMessenger.optionsTab({ url: OPTIONS_PAGE_URL });
}
});

/**
Expand All @@ -51,27 +52,6 @@ try {
chrome.runtime.setUninstallURL(UNINSTALL_URL);
}

/**
* On start up check for rate
*/
chrome.runtime.onStartup.addListener(() => {
chrome.storage.local.get(EXTENSION_VERSION).then(({ [EXTENSION_VERSION]: version }) => {
const manifestVersion = chrome.runtime.getManifest().version;
if (version !== undefined && version !== manifestVersion) {
chrome.storage.local.remove(EXTENSION_VERSION);
chrome.runtime.reload();
}
});
Blog.check(OPTIONS_PAGE_URL);
});

/**
* If an update is available it will auto update
*/
chrome.runtime.onUpdateAvailable.addListener(({ version }) => {
chrome.storage.local.set({ [EXTENSION_VERSION]: version });
});

/**
* Setup on Message Listener
*/
Expand Down
2 changes: 0 additions & 2 deletions apps/acf-extension/src/common/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const sentryInit = (page: string) => {
'canvas.contentDocument',
'MyApp_RemoveAllHighlights',
'http://tt.epicplay.com',
'Extension context invalidated.',
"Can't find variable: ZiteReader",
'jigsaw is not defined',
'ComboSearch is not defined',
Expand All @@ -44,7 +43,6 @@ export const sentryInit = (page: string) => {
'EBCallBackMessageReceived',
// See http://toolbar.conduit.com/Developer/HtmlAndGadget/Methods/JSInjection.aspx
'conduitPage',
'Could not establish connection. Receiving end does not exist.',
'Non-Error promise rejection captured',
],
denyUrls: [
Expand Down
51 changes: 26 additions & 25 deletions apps/acf-extension/src/content_scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ const Common = (() => {
const getElements = async (document: Document, elementFinder: string, retry: number, retryInterval: number | string): Promise<Array<HTMLElement> | undefined> => {
Logger.colorDebug('GetElements', elementFinder);
let elements: HTMLElement[] | undefined;
if (/^(id::|#)/gi.test(elementFinder)) {
const element = document.getElementById(elementFinder.replace(/^(id::|#)/gi, ''));
elements = element ? [element] : undefined;
} else if (/^Selector::/gi.test(elementFinder)) {
const element = document.querySelector<HTMLElement>(elementFinder.replace(/^Selector::/gi, ''));
elements = element ? [element] : undefined;
} else if (/^ClassName::/gi.test(elementFinder)) {
const classElements = document.getElementsByClassName(elementFinder.replace(/^ClassName::/gi, '')) as HTMLCollectionOf<HTMLElement>;
elements = classElements.length !== 0 ? Array.from(classElements) : undefined;
} else if (/^Name::/gi.test(elementFinder)) {
const nameElements = document.getElementsByName(elementFinder.replace(/^Name::/gi, ''));
elements = nameElements.length !== 0 ? Array.from(nameElements) : undefined;
} else if (/^TagName::/gi.test(elementFinder)) {
const tagElements = document.getElementsByTagName(elementFinder.replace(/^TagName::/gi, '')) as HTMLCollectionOf<HTMLElement>;
elements = tagElements.length !== 0 ? Array.from(tagElements) : undefined;
} else if (/^SelectorAll::/gi.test(elementFinder)) {
const querySelectAll = document.querySelectorAll<HTMLElement>(elementFinder.replace(/^SelectorAll::/gi, ''));
elements = querySelectAll.length !== 0 ? Array.from(querySelectAll) : undefined;
} else {
try {
try {
if (/^(id::|#)/gi.test(elementFinder)) {
const element = document.getElementById(elementFinder.replace(/^(id::|#)/gi, ''));
elements = element ? [element] : undefined;
} else if (/^Selector::/gi.test(elementFinder)) {
const element = document.querySelector<HTMLElement>(elementFinder.replace(/^Selector::/gi, ''));
elements = element ? [element] : undefined;
} else if (/^ClassName::/gi.test(elementFinder)) {
const classElements = document.getElementsByClassName(elementFinder.replace(/^ClassName::/gi, '')) as HTMLCollectionOf<HTMLElement>;
elements = classElements.length !== 0 ? Array.from(classElements) : undefined;
} else if (/^Name::/gi.test(elementFinder)) {
const nameElements = document.getElementsByName(elementFinder.replace(/^Name::/gi, ''));
elements = nameElements.length !== 0 ? Array.from(nameElements) : undefined;
} else if (/^TagName::/gi.test(elementFinder)) {
const tagElements = document.getElementsByTagName(elementFinder.replace(/^TagName::/gi, '')) as HTMLCollectionOf<HTMLElement>;
elements = tagElements.length !== 0 ? Array.from(tagElements) : undefined;
} else if (/^SelectorAll::/gi.test(elementFinder)) {
const querySelectAll = document.querySelectorAll<HTMLElement>(elementFinder.replace(/^SelectorAll::/gi, ''));
elements = querySelectAll.length !== 0 ? Array.from(querySelectAll) : undefined;
} else {
const nodes = document.evaluate(elementFinder, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (nodes.snapshotLength !== 0) {
elements = [];
Expand All @@ -68,13 +68,14 @@ const Common = (() => {
i += 1;
}
}
} catch (e) {
if (e instanceof Error) {
throw new ConfigError(`elementFinder: ${e.message.split(':')[1]}`, 'Invalid Xpath');
}
throw new ConfigError(`elementFinder: ${JSON.stringify(e)}`, 'Invalid Xpath');
}
} catch (e) {
if (e instanceof Error) {
throw new ConfigError(e.message, 'Invalid Xpath');
}
throw new ConfigError(JSON.stringify(e), 'Invalid Xpath');
}

if (!elements) {
const doRetry = await retryFunc(retry, retryInterval);
if (doRetry) {
Expand Down
24 changes: 11 additions & 13 deletions apps/acf-extension/src/content_scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ const ConfigProcessor = (() => {
const error = { title: e.title, message: `url : ${config.url}\n${e.message}` };
setBadgeError();
const { notifications } = await new SettingsStorage().getSettings();
if (notifications) {
const { onError, sound, discord } = notifications;
if (onError) {
NotificationsService.create(chrome.runtime.id, { type: 'basic', ...error, silent: !sound, iconUrl: Common.getNotificationIcon() }, 'error');
if (discord) {
DiscordMessagingService.error(chrome.runtime.id, e.title || 'Configuration Error', [
...getFields(config),
...e.message.split('\n').map((info) => {
const [name, value] = info.split(':');
return { name, value: value.replace(/'/g, '`') };
}),
]);
}
if (notifications && notifications.onError) {
const { sound, discord } = notifications;
NotificationsService.create(chrome.runtime.id, { type: 'basic', ...error, silent: !sound, iconUrl: Common.getNotificationIcon() }, 'error');
if (discord) {
DiscordMessagingService.error(chrome.runtime.id, e.title || 'Configuration Error', [
...getFields(config),
...e.message.split('\n').map((info) => {
const [name, value] = info.split(':');
return { name, value: value.replace(/'/g, '`') };
}),
]);
}
} else {
console.error(error.title, '\n', error.message);
Expand Down
24 changes: 15 additions & 9 deletions apps/acf-extension/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,27 @@ module.exports = composePlugins(withNx(), (config, ctx) => {
},
],
});

config.plugins.push(
new Dotenv({
path: config.watch ? path.resolve(config.context, '.env') : './.env',
safe: true,
systemvars: true,
}),
sentryWebpackPlugin({
org: 'dhruv-techapps',
project: 'acf-extension',
telemetry: false,
authToken: process.env.SENTRY_AUTH_TOKEN,
release: {
name: process.env.NX_RELEASE_VERSION?.replace('v', ''),
},
})
);

if (config.mode === 'development') {
config.plugins.push(
sentryWebpackPlugin({
org: 'dhruv-techapps',
project: 'acf-extension',
telemetry: false,
authToken: process.env.SENTRY_AUTH_TOKEN,
release: {
name: process.env.NX_RELEASE_VERSION?.replace('v', ''),
},
})
);
}
return config;
});
5 changes: 4 additions & 1 deletion apps/acf-options-page/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { sentryInit } from './sentry';

window.EXTENSION_ID = process.env[`NX_${BROWSER}_EXTENSION_ID`] ?? '';

sentryInit();
if (process.env.NODE_ENV !== 'development') {
sentryInit();
}

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
<Provider store={store}>
Expand Down
2 changes: 1 addition & 1 deletion apps/acf-options-page/src/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const sentryInit = () => {
Sentry.init({
dsn: 'https://aacf1f88c133d2c9b4823c4c0b485ecc@o4506036997455872.ingest.sentry.io/4506037000994816',
release,
ignoreErrors: ['NetFunnel is not defined', 'adsbygoogle.push() error: No slot size for availableWidth=0'],
ignoreErrors: ['NetFunnel is not defined', 'adsbygoogle.push() error: No slot size for availableWidth=0', 'ResizeObserver loop completed with undelivered notifications.'],
environment: process.env.NX_VARIANT,
// This sets the sample rate to be 10%. You may want this to be 100% while
// in development and sample at a lower rate in production
Expand Down
22 changes: 12 additions & 10 deletions apps/acf-options-page/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ const { withReact } = require('@nx/react');
module.exports = composePlugins(withNx(), withReact(), (config) => {
// Update the webpack config as needed here.
// e.g. `config.plugins.push(new MyPlugin())`
config.plugins.push(
sentryWebpackPlugin({
org: 'dhruv-techapps',
project: 'acf-options-page',
authToken: process.env.SENTRY_AUTH_TOKEN,
release: {
name: process.env.NX_RELEASE_VERSION,
},
})
);
if (config.mode !== 'development') {
config.plugins.push(
sentryWebpackPlugin({
org: 'dhruv-techapps',
project: 'acf-options-page',
authToken: process.env.SENTRY_AUTH_TOKEN,
release: {
name: process.env.NX_RELEASE_VERSION,
},
})
);
}
return config;
});

0 comments on commit d4f94e3

Please sign in to comment.