Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions src/action/pageAction.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -35,7 +40,7 @@ export const PageAction = {
selectedText: selectionText,
clipboardText: clipboard,
srcUrl: location.href,
openMode: command.pageActionOption.openMode,
openMode,
})
},
}
75 changes: 21 additions & 54 deletions src/background_script.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand All @@ -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<void> => {
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 & {
Expand Down Expand Up @@ -144,9 +108,25 @@ const commandFuncs = {
return true
},

[BgCommand.openTab]: (param: openTabProps, sender: Sender, response: (res: unknown) => void): boolean => {
getCurrentTab().then(tab => {
const currentTab = sender.tab ?? tab
chrome.tabs.create({
...param,
windowId: currentTab.windowId,
index: currentTab.index + 1,
}, (newTab) => {
incrementCommandExecutionCount(newTab.id).then(() => {
response(true)
})
})
})
return true
},

[BgCommand.openOption]: (): boolean => {
chrome.tabs.create({
url: CONSTANTS.OPTION_PAGE,
url: OPTION_PAGE_PATH,
})
return false
},
Expand All @@ -168,7 +148,7 @@ const commandFuncs = {
pageRules,
}, true)
chrome.tabs.create({
url: `${CONSTANTS.OPTION_PAGE}#pageRules`,
url: `${OPTION_PAGE_PATH}#pageRules`,
})
}
add()
Expand Down Expand Up @@ -218,19 +198,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,
Expand Down Expand Up @@ -445,7 +412,7 @@ const updateWindowSize = async (

chrome.action.onClicked.addListener(() => {
chrome.tabs.create({
url: CONSTANTS.OPTION_PAGE,
url: OPTION_PAGE_PATH,
})
})

Expand Down
11 changes: 11 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions src/services/commandMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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 (tabId?: number): Promise<void> => {
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) {
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) {
console.error('Failed to increment command execution count:', error)
}
}
3 changes: 3 additions & 0 deletions src/services/pageAction/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -313,6 +314,8 @@ export const openAndRun = (

// Run the steps on the popup.
run({ ...param, tabId, steps }, sender, response)

await incrementCommandExecutionCount()
}
open()
return true
Expand Down