diff --git a/manifest.json b/manifest.json index c88dc76..53e2d59 100644 --- a/manifest.json +++ b/manifest.json @@ -28,7 +28,7 @@ "name": "Search and Replace", "description": "__MSG_ext_description__", "manifest_version": 3, - "permissions": ["activeTab", "storage", "notifications", "tabs"], + "permissions": ["activeTab", "storage", "notifications", "tabs", "scripting"], "host_permissions": ["http://*/*", "https://*/*"], "update_url": "http://clients2.google.com/service/update2/crx", "version":"2.3.0", diff --git a/server.log b/server.log new file mode 100644 index 0000000..ddbd402 --- /dev/null +++ b/server.log @@ -0,0 +1,8 @@ + +> search_and_replace@2.3.0 start +> light-server --no-reload -s . -p 9000 -w "**/*.html,**/*.css,**/*.js,**/*.gz" + +light-server is listening at http://0.0.0.0:9000 + serving static dir: . + +## WARNING: Ignoring watch expression "**/*.html,**/*.css,**/*.js,**/*.gz", because it doesn't specify a command and live-reloading is disabled. diff --git a/src/background/install.ts b/src/background/install.ts index 0509170..2a73e01 100644 --- a/src/background/install.ts +++ b/src/background/install.ts @@ -2,13 +2,42 @@ import { LangList } from '../types' import { getAvailableLanguages } from '../util' import { getDefaultStorage } from './storage' +// Content scripts to inject (must match manifest.json content_scripts) +const CONTENT_SCRIPTS = ['searchreplace.js', 'options.js', 'popup.js', 'help.js', 'util.js', 'elements.js'] + +// Inject content scripts into existing tabs +async function injectContentScripts() { + try { + const tabs = await chrome.tabs.query({}) + for (const tab of tabs) { + // Only inject into http(s) and file URLs (matching manifest.json) + if (tab.id && tab.url && (tab.url.startsWith('http://') || tab.url.startsWith('https://') || tab.url.startsWith('file://'))) { + try { + for (const script of CONTENT_SCRIPTS) { + await chrome.scripting.executeScript({ + target: { tabId: tab.id, allFrames: true }, + files: [script], + }) + } + console.debug(`BACKGROUND: Injected content scripts into tab ${tab.id}`) + } catch (error) { + // Ignore errors for tabs where we can't inject (e.g., chrome:// URLs, extension pages) + console.debug(`BACKGROUND: Could not inject into tab ${tab.id}:`, error) + } + } + } + } catch (error) { + console.error('BACKGROUND: Error injecting content scripts:', error) + } +} + export function listenerInstall(details: chrome.runtime.InstalledDetails) { if (details.reason === 'install') { chrome.notifications.create('install', { type: 'basic', title: 'Search and Replace', iconUrl: 'assets/icon-32.png', - message: 'Thanks for installing. Remember to REFRESH the page you wish to replace text on before using!', + message: 'Thanks for installing. You can now use Search and Replace on any open tabs!', priority: 2, buttons: [{ title: 'Ok' }], }) @@ -34,5 +63,13 @@ export function listenerInstall(details: chrome.runtime.InstalledDetails) { }) }) }) + + // Inject content scripts into existing tabs + injectContentScripts() + } + + // Also inject on update to ensure all tabs have the latest scripts + if (details.reason === 'update') { + injectContentScripts() } }