From af07bed13e39d4277be5eadac750dbba12a30c6f Mon Sep 17 00:00:00 2001 From: Mikhail Korolev Date: Thu, 7 Dec 2023 00:15:15 +0100 Subject: [PATCH] Extremely stupid hack with per-tab panels --- package.json | 2 +- script/post-build.js | 11 ++++++++ src/background/index.ts | 18 +++++++++++++ src/components/SidePanel.svelte | 21 ++++++++++----- src/manifest.config.ts | 1 + src/manifest.dev.config.ts | 46 +++++++++++++++++++++++++++++++++ src/sidepanel/index.ts | 9 ------- tsconfig.node.json | 2 +- vite.config.ts | 10 +++++-- 9 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 script/post-build.js create mode 100644 src/manifest.dev.config.ts diff --git a/package.json b/package.json index ca38cea..da8e54a 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ }, "scripts": { "dev": "vite", - "build": "vite build", + "build": "vite build && node script/post-build.js", "check": "svelte-check --tsconfig ./tsconfig.json" }, "devDependencies": { diff --git a/script/post-build.js b/script/post-build.js new file mode 100644 index 0000000..529aab0 --- /dev/null +++ b/script/post-build.js @@ -0,0 +1,11 @@ +console.log('Removing side_panel from manifest, be ready...') + +import manifest from '../dist/manifest.json' assert { type: "json" }; +import fs from 'fs/promises' + +const newManifest = {...manifest} +delete newManifest.side_panel + +fs.writeFile('./dist/manifest.json', JSON.stringify(newManifest, null, 2)) + +console.log('side_panel eliminated') diff --git a/src/background/index.ts b/src/background/index.ts index feaf299..81deaf1 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,6 +1,7 @@ import {storageLocal, storageSync} from "../storage"; // @ts-ignore import contentScript from '../content?script' +// import panel from '../sidepanel/sidepanel.html?url' // Background service workers // https://developer.chrome.com/docs/extensions/mv3/service_workers/ @@ -12,6 +13,23 @@ chrome.runtime.onInstalled.addListener(() => { chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }) .catch((error) => console.error(error)); + +// Completely stupid hack, openPanelOnActionClick: true is only able to open a panel that persists through all the tabs +// the only way to avoid this is to remove side_panel.default_path from manifest, set openPanelOnActionClick to true and then +// for every tab you navigate to you would set tabId for the side panel in the listener +// +// It is also not possible to use chrome.sidePanel.open with the needed tab.id because it demands user interaction first. +// Clicking on the action (chrome.action.onClicked) is not considered a user interaction... however it works on the second attempt +chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => { + if (!tab.url) return; + const url = new URL(tab.url); + await chrome.sidePanel.setOptions({ + tabId: tab.id, + path: 'src/sidepanel/sidepanel.html', + enabled: true + }); +}); + chrome.runtime.onMessage.addListener( async function(request, sender, sendResponse) { const [tab] = await chrome.tabs.query({active: true, lastFocusedWindow: true}); diff --git a/src/components/SidePanel.svelte b/src/components/SidePanel.svelte index 874c1b7..cb7daaf 100644 --- a/src/components/SidePanel.svelte +++ b/src/components/SidePanel.svelte @@ -10,6 +10,7 @@ let talks: Talk[] = []; let lastSyncTime: string = "never"; let selectedTalk: Talk; + let submitInitialized = false; onMount(() => { storageLocal.get().then(({ talkList, lastSyncedAt }) => { @@ -24,19 +25,27 @@ const onRoll = async () => { await storageLocal.set({ selectedTalk: selectedTalk.id }) await chrome.runtime.sendMessage({ submitTalk: selectedTalk.id }); + submitInitialized = true }
-

What are we submitting today?

- {#if lastSyncTime === "never"}Looks like you have not yet set up Github sync or have never synced. You can do that in extension options.{/if} - {#if talks.length === 0 && lastSyncTime !== "never"}Looks like you have not added any talks. You can do that by pushing any suitable markdown files to your repo.{/if} - {#if talks.length > 0} - + {/if} + {:else} +

You're on your way to submit your next CFP, way to go!

{/if} + {#if selectedTalk}
-