From b90636b4772a03007c94643b8b6917712eedaeda Mon Sep 17 00:00:00 2001 From: ujiro99 Date: Fri, 16 May 2025 12:49:28 +0900 Subject: [PATCH 1/3] Update: Enables counting command execution counts when using tabs, windows, and page actions. --- src/background_script.ts | 72 +++++++-------------------- src/const.ts | 11 ++++ src/services/commandMetrics.ts | 32 ++++++++++++ src/services/pageAction/background.ts | 3 ++ 4 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 src/services/commandMetrics.ts diff --git a/src/background_script.ts b/src/background_script.ts index 94985751..ea4ca984 100644 --- a/src/background_script.ts +++ b/src/background_script.ts @@ -1,4 +1,4 @@ -import { isDebug, LINK_COMMAND_ENABLED, POPUP_ENABLED } from '@/const' +import { isDebug, LINK_COMMAND_ENABLED, POPUP_ENABLED, OPTION_PAGE_PATH } from '@/const' import '@/services/contextMenus' import { Ipc, BgCommand, TabCommand } from '@/services/ipc' import { Settings } from '@/services/settings' @@ -7,6 +7,7 @@ import * as PageActionBackground from '@/services/pageAction/background' import { openPopups, OpenPopupsProps, getCurrentTab } from '@/services/chrome' import { BgData } from '@/services/backgroundData' import { escapeJson, isSearchCommand, isPageActionCommand } from '@/lib/utils' +import { incrementCommandExecutionCount } from '@/services/commandMetrics' import type { IpcCallback } from '@/services/ipc' import type { CommandVariable, @@ -18,45 +19,8 @@ import type { } from '@/types' import { Storage, SESSION_STORAGE_KEY } from './services/storage' -const CONSTANTS = { - OPTION_PAGE: 'src/options_page.html', - REVIEW_THRESHOLD: 100, - REVIEW_INTERVAL: 50, - SETTING_KEY: { - COMMAND_EXECUTION_COUNT: 'commandExecutionCount', - HAS_SHOWN_REVIEW_REQUEST: 'hasShownReviewRequest', - }, -} as const - BgData.init() -// Increment command execution count and check review request -const incrementCommandExecutionCount = async (): Promise => { - try { - const settings = await Settings.get() - let count = settings.commandExecutionCount ?? 0 - const hasShown = settings.hasShownReviewRequest ?? false - - // Increment command execution count - count++ - await Settings.update( - CONSTANTS.SETTING_KEY.COMMAND_EXECUTION_COUNT, - () => count, - true, - ) - - // Show review request when threshold is exceeded - if ((count === CONSTANTS.REVIEW_THRESHOLD || (count > CONSTANTS.REVIEW_THRESHOLD && (count - CONSTANTS.REVIEW_THRESHOLD) % CONSTANTS.REVIEW_INTERVAL === 0)) && !hasShown) { - const tabs = await chrome.tabs.query({ active: true }) - if (tabs[0]?.id) { - await Ipc.sendTab(tabs[0].id, TabCommand.showReviewRequest) - } - } - } catch (error) { - console.error('Failed to increment command execution count:', error) - } -} - type Sender = chrome.runtime.MessageSender export type openPopupAndClickProps = OpenPopupsProps & { @@ -144,9 +108,22 @@ const commandFuncs = { return true }, + [BgCommand.openTab]: (param: openTabProps, sender: Sender, response: (res: unknown) => void): boolean => { + incrementCommandExecutionCount().then(async () => { + const tab = sender.tab ?? (await getCurrentTab()) + chrome.tabs.create({ + ...param, + windowId: tab.windowId, + index: tab.index + 1, + }) + response(true) + }) + return true + }, + [BgCommand.openOption]: (): boolean => { chrome.tabs.create({ - url: CONSTANTS.OPTION_PAGE, + url: OPTION_PAGE_PATH, }) return false }, @@ -168,7 +145,7 @@ const commandFuncs = { pageRules, }, true) chrome.tabs.create({ - url: `${CONSTANTS.OPTION_PAGE}#pageRules`, + url: `${OPTION_PAGE_PATH}#pageRules`, }) } add() @@ -218,19 +195,6 @@ const commandFuncs = { return true }, - [BgCommand.openTab]: (param: openTabProps, sender: Sender): boolean => { - const open = async () => { - const tab = sender.tab ?? (await getCurrentTab()) - chrome.tabs.create({ - ...param, - windowId: tab.windowId, - index: tab.index + 1, - }) - } - open() - return false - }, - [BgCommand.execApi]: ( param: execApiProps, _: Sender, @@ -445,7 +409,7 @@ const updateWindowSize = async ( chrome.action.onClicked.addListener(() => { chrome.tabs.create({ - url: CONSTANTS.OPTION_PAGE, + url: OPTION_PAGE_PATH, }) }) diff --git a/src/const.ts b/src/const.ts index 9dc81e26..146015b2 100644 --- a/src/const.ts +++ b/src/const.ts @@ -187,3 +187,14 @@ export const HUB_URL = isDebug export const PAGE_ACTION_MAX = 12 // 10 actions + 1 start + 1 end export const PAGE_ACTION_TIMEOUT = 5000 + +export const OPTION_PAGE_PATH = 'src/options_page.html' + +export const COMMAND_USAGE = { + REVIEW_THRESHOLD: 100, + REVIEW_INTERVAL: 50, + SETTING_KEY: { + COMMAND_EXECUTION_COUNT: 'commandExecutionCount', + HAS_SHOWN_REVIEW_REQUEST: 'hasShownReviewRequest', + }, +} as const diff --git a/src/services/commandMetrics.ts b/src/services/commandMetrics.ts new file mode 100644 index 00000000..707c394a --- /dev/null +++ b/src/services/commandMetrics.ts @@ -0,0 +1,32 @@ +import { Settings } from '@/services/settings' +import { Ipc, TabCommand } from '@/services/ipc' +import { COMMAND_USAGE } from '@/const' + +/** + * Increment command execution count and check review request + */ +export const incrementCommandExecutionCount = async (): Promise => { + try { + const settings = await Settings.get() + let count = settings.commandExecutionCount ?? 0 + const hasShown = settings.hasShownReviewRequest ?? false + + // Increment command execution count + count++ + await Settings.update( + COMMAND_USAGE.SETTING_KEY.COMMAND_EXECUTION_COUNT, + () => count, + true, + ) + + // Show review request when threshold is exceeded + if ((count === COMMAND_USAGE.REVIEW_THRESHOLD || (count > COMMAND_USAGE.REVIEW_THRESHOLD && (count - COMMAND_USAGE.REVIEW_THRESHOLD) % COMMAND_USAGE.REVIEW_INTERVAL === 0)) && !hasShown) { + const tabs = await chrome.tabs.query({ active: true }) + if (tabs[0]?.id) { + await Ipc.sendTab(tabs[0].id, TabCommand.showReviewRequest) + } + } + } catch (error) { + console.error('Failed to increment command execution count:', error) + } +} \ No newline at end of file diff --git a/src/services/pageAction/background.ts b/src/services/pageAction/background.ts index a441ad9a..aa8912d2 100644 --- a/src/services/pageAction/background.ts +++ b/src/services/pageAction/background.ts @@ -10,6 +10,7 @@ import type { PageAction } from '@/services/pageAction' import { RunningStatus } from '@/services/pageAction' import { ScreenSize } from '@/services/dom' import { openPopups, OpenPopupsProps, getCurrentTab } from '@/services/chrome' +import { incrementCommandExecutionCount } from '@/services/commandMetrics' import { POPUP_TYPE, PAGE_ACTION_MAX, @@ -273,6 +274,8 @@ export const openAndRun = ( response: (res: unknown) => void, ): boolean => { const open = async () => { + await incrementCommandExecutionCount() + let tabId: number if (param.openMode === PAGE_ACTION_OPEN_MODE.POPUP) { From bf7c946c6793f757818687b2e767bcb00579b4bb Mon Sep 17 00:00:00 2001 From: ujiro99 Date: Fri, 16 May 2025 22:02:34 +0900 Subject: [PATCH 2/3] Update: When a tab is opened, review requests will be displayed in the corresponding tab. --- src/background_script.ts | 13 ++++++++----- src/services/commandMetrics.ts | 12 ++++++++---- src/services/pageAction/background.ts | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/background_script.ts b/src/background_script.ts index ea4ca984..a1c9cd97 100644 --- a/src/background_script.ts +++ b/src/background_script.ts @@ -109,14 +109,17 @@ const commandFuncs = { }, [BgCommand.openTab]: (param: openTabProps, sender: Sender, response: (res: unknown) => void): boolean => { - incrementCommandExecutionCount().then(async () => { - const tab = sender.tab ?? (await getCurrentTab()) + getCurrentTab().then(tab => { + const currentTab = sender.tab ?? tab chrome.tabs.create({ ...param, - windowId: tab.windowId, - index: tab.index + 1, + windowId: currentTab.windowId, + index: currentTab.index + 1, + }, (newTab) => { + incrementCommandExecutionCount(newTab.id).then(() => { + response(true) + }) }) - response(true) }) return true }, diff --git a/src/services/commandMetrics.ts b/src/services/commandMetrics.ts index 707c394a..b4ff19e8 100644 --- a/src/services/commandMetrics.ts +++ b/src/services/commandMetrics.ts @@ -5,7 +5,7 @@ import { COMMAND_USAGE } from '@/const' /** * Increment command execution count and check review request */ -export const incrementCommandExecutionCount = async (): Promise => { +export const incrementCommandExecutionCount = async (tabId?: number): Promise => { try { const settings = await Settings.get() let count = settings.commandExecutionCount ?? 0 @@ -21,9 +21,13 @@ export const incrementCommandExecutionCount = async (): Promise => { // Show review request when threshold is exceeded if ((count === COMMAND_USAGE.REVIEW_THRESHOLD || (count > COMMAND_USAGE.REVIEW_THRESHOLD && (count - COMMAND_USAGE.REVIEW_THRESHOLD) % COMMAND_USAGE.REVIEW_INTERVAL === 0)) && !hasShown) { - const tabs = await chrome.tabs.query({ active: true }) - if (tabs[0]?.id) { - await Ipc.sendTab(tabs[0].id, TabCommand.showReviewRequest) + if (!tabId) { + const tabs = await chrome.tabs.query({ active: true }) + tabId = tabs[0].id + } + if (tabId) { + await Ipc.ensureConnection(tabId) + await Ipc.sendTab(tabId, TabCommand.showReviewRequest) } } } catch (error) { diff --git a/src/services/pageAction/background.ts b/src/services/pageAction/background.ts index aa8912d2..39166eae 100644 --- a/src/services/pageAction/background.ts +++ b/src/services/pageAction/background.ts @@ -274,8 +274,6 @@ export const openAndRun = ( response: (res: unknown) => void, ): boolean => { const open = async () => { - await incrementCommandExecutionCount() - let tabId: number if (param.openMode === PAGE_ACTION_OPEN_MODE.POPUP) { @@ -316,6 +314,8 @@ export const openAndRun = ( // Run the steps on the popup. run({ ...param, tabId, steps }, sender, response) + + await incrementCommandExecutionCount() } open() return true From c5ece6ac92e2d639bcdec68be0f1030cce767afb Mon Sep 17 00:00:00 2001 From: ujiro99 Date: Fri, 16 May 2025 22:16:11 +0900 Subject: [PATCH 3/3] Update: Enables using secontary for PageAction. --- src/action/pageAction.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/action/pageAction.ts b/src/action/pageAction.ts index 7e5c5c11..66644d83 100644 --- a/src/action/pageAction.ts +++ b/src/action/pageAction.ts @@ -1,11 +1,11 @@ import { Ipc, BgCommand } from '@/services/ipc' import { getScreenSize } from '@/services/dom' import { isValidString, isPageActionCommand } from '@/lib/utils' -import { POPUP_TYPE } from '@/const' +import { POPUP_TYPE, PAGE_ACTION_OPEN_MODE } from '@/const' import type { ExecProps } from './index' export const PageAction = { - async execute({ selectionText, command, position }: ExecProps) { + async execute({ selectionText, command, position, useSecondary }: ExecProps) { if (!isPageActionCommand(command)) { console.error('command is not for PageAction.') return @@ -22,6 +22,11 @@ export const PageAction = { const clipboard = await navigator.clipboard.readText() const urls = [command.pageActionOption?.startUrl] + const openMode = useSecondary + ? command.pageActionOption.openMode === PAGE_ACTION_OPEN_MODE.TAB + ? PAGE_ACTION_OPEN_MODE.POPUP + : PAGE_ACTION_OPEN_MODE.TAB + : command.pageActionOption.openMode Ipc.send(BgCommand.openAndRunPageAction, { commandId: command.id, @@ -35,7 +40,7 @@ export const PageAction = { selectedText: selectionText, clipboardText: clipboard, srcUrl: location.href, - openMode: command.pageActionOption.openMode, + openMode, }) }, }