Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions apps/acf-extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ try {
[RUNTIME_MESSAGE_OPENAI]: new OpenAIBackground(auth, FIREBASE_FUNCTIONS_URL, EDGE_OAUTH_CLIENT_ID)
};
Runtime.onMessageExternal(onMessageListener);

Runtime.onMessage(onMessageListener);
Runtime.onConnect(onMessageListener);

auth.authStateReady().then(() => {
const clientId = auth.currentUser?.uid;
Expand Down
5 changes: 2 additions & 3 deletions apps/acf-extension/src/content_scripts/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const BatchProcessor = (() => {
statusBar.batchUpdate(i + 2);
console.groupCollapsed(`${BATCH_I18N.TITLE} #${i + 2} [${I18N_COMMON.REPEAT}]`);
if (batch?.repeatInterval) {
await statusBar.wait(batch?.repeatInterval, STATUS_BAR_TYPE.BATCH_REPEAT, i + 2);
await statusBar.wait(batch?.repeatInterval, STATUS_BAR_TYPE.BATCH_REPEAT);
}
await Actions.start(actions, i + 2);
const { notifications } = await new SettingsStorage().getSettings();
Expand All @@ -50,7 +50,7 @@ const BatchProcessor = (() => {
while (true) {
if (batch?.repeatInterval) {
statusBar.batchUpdate('∞');
await statusBar.wait(batch?.repeatInterval, STATUS_BAR_TYPE.BATCH_REPEAT, '∞');
await statusBar.wait(batch?.repeatInterval, STATUS_BAR_TYPE.BATCH_REPEAT);
}
await Actions.start(actions, i);
i += 1;
Expand All @@ -61,7 +61,6 @@ const BatchProcessor = (() => {

const start = async (actions: Array<IAction | IUserScript>, batch?: IBatch) => {
try {
statusBar.batchUpdate(1);
console.groupCollapsed(`${BATCH_I18N.TITLE} #1 (${I18N_COMMON.DEFAULT})`);
await Actions.start(actions, 1);
console.groupEnd();
Expand Down
7 changes: 1 addition & 6 deletions apps/acf-extension/src/content_scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,12 @@ const ConfigProcessor = (() => {
}
};

const setupStatusBar = async () => {
const { statusBar: statusBarLocation } = await new SettingsStorage().getSettings();
statusBar.setLocation(statusBarLocation);
};

const checkStartType = async (configs: Array<IConfiguration>, config?: IConfiguration) => {
setupStatusBar();
configs.forEach((c) => {
Hotkey.setup(start.bind(this, c), c.hotkey);
});
if (config) {
statusBar.enable(config.actions.length, config.batch?.repeat);
await checkStartTime(config);
await start(config);
}
Expand Down
2 changes: 2 additions & 0 deletions apps/acf-extension/src/content_scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ async function loadConfig(loadType: ELoadTypes) {
}
} else if (manualConfigs.length > 0 && loadType === ELoadTypes.DOCUMENT) {
await ConfigProcessor.checkStartType(manualConfigs);
} else {
statusBar.disable();
}
});
} catch (e) {
Expand Down
10 changes: 9 additions & 1 deletion apps/acf-extension/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
"128": "assets/icons/icon128.png"
},
"action": {
"default_icon": "assets/icons/icon64.png"
"default_icon": {
"16": "assets/icons/icon16.png",
"32": "assets/icons/icon32.png",
"48": "assets/icons/icon48.png",
"64": "assets/icons/icon64.png",
"96": "assets/icons/icon96.png",
"128": "assets/icons/icon128.png"
},
"default_title": "↗ Options Page"
},
"background": {
"service_worker": "background.js",
Expand Down
1 change: 1 addition & 0 deletions packages/core/common/src/lib/model/core-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[index: string]: {
startRange: string;
endRange: string;
values: Array<any>;

Check warning on line 5 in packages/core/common/src/lib/model/core-model.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
};
}

Expand All @@ -11,6 +11,7 @@
__currentActionName: string;
__actionError: string;
__actionRepeat: number;
__addonRecheck: number;
__batchRepeat: number;
__sessionCount: number;
__sheets?: ISheets;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
export interface ActionRequest {
messenger: 'action';
methodName: 'setIcon' | 'setBadgeBackgroundColor' | 'setBadgeText' | 'setTitle';
methodName: 'setIcon' | 'setBadgeBackgroundColor' | 'setBadgeText' | 'setTitle' | 'setBadgeTextColor' | 'enable' | 'disable';
message: chrome.action.TabIconDetails | chrome.action.BadgeColorDetails | chrome.action.BadgeTextDetails | chrome.action.TitleDetails;
}

export class ActionMessenger {
setIcon(details: chrome.action.TabIconDetails) {
return chrome.action.setIcon(details);
disable(sender: chrome.runtime.MessageSender) {
return chrome.action.disable(sender.tab?.id);
}

setBadgeBackgroundColor(details: chrome.action.BadgeColorDetails) {
return chrome.action.setBadgeBackgroundColor(details);
enable(sender: chrome.runtime.MessageSender) {
return chrome.action.enable(sender.tab?.id);
}

setBadgeText(details: chrome.action.BadgeTextDetails) {
return chrome.action.setBadgeText(details);
setIcon(details: chrome.action.TabIconDetails, sender: chrome.runtime.MessageSender) {
return chrome.action.setIcon({ ...details, tabId: sender.tab?.id });
}

setTitle(details: chrome.action.TitleDetails) {
return chrome.action.setTitle(details);
setBadgeBackgroundColor(details: chrome.action.BadgeColorDetails, sender: chrome.runtime.MessageSender) {
return chrome.action.setBadgeBackgroundColor({ ...details, tabId: sender.tab?.id });
}

setBadgeText(details: chrome.action.BadgeTextDetails, sender: chrome.runtime.MessageSender) {
return chrome.action.setBadgeText({ ...details, tabId: sender.tab?.id });
}

setBadgeTextColor(details: chrome.action.BadgeColorDetails, sender: chrome.runtime.MessageSender) {
return chrome.action.setBadgeTextColor({ ...details, tabId: sender.tab?.id });
}

setTitle(details: chrome.action.TitleDetails, sender: chrome.runtime.MessageSender) {
return chrome.action.setTitle({ ...details, tabId: sender.tab?.id });
}
}
22 changes: 21 additions & 1 deletion packages/core/extension/src/lib/background/chrome/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
case 'manifest':
return new ManifestMessenger()[(methodName as keyof ManifestMessenger) || 'values'](message);
case 'action':
return new ActionMessenger()[(methodName as keyof ActionMessenger) || 'setIcon'](message);
return new ActionMessenger()[(methodName as keyof ActionMessenger) || 'setIcon'](message, sender);
case 'alarms':
return new AlarmsMessenger()[(methodName as keyof AlarmsMessenger) || 'create'](message);
case 'userScripts':
Expand Down Expand Up @@ -49,6 +49,26 @@
chrome.runtime.sendMessage(message, callback);
}

static onConnect(configs: MessengerConfigObject) {
chrome.runtime.onConnect.addListener((port) => {
const { sender = {}, name } = port;
const tabId = sender.tab?.id;
if (!tabId) {
port.postMessage({ error: 'no tab id' });
port.disconnect();
return;
}
console.log('port connected', name);
port.onMessage.addListener((msg) => {
messageListener(msg, sender, configs);
});
});
}

static onConnectExternal(configs: MessengerConfigObject) {

Check warning on line 68 in packages/core/extension/src/lib/background/chrome/runtime.ts

View workflow job for this annotation

GitHub Actions / Check

'configs' is defined but never used

Check warning on line 68 in packages/core/extension/src/lib/background/chrome/runtime.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/core/extension/src/lib/background/chrome/runtime.ts#L68

'configs' is defined but never used.
//TODO
}

static onMessage(configs: MessengerConfigObject) {
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
messageListener(request, sender, configs)
Expand Down
32 changes: 22 additions & 10 deletions packages/core/service/src/lib/action.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { ActionRequest } from '@dhruv-techapps/core-extension';
import { CoreService } from './service';
import { PortService } from './service';

export class ActionService extends CoreService {
static async setBadgeBackgroundColor(details: chrome.action.BadgeColorDetails) {
return await this.message<ActionRequest>({ messenger: 'action', methodName: 'setBadgeBackgroundColor', message: details });
export class ActionService {
static setBadgeBackgroundColor(details: chrome.action.BadgeColorDetails) {
PortService.getInstance().message<ActionRequest>({ messenger: 'action', methodName: 'setBadgeBackgroundColor', message: details });
}

static async setBadgeText(details: chrome.action.BadgeTextDetails) {
return await this.message<ActionRequest>({ messenger: 'action', methodName: 'setBadgeText', message: details });
static setBadgeText(details: chrome.action.BadgeTextDetails) {
PortService.getInstance().message<ActionRequest>({ messenger: 'action', methodName: 'setBadgeText', message: details });
}

static async setIcon(details: chrome.action.TabIconDetails) {
return await this.message<ActionRequest>({ messenger: 'action', methodName: 'setIcon', message: details });
static setIcon(details: chrome.action.TabIconDetails) {
PortService.getInstance().message<ActionRequest>({ messenger: 'action', methodName: 'setIcon', message: details });
}

static async setTitle(details: chrome.action.TitleDetails) {
return await this.message<ActionRequest>({ messenger: 'action', methodName: 'setTitle', message: details });
static setTitle(details: chrome.action.TitleDetails) {
PortService.getInstance().message<ActionRequest>({ messenger: 'action', methodName: 'setTitle', message: details });
}

static setBadgeTextColor(details: chrome.action.BadgeColorDetails) {
PortService.getInstance().message<ActionRequest>({ messenger: 'action', methodName: 'setBadgeTextColor', message: details });
}

static enable(tabId?: number) {
PortService.getInstance().message<ActionRequest>({ messenger: 'action', methodName: 'enable', message: { tabId } });
}

static disable(tabId?: number) {
PortService.getInstance().message<ActionRequest>({ messenger: 'action', methodName: 'disable', message: { tabId } });
}
}
56 changes: 56 additions & 0 deletions packages/core/service/src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,59 @@
return await this.messageChrome<K, T>(message);
}
}

export class PortService {
private static instance?: PortService;
private readonly port: chrome.runtime.Port;
private readonly portName: string;

private constructor(name: string = 'Auto Clicker & Auto Fill') {

Check failure on line 53 in packages/core/service/src/lib/service.ts

View workflow job for this annotation

GitHub Actions / Check

Type string trivially inferred from a string literal, remove type annotation

Check failure on line 53 in packages/core/service/src/lib/service.ts

View workflow job for this annotation

GitHub Actions / Check

Type string trivially inferred from a string literal, remove type annotation

Check notice on line 53 in packages/core/service/src/lib/service.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/core/service/src/lib/service.ts#L53

Type string trivially inferred from a string literal, remove type annotation.
if (!chrome.runtime?.connect) {
throw new Error('Extension context invalidated');
}

const id = chrome.runtime.id || window.EXTENSION_ID;
if (!id || typeof id !== 'string') {
throw new Error('extensionId is not undefined neither string');
}

this.portName = name;
this.port = chrome.runtime.connect(id, { name: this.portName });
}

static getInstance(name?: string): PortService {
if (!PortService.instance) {
PortService.instance = new PortService(name);
return PortService.instance;
}

if (name && PortService.instance.portName !== name) {
console.warn(`PortService already initialized with name "${PortService.instance.portName}". Ignoring request for "${name}".`);
}

return PortService.instance;
}

private postMessage(message: unknown): void {
this.port.postMessage(message);
}

public onMessage(listener: (message: unknown) => void): void {
if (this.port.onMessage.hasListener(listener)) {
return;
}
this.port.onMessage.addListener(listener);
}

public disconnect(): void {
this.port.disconnect();
}

public onDisconnect(listener: () => void): void {
this.port.onDisconnect.addListener(listener);
}

public message<K extends CoreServiceRequest>(message: K): void {
this.postMessage(message);
}
}
2 changes: 2 additions & 0 deletions packages/shared/status-bar/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/// <reference types="chrome"/>

export * from './lib/status-bar';
export * from './lib/status-bar.types';
Loading
Loading