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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "selection-command-monorepo",
"version": "0.16.0",
"version": "0.17.0",
"private": true,
"description": "Selection Command - Monorepo for Chrome Extension and Hub",
"author": "ujiro99",
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "__MSG_extName__",
"description": "__MSG_extDescription__",
"version": "0.16.0",
"version": "0.17.0",
"default_locale": "en",
"icons": {
"128": "icon128.png"
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selection-command/extension",
"version": "0.16.0",
"version": "0.17.0",
"private": true,
"author": "ujiro99",
"license": "MIT",
Expand Down
38 changes: 29 additions & 9 deletions packages/extension/src/action/sidePanel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isValidString, toUrl } from "@/lib/utils"
import { isValidString, toUrl, isEmpty } from "@/lib/utils"
import { SPACE_ENCODING } from "@/const"
import type { ExecuteCommandParams } from "@/types"
import type { ExecuteCommandParams, ShowToastParam } from "@/types"
import type { OpenSidePanelProps } from "@/services/chrome"
import { Ipc, BgCommand } from "@/services/ipc"
import { Ipc, BgCommand, TabCommand } from "@/services/ipc"
import { t } from "@/services/i18n"

export const SidePanel = {
async execute({
Expand All @@ -14,14 +15,33 @@ export const SidePanel = {
console.error("searchUrl is not valid.")
return
}

// Read clipboard text for interpolation, but don't block execution if it fails.
let clipboardText: string = ""
try {
const url = toUrl({
searchUrl: command.searchUrl,
spaceEncoding: command.spaceEncoding ?? SPACE_ENCODING.PLUS,
selectionText,
useClipboard: useClipboard ?? false,
if (useClipboard && isEmpty(selectionText)) {
clipboardText = await navigator.clipboard.readText()
}
} catch (e) {
console.warn("Failed to read clipboard text:", e)

const tabId = await Ipc.getActiveTabId()
await Ipc.sendTab<ShowToastParam>(tabId, TabCommand.showToast, {
title: t("clipboard_error_title"),
description: t("clipboard_error_description"),
action: t("clipboard_error_action"),
})
}

try {
const url = toUrl(
{
searchUrl: command.searchUrl,
spaceEncoding: command.spaceEncoding ?? SPACE_ENCODING.PLUS,
selectionText,
useClipboard: useClipboard ?? false,
},
clipboardText,
)

Ipc.send<OpenSidePanelProps>(BgCommand.openSidePanel, {
url,
Expand Down
14 changes: 7 additions & 7 deletions packages/extension/src/background_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,19 +580,12 @@ chrome.commands.onCommand.addListener(async (commandName) => {
return
}

// Get active tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true })
const command = settings.commands.find((c) => c.id === shortcut.commandId)
if (!command) {
console.warn(`Command not found: ${shortcut.commandId}`)
return
}

const enableSendTab =
tab?.id &&
!tab.url?.startsWith("chrome") &&
!tab.url?.includes("chromewebstore.google.com")

const selectionText = await Storage.get<string>(
SESSION_STORAGE_KEY.SELECTION_TEXT,
)
Expand All @@ -613,6 +606,13 @@ chrome.commands.onCommand.addListener(async (commandName) => {
}
}

// Get active tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true })
const enableSendTab =
tab?.id &&
!tab.url?.startsWith("chrome") &&
!tab.url?.includes("chromewebstore.google.com")

let ret: unknown
if (enableSendTab) {
// Execute command in tab
Expand Down
16 changes: 9 additions & 7 deletions packages/extension/src/services/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,7 @@ export const openSidePanel = async (
): Promise<{ tabId: number | undefined }> => {
const { url, tabId } = param

const targetTabId = tabId
if (!targetTabId) {
if (!tabId) {
console.warn("No valid tab ID for side panel")
return {
tabId: undefined,
Expand All @@ -692,17 +691,20 @@ export const openSidePanel = async (
// Set the side panel options for the tab
// Do not await here because sidePanel.open() must be executed within a user gesture.
chrome.sidePanel.setOptions({
tabId: targetTabId,
tabId,
path: toUrl(url),
enabled: true,
})

// Open the side panel
await chrome.sidePanel.open({ tabId: targetTabId })

return {
tabId: targetTabId,
try {
await chrome.sidePanel.open({ tabId })
} catch (e) {
console.error("Failed to open side panel:", e)
throw e
}

return { tabId }
}

const SIDE_PANEL_CLOSE_ANIMATION = 1000
Expand Down
Loading