Skip to content

Commit

Permalink
added crx helpers to core
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Feb 12, 2025
1 parent 20c94f7 commit f34ef41
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { WebExtensionMessage } from 'altair-graphql-core/build/types/messaging';
import { WindowService } from '../window.service';
import { debug } from '../../utils/logger';
import { isExtension } from '../../utils';
import { isExtension, sendMessage } from 'altair-graphql-core/build/crx';

@Injectable({
providedIn: 'root',
Expand All @@ -27,7 +27,7 @@ export class WebExtensionsService {
return;
}
case 'ping': {
browser.runtime.sendMessage({ type: 'pong' });
sendMessage({ type: 'pong' });
return;
}
default: {
Expand All @@ -38,7 +38,6 @@ export class WebExtensionsService {
}
);

const readyMessage: WebExtensionMessage = { type: 'ready' };
browser.runtime.sendMessage(readyMessage);
sendMessage({ type: 'ready' });
}
}
2 changes: 2 additions & 0 deletions packages/altair-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
"@jedmao/location": "^3.0.0",
"@jest/globals": "^29.7.0",
"@types/actioncable": "^5.2.5",
"@types/chrome": "^0.0.209",
"@types/color-name": "^1.1.4",
"@types/crypto-js": "^4.0.1",
"@types/firefox-webext-browser": "^109.0.0",
"@types/jest": "^29.5.12",
"@types/json-bigint": "^1.0.1",
"@types/node": "^14.14.41",
Expand Down
17 changes: 17 additions & 0 deletions packages/altair-core/src/crx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { WebExtensionMessage } from '../types/messaging';

const browser = window.chrome || window.browser;
export const isExtension = !!browser?.runtime?.id;
export const isFirefoxExtension = location.protocol === 'moz-extension:';

export const sendMessage = (message: WebExtensionMessage) => {
const browser = window.chrome || window.browser;
if (!isExtension) {
// eslint-disable-next-line prettier/prettier, no-console
console.log('Not running in an extension');
// eslint-disable-next-line prettier/prettier, no-console
console.log('Message:', message);
return;
}
browser.runtime.sendMessage(message);
};
2 changes: 1 addition & 1 deletion packages/altair-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"module": "ES2020",
"moduleResolution": "node",
"lib": ["dom", "esnext"],
"types": ["node"],
// "types": ["node", "chrome"],
"noImplicitAny": true,
"skipLibCheck": true,
"esModuleInterop": true,
Expand Down
1 change: 1 addition & 0 deletions packages/altair-crx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"devDependencies": {
"@types/adm-zip": "^0.5.7",
"@types/chrome": "^0.0.209",
"@types/firefox-webext-browser": "^109.0.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@vitejs/plugin-react": "^1.3.0",
Expand Down
15 changes: 2 additions & 13 deletions packages/altair-crx/src/helpers/messaging.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { ExportWindowState } from 'altair-graphql-core/build/types/state/window.interfaces';
import { WebExtensionMessage } from 'altair-graphql-core/build/types/messaging';
import { isExtension, sendMessage } from 'altair-graphql-core/build/crx';
import { openAltairApp } from './tabs';

export const isExtension = () => {
return !!(window.chrome || window.browser)?.runtime?.id;
};
export const sendMessage = (message: WebExtensionMessage) => {
const browser = window.chrome || window.browser;
if (!isExtension()) {
console.log('Not running in an extension');
console.log('Message:', message);
return;
}
browser.runtime.sendMessage(message);
};
export const openInAltair = (data: ExportWindowState) => {
const browser = window.chrome || window.browser;
if (!isExtension()) {
if (!isExtension) {
console.log('Not running in an extension');
console.log('Data:', data);
return;
Expand Down
8 changes: 4 additions & 4 deletions packages/altair-crx/src/hooks/requests/use-requests.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useEffect, useState } from 'react';
import { isExtension } from 'altair-graphql-core/build/crx';
import { mockRequests } from './mock-requests';
import { getRequest } from '../../helpers/request';
import { GraphQLRequest } from '../../types';
import { isExtension } from '../../helpers/messaging';

const useCurrentTab = () => {
const [tabId, setTabId] = useState<number | undefined>(undefined);
const browser = window.chrome || window.browser;

if (!isExtension()) {
if (!isExtension) {
return;
}
browser.tabs.query({ active: true, currentWindow: true }, (tabs) => {
Expand All @@ -29,7 +29,7 @@ const useTabStatus = () => {
);
const browser = window.chrome || window.browser;

if (!isExtension()) {
if (!isExtension) {
return tabStatus;
}
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
Expand All @@ -55,7 +55,7 @@ export const useGraphQLRequests = () => {
}
}, [tabStatus]);

if (!isExtension()) {
if (!isExtension) {
return {
requests: mockRequests,
};
Expand Down
49 changes: 29 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f34ef41

Please sign in to comment.