Skip to content
Draft
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 manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions server.log
Original file line number Diff line number Diff line change
@@ -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.
39 changes: 38 additions & 1 deletion src/background/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }],
})
Expand All @@ -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()
}
}
Loading