From d1ff05f6a760da85a15b5a3bfc89ab5107d1f97b Mon Sep 17 00:00:00 2001 From: Ivan Melentyev Date: Sun, 3 Nov 2024 02:45:12 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=BE=D1=82=D0=BA=D1=80=D1=8B=D1=82?= =?UTF-8?q?=D0=B8=D0=B5=20sidePanel=20=D0=B2=20chromium,=20=D0=B8=D0=BD?= =?UTF-8?q?=D0=B8=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=BE=D0=B2=D1=8B=D1=85?= =?UTF-8?q?=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +- src/background/main.ts | 99 +++++++++------- src/composables/useShikimoriMetaName.ts | 10 +- src/manifest.ts | 149 ++++++++++++------------ 4 files changed, 135 insertions(+), 127 deletions(-) diff --git a/package.json b/package.json index 02411b1..eba797a 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "shikimori-search-extension", "displayName": "Shiki Search", - "version": "1.0.1", + "version": "1.0.2", "private": true, "packageManager": "pnpm@9.7.1", - "description": "[description]", + "description": "Расширение для добавления поисковых кнопок на сайте Shikimori", "scripts": { "dev": "npm run clear && cross-env NODE_ENV=development run-p dev:*", "dev-firefox": "npm run clear && cross-env NODE_ENV=development EXTENSION=firefox run-p dev:*", diff --git a/src/background/main.ts b/src/background/main.ts index 9171cad..d91a963 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -1,59 +1,68 @@ -import {onMessage, sendMessage} from 'webext-bridge/background' -import type {Tabs} from 'webextension-polyfill' +import { onMessage, sendMessage } from "webext-bridge/background"; +import type { Tabs } from "webextension-polyfill"; -const isFirefox = navigator.userAgent.includes('Firefox'); +const isFirefox = navigator.userAgent.includes("Firefox"); // only on dev mode if (import.meta.hot) { - // @ts-expect-error for background HMR - import('/@vite/client') - // load latest content script - import('./contentScriptHMR') + // @ts-expect-error for background HMR + import("/@vite/client"); + // load latest content script + import("./contentScriptHMR"); } if (isFirefox) { -// для переключения боковой панели с помощью кнопки действия в хроме: - browser.action.onClicked.addListener(() => { - browser.sidebarAction.toggle() - }) + // для переключения боковой панели с помощью кнопки действия в хроме: + browser.action.onClicked.addListener(() => { + browser.sidebarAction.toggle(); + }); +} else { + // @ts-expect-error missing types + browser.sidePanel + .setPanelBehavior({ openPanelOnActionClick: true }) + .catch((error: unknown) => console.error(error)); } browser.runtime.onInstalled.addListener((): void => { - // eslint-disable-next-line no-console - console.log('Extension installed') -}) + // eslint-disable-next-line no-console + console.log("Extension installed"); +}); -let previousTabId = 0 +let previousTabId = 0; // пример коммуникации: отправьте заголовок предыдущей вкладки с фоновой страницы // смотрите shim.d.ts -browser.tabs.onActivated.addListener(async ({tabId}) => { - if (!previousTabId) { - previousTabId = tabId - return - } - - let tab: Tabs.Tab - - try { - tab = await browser.tabs.get(previousTabId) - previousTabId = tabId - } catch { - return - } - - sendMessage('tab-prev', {title: tab.title}, {context: 'content-script', tabId}) -}) - -onMessage('get-current-tab', async () => { - try { - const tab = await browser.tabs.get(previousTabId) - return { - title: tab?.title, - } - } catch { - return { - title: undefined, - } - } -}) +browser.tabs.onActivated.addListener(async ({ tabId }) => { + if (!previousTabId) { + previousTabId = tabId; + return; + } + + let tab: Tabs.Tab; + + try { + tab = await browser.tabs.get(previousTabId); + previousTabId = tabId; + } catch { + return; + } + + sendMessage( + "tab-prev", + { title: tab.title }, + { context: "content-script", tabId }, + ); +}); + +onMessage("get-current-tab", async () => { + try { + const tab = await browser.tabs.get(previousTabId); + return { + title: tab?.title, + }; + } catch { + return { + title: undefined, + }; + } +}); diff --git a/src/composables/useShikimoriMetaName.ts b/src/composables/useShikimoriMetaName.ts index 559b0e4..1281ee6 100644 --- a/src/composables/useShikimoriMetaName.ts +++ b/src/composables/useShikimoriMetaName.ts @@ -59,14 +59,15 @@ export function useShikimoriMetaName() { const targetEl = document.querySelector( ".block:has(.b-external_link), .block.block-shiki-search-extension", ); - if (!targetEl || init) { + const classContains = Boolean( + targetEl?.classList.contains("block-shiki-search-extension"), + ); + if (!targetEl || (init && classContains)) { el.value = targetEl ?? undefined; return; } - init = true; - - if (!targetEl.classList.contains("block-shiki-search-extension")) { + if (!classContains) { targetEl.classList.add("block-shiki-search-extension"); } @@ -75,6 +76,7 @@ export function useShikimoriMetaName() { link.remove(); }); el.value = targetEl ?? undefined; + init = true; } return { el, metaName }; diff --git a/src/manifest.ts b/src/manifest.ts index 4fd6caa..ed36fcd 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -1,85 +1,82 @@ -import fs from 'fs-extra' -import type {Manifest} from 'webextension-polyfill' -import type PkgType from '../package.json' -import {isDev, isFirefox, port, r} from '../scripts/utils' +import fs from "fs-extra"; +import type { Manifest } from "webextension-polyfill"; +import type PkgType from "../package.json"; +import { isDev, isFirefox, port, r } from "../scripts/utils"; interface WebExtensionChrome { - side_panel?: { default_path: string } + side_panel?: { default_path: string }; } export async function getManifest() { - const pkg = await fs.readJSON(r('package.json')) as typeof PkgType - const manifest: Manifest.WebExtensionManifest & WebExtensionChrome = { - manifest_version: 3, - name: pkg.displayName || pkg.name, - version: pkg.version, - description: pkg.description, - action: { - default_icon: './assets/icon-512.png', - // default_popup: './dist/popup/index.html', + const pkg = (await fs.readJSON(r("package.json"))) as typeof PkgType; + const manifest: Manifest.WebExtensionManifest & WebExtensionChrome = { + manifest_version: 3, + name: pkg.displayName || pkg.name, + version: pkg.version, + description: pkg.description, + action: { + default_icon: "./assets/icon-512.png", + // default_popup: './dist/popup/index.html', + }, + // не работает для chromium + sidebar_action: { + default_panel: "dist/sidepanel/index.html", + open_at_install: true, + }, + side_panel: { + default_path: "dist/sidepanel/index.html", + }, + // options_ui: { + // page: "./dist/options/index.html", + // open_in_tab: true, + // }, + background: isFirefox + ? { + scripts: ["dist/background/index.mjs"], + type: "module", + } + : { + service_worker: "./dist/background/index.mjs", }, - // does not work for chromium based - sidebar_action: { - default_panel: 'dist/sidepanel/index.html', - open_at_install: true, - }, - side_panel: { - default_path: 'dist/sidepanel/index.html', - }, - options_ui: { - page: './dist/options/index.html', - open_in_tab: true, - }, - background: isFirefox - ? { - scripts: ['dist/background/index.mjs'], - type: 'module', - } - : { - service_worker: './dist/background/index.mjs', - }, - icons: { - 16: './assets/icon-512.png', - 48: './assets/icon-512.png', - 128: './assets/icon-512.png', - }, - permissions: [ - 'tabs', - 'storage', - 'activeTab', - 'sidePanel', - ], - host_permissions: ['*://*/*'], - content_scripts: [ - { - matches: [ - 'https://*.shikimori.one/*', - ], - js: [ - 'dist/contentScripts/index.global.js', - ], - }, - ], - web_accessible_resources: [ - { - resources: ['dist/contentScripts/style.css'], - matches: [''], - }, - ], - content_security_policy: { - extension_pages: isDev - // this is required on dev for Vite script to load - ? `script-src \'self\' http://localhost:${port}; object-src \'self\'` - : 'script-src \'self\'; object-src \'self\'', - }, - } - - if (isFirefox) { - delete manifest.side_panel + icons: { + 16: "./assets/icon-512.png", + 48: "./assets/icon-512.png", + 128: "./assets/icon-512.png", + }, + permissions: ["tabs", "storage", "activeTab", "sidePanel"], + host_permissions: ["*://*/*"], + content_scripts: [ + { + matches: ["https://*.shikimori.one/*"], + js: ["dist/contentScripts/index.global.js"], + }, + ], + web_accessible_resources: [ + { + resources: ["dist/contentScripts/style.css"], + matches: [""], + }, + ], + content_security_policy: { + extension_pages: isDev + ? // this is required on dev for Vite script to load + `script-src \'self\' http://localhost:${port}; object-src \'self\'` + : "script-src 'self'; object-src 'self'", + }, + // не нужно для chromium + browser_specific_settings: { + gecko: { + id: "shikimori-search-extension@example.com", + }, + }, + }; - } else { - delete manifest.sidebar_action - } + if (isFirefox) { + delete manifest.side_panel; + } else { + delete manifest.sidebar_action; + delete manifest.browser_specific_settings; + } - return manifest + return manifest; }